更改url后PHP CURL错误

时间:2011-08-03 20:26:06

标签: php curl

出于某种原因,我的CURL现在还没有工作,我所做的就是更改网址(就像之前我使用它来调用来自速度世界服务器的需要的信息)并且它工作完美无缺,现在我正在尝试使用它IMDBAPI,它给了我一个错误。

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.

我是CURL的菜鸟,所以感谢任何帮助。

3 个答案:

答案 0 :(得分:2)

您应该urlencode() $title

答案 1 :(得分:0)

http://www.imdbapi.com/?t=The%20Green%20Mile&r=XML

这个对我来说很好。尝试rawurlencode该标题

$title = rawurlencode($title);

答案 2 :(得分:0)

正如史所说,你需要对值进行编码:

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

您可以通过以下方式获取请求的http代码:

$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);