PHP Curl GET呼叫故障排除

时间:2013-02-13 21:11:56

标签: php curl get xampp

我正在构建一个对API进行标准HTTP调用的网站。我的第一个电话是直接的GET,没有使用基本auth的参数。我在我的PHP中使用Curl。我通过本地安装的XAMPP运行它。我的电话不起作用,但如果我有一个同事在运行旧版本的ubuntu PHP的Linux机器上运行php,它运行正常。解决此问题的最佳方法是什么?我的猜测是我的XAMPP安装,但有一个很好的故障排除方法吗?我在curl会话中使用了curl_getinfo来获取返回值,据我所知,它似乎没有提供太多的见解。

这是curl_getinfo输出:

Array ( 
[url] => https://www.ebusservices.net/webservices/hrpcertws/rbs/api/merchants/267811683882/consumers.xml? 
[content_type] => 
[http_code] => 0 
[header_size] => 0 
[request_size] => 107 
[filetime] => -1 
[ssl_verify_result] => 0 
[redirect_count] => 0 
[total_time] => 0.28 
[namelookup_time] => 0.015 
[connect_time] => 0.015 
[pretransfer_time] => 0 
[size_upload] => 0 
[size_download] => 0
[speed_download] => 0 
[speed_upload] => 0 
[download_content_length] => -1 
[upload_content_length] => -1 
[starttransfer_time] => 0 
[redirect_time] => 0 
[certinfo] => Array ( ) 
[primary_ip] => 127.0.0.1 
[primary_port] => 8888 
[local_ip] => 127.0.0.1 
[local_port] => 59509 
[redirect_url] => 
)

我正在使用:
XAMPP 1.8.1
PHP版本5.4.7
cURL 7.24.0
在Windows 7上

添加了代码:

<?php

    error_reporting(E_ALL);

    $session = 'FALSE';
    // Initialize the session
    $session = curl_init();

    $stderr = fopen("curl.txt", "w+"); 

    // Set curl options
    curl_setopt($session,     CURLOPT_URL, 'https://www.ebusservices.net/webservices/hrpcertws/rbs/api/merchants/12233442/consumers.xml?');
    curl_setopt($session, CURLOPT_STDERR, $stderr);
    curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($session, CURLOPT_USERPWD, "username:pwd");
    curl_setopt($session, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($session, CURLOPT_SSLVERSION, 3);
    curl_setopt($session, CURLOPT_VERBOSE, 1);

    // Make the request
    $response = curl_exec($session);

    print_r(curl_getinfo($session));

    // Close the curl session
    curl_close($session);

    fclose($stderr);

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

    // Check the HTTP Status code
    if(isset($status_code[0]))
    {
         switch( $status_code[0] ) 
    {
            case 100:
                   break;
            case 200:
                   break;
            case 503:
                   die('Your call to HRP API failed and returned an HTTP status of 503. That means: Service unavailable. An internal problem prevented us from returning data to you.');
                   break;
            case 403:
                   die('Your call to HRP API failed and returned an HTTP status of   403. That means: Forbidden. You do not have permission to access this resource, or are over your rate limit.');
                   break;
            case 400:
                  die('Your call to HRP API failed and returned an HTTP status of 400. That means:  Bad request. The parameters passed to the service did not match as expected. The exact error is returned in the XML response.');
                  break;
            case 401:
                  die('Your call to HRP API failed and returned an HTTP status of 401. That means: Unauthorized. The credentials supplied do not have permission to access this resource.');
                  break;
            case 404:
                  die('Page not found.');
                  break;
            default:
                  die('Your call to HRP API returned an unexpected HTTP status of:' . $status_code[0]);
        } 
    }
    else
    { 
       echo 'failed';
    }

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

     // Output the XML
     echo htmlspecialchars($xml, ENT_QUOTES);

?>

1 个答案:

答案 0 :(得分:0)

尝试使用Fiddler查看HTTP流量中的确切内容。

相关问题