2013-02-13 40 views
0

我正在構建一個網站,可以對API進行標準HTTP調用。我的第一個電話是一個簡單的GET,沒有使用基本身份驗證的參數。我在我的php中使用Curl。我通過XAMPP的本地安裝來運行它。我的電話不工作,但如果我有一個同事在運行舊版本的ubuntu PHP的Linux機器上運行php,它工作正常。解決此問題的最佳方法是什麼?我的猜測是這是與我的XAMPP安裝的東西,但有一個很好的方法來解決?我在我的curl會話中使用了curl_getinfo來獲取返回值,而且據我所知,它似乎沒有提供太多的見解。PHP Curl GET調用故障排除

這裏是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); 

?> 
+0

添加詳細選項並查看輸出是哪一個命令行。你應該顯示一些代碼。 – 2013-02-13 21:16:53

+0

讓接收頁面執行'php_info()'並檢查返回的信息。 – 2013-02-13 21:20:26

+0

我可以從XAMPP以外的curl命令行運行GET,它工作正常。有沒有辦法在xampp中使用相同的curl來運行curl命令行?我不知道這是可能的。 – user1757986 2013-02-13 21:53:11

回答

0

嘗試使用Fiddler查看HTTP流量中的確切內容。

+0

我嘗試使用提琴手,但我得到的唯一條目是最初的「連接」到基地網址「www.ebusservices.net:443」。我沒有得到其他條目。它的TextView表明發現了clienthello握手,並且在密碼中看到了一些表示「無法識別的密碼」的條目。這是否指向可能的證書問題?我不這麼認爲,因爲我可以通過cURL命令行和SoapUI成功處理這個GET。 – user1757986 2013-02-14 14:04:55

+0

在我能真正知道發生了什麼之前,我必須看到提琴手的輸出。 – 2013-02-15 13:11:36