【问题标题】:PHP Curl GET call troubleshootingPHP Curl GET 调用疑难解答
【发布时间】:2013-01-29 13:41:31
【问题描述】:

我正在构建一个对 API 进行标准 HTTP 调用的网站。我的第一个调用是使用基本身份验证的不带参数的直接 GET。我在我的 php 中使用 Curl。我通过本地安装的 XAMPP 运行它。我的电话不工作,但如果我有一个同事在他的 Linux 机器上运行 php,运行旧版本的 ubuntu 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
卷曲 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);

?>

【问题讨论】:

  • 添加详细选项并查看输出是命令行之一。你应该显示一些代码。
  • 让接收页面做一个php_info() 并检查返回的信息。
  • 我可以从 XAMPP 之外的 curl 命令行运行 GET,它工作正常。有没有办法在 xampp 中使用相同的 curl 运行 curl 命令行?我不知道这是可能的。

标签: php curl get xampp


【解决方案1】:

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

【讨论】:

  • 我尝试使用 Fiddler,但我得到的唯一条目是基本 URL“www.ebusservices.net:443”的初始“CONNECT”。我没有其他条目。它的 TextView 状态发现了 clienthello 握手,我看到密码中的一些条目显示“无法识别的密码”。这是否表明可能存在证书问题?我不这么认为,因为我可以通过 cURL 命令行和 SoapUI 成功处理这个 GET。
  • 我必须先查看 Fiddler 的输出,然后才能真正了解发生了什么。
猜你喜欢
  • 2013-10-22
  • 1970-01-01
  • 2013-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多