我們想從 linux 終端與我們使用 curl 的第 3 方 API 進行通信。curl 是 - curl -X POST \ \ -H 'Authorization: Bearer' \ -H 'Content-Type: application/json'當我們觸發這個 curl 時,我們會得到預期的響應。但是,當我們嘗試從 PHP 腳本執行此操作時,我們會收到錯誤消息 - HTTP ERROR 500PHP代碼片段是 -<?php$ch = curl_init();curl_setopt($ch, CURLOPT_URL, <URL>);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_POST, 1);//curl_setopt($ch, CURLOPT_POSTFIELDS,'');curl_setopt($ch, CURLOPT_VERBOSE,true);$headers = array();$headers[] = 'Authorization: Bearer <token>';$headers[] = 'Content-Type: application/json';curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);$result = curl_exec($ch);echo $result;if (curl_errno($ch)) { echo 'Error:' . curl_error($ch);}curl_close($ch);?>當詳細模式打開時,收到的響應是 -* Trying <IP>...* Connected to <URL> (<IP>) port 443 (#0)* ALPN, offering http/1.1* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH* successfully set certificate verify locations:* CAfile: /etc/ssl/certs/ca-certificates.crt CApath: /etc/ssl/certs* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256* ALPN, server did not agree to a protocol* Server certificate:* subject: OU=Domain Control Validated; CN=* <domain>* start date: Dec 17 10:41:01 2017 GMT* expire date: Dec 17 10:41:01 2020 GMT* subjectAltName: <URL> matched* issuer: C=US; ST=Arizona; L=Scottsdale; O=GoDaddy.com, Inc.; OU=http://certs.godaddy.com/repository/; CN=Go Daddy Secure Certificate Authority - G2* SSL certificate verify ok.> POST /app/auth HTTP/1.1Host: <URL>Accept: */*Authorization: Bearer <Token>Content-Type: application/jsonExpect: 100-continue請注意,在此處發布問題時,我已經替換了實際的 URL、IP 和令牌。為了確保 PHP curl 沒有問題,我們使用 curl-to-PHP 代碼生成器實用程序來生成代碼 ( http://incarnate.github.io/curl-to-php/ )。有人可以幫助我,讓我知道可能出了什么問題。
1 回答

縹緲止盈
TA貢獻2041條經驗 獲得超4個贊
為了避免?500 error?(例如),請務必:
set proper "Referer: " header if needed, with
curl_setopt(CURLOPT_REFERER, 'ref page');
set proper "User-Agent: " header, with
curl_setopt(CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)')
- 1 回答
- 0 關注
- 198 瀏覽
添加回答
舉報
0/150
提交
取消