2011-08-03 46 views
1

由於某種原因,我的CURL現在不工作,我所做的只是更改網址(因爲之前我使用這個來調用來自速度世界服務器需求的信息),它現在工作完美無缺,現在我正在嘗試將它與IMDBAPI一起使用,它給了我一個錯誤。PHP CURL更改僅url後出現錯誤

網址我輸入:

http://localhost/movie.php?title=The Green Mile 

代碼:

<?php  
    $title = $_GET['title']; 

    //optional comment out or delete  
    error_reporting(E_ALL);  

    // The POST URL and parameters  
    $request = 'http://www.imdbapi.com/?t='.$title.'&r=XML';  

    // Get the curl session object  
    $session = curl_init($request);  

    // Set the POST options.  
    curl_setopt($session, CURLOPT_HEADER, true);  
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);  

    // Do the POST and then close the session  
    $response = curl_exec($session);  
    curl_close($session);  

    // Get HTTP Status code from the response  
    $status_code = array();  
    preg_match('/\d\d\d/', $response, $status_code);  

    // Check for errors  
    switch($status_code[0]) {  
     case 200:  
      // Success  
      break;  
     case 503:  
      die('Service unavailable. An internal problem prevented us from returning data to you.');  
      break;  
     case 403:  
      die('Forbidden. You do not have permission to access this resource, or are over your rate limit.');  
      break;  
     case 400:  
      // You may want to fall through here and read the specific XML error  
      die('Bad request. The parameters passed to the service did not match as expected. The exact error is returned in the XML response.');  
      break;  
     default:  
      die('Your call returned an unexpected HTTP status of:' . $status_code[0]);  
    }  

    // Get the XML from the response, bypassing the header  
    if (!($xml = strstr($response, '<?xml'))) {  
     $xml = null;  
    }  

    // Output the XML  

    $movieInfo = simplexml_load_string($xml);  

     $movieTitle = $movieInfo->movie['title'];  

    echo "Title: $movieTitle <br />";  

    ?> 

錯誤:

Bad request. The parameters passed to the service did not match as expected. The exact error is returned in the XML response. 

我是一個小白卷曲所以任何幫助表示讚賞。

+0

那麼,什麼是在XML響應? –

+0

你怎麼知道?我用什麼打印出來? – rackemup420

+0

'print $ response'。 –

回答

2

您應該urlencode()$title

+0

ty先生,正如我所說我是一個noob使用這個捲曲的東西:D。只要它讓我感到不適就投了! – rackemup420

0

正如施說,你需要編碼值:

$title = rawurlencode($_GET["title"]); 

你可以得到你的要求與HTTP代碼:

$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);