2014-11-04 67 views
0

我試圖使用cURL向API(Xero,具體)發送請求,但我顯然發送空請求。我檢查了cURL信息,它看起來像我沒有Content-Type集合,即使我已經在代碼中設置了它。內容類型未在cURL中設置爲PHP

這裏是我的代碼:

$content = $this->getContent(); 
    $headers = [ 
     "Content-Type: application/x-www-form-urlencoded", 
     "Content-Length: " . strlen($content), 
     "Connection: close" 
    ]; 

    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $this->getUrl()); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 10); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($ch, CURLOPT_HEADER, true); 

    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $content); 

    de($headers, curl_getinfo($ch)); 

de()是AA「轉儲和退出」的功能,它輸出這樣的:

array(3) { 
    [0] => 
     string(47) "Content-Type: application/x-www-form-urlencoded" 
    [1] => 
     string(20) "Content-Length: 1003" 
    [2] => 
     string(17) "Connection: close" 
} 

array(26) { 
    'url' => 
    string(41) "https://api.xero.com/api.xro/2.0/Invoices" 
    'content_type' => 
    NULL 
    'http_code' => 
    int(0) 
    'header_size' => 
    int(0) 
    'request_size' => 
    int(0) 
    'filetime' => 
    int(0) 
    'ssl_verify_result' => 
    int(0) 
    'redirect_count' => 
    int(0) 
    'total_time' => 
    double(0) 
    'namelookup_time' => 
    double(0) 
    'connect_time' => 
    double(0) 
    'pretransfer_time' => 
    double(0) 
    'size_upload' => 
    double(0) 
    'size_download' => 
    double(0) 
    'speed_download' => 
    double(0) 
    'speed_upload' => 
    double(0) 
    'download_content_length' => 
    double(-1) 
    'upload_content_length' => 
    double(-1) 
    'starttransfer_time' => 
    double(0) 
    'redirect_time' => 
    double(0) 
    'certinfo' => 
    array(0) { 
    } 
    'primary_ip' => 
    string(0) "" 
    'primary_port' => 
    int(0) 
    'local_ip' => 
    string(0) "" 
    'local_port' => 
    int(0) 
    'redirect_url' => 
    string(0) "" 
} 

至於我能看到我的正確設置標頭(常數拼寫正確,我沒有多次設置它們)。我究竟做錯了什麼?

+1

你應該轉讓一些東西之前打電話curl_getinfo – matteo 2014-11-04 11:34:13

+0

啊,是的,我的壞。看起來像這個問題是別的東西:( – 2014-11-04 11:49:21

回答

2

curl_error功能click me for detail!!

首先檢查,因爲您的網址是https。因此遵循ssl驗證過程。

非正式修復:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 

正式修復:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/CAcerts/your_certificate.crt"); 

在哪裏證書?

  1. 瀏覽網址在Firefox瀏覽器

Go to the site

  • 按此鎖定在地址欄的左墊

  • 然後點擊更多信息 more info

  • 現在點擊view certificate按鈕 certificate

  • 在打開彈出窗口,你會看到在點擊詳細信息選項卡(快捷ALT + D)兩個選項卡。

  • 然後單擊導出按鈕保存證書

  • export

  • 也就是說,你必須給這裏

    curl_setopt路徑( $ ch,CURLOPT_CAINFO,getcwd()。「/CAcerts/your_certificate.crt」)