开发者

PHP CURL Error after changing only url

开发者 https://www.devze.com 2023-03-26 04:59 出处:网络
For some reason my CURL isnt working now, all i did was change the url (as before i was using this to call info from the need for speed world servers) and it worked flawlessly, now I am trying to use

For some reason my CURL isnt working now, all i did was change the url (as before i was using this to call info from the need for speed world servers) and it worked flawlessly, now I am trying to use it with IMDBAPI and it gives me an error.

url i type in:

http://localhos开发者_C百科t/movie.php?title=The Green Mile

Code:

<?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 />";    

    ?> 

Error:

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

I am a noob to CURL so any help is appreciated.


You should urlencode() the $title.


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

This one works fine for me. Try to rawurlencode that title

$title = rawurlencode($title);


As Shi said,you need encode the values:

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

You can get the http code of your request with:

$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号