2017-09-02 131 views
2

我試圖通過使用cURL的php設置API調用。 API文檔給出了一個例子,以從Java到這個API的調用:Curl php請求錯誤

HttpResponse<JsonNode> response = Unirest.get("https://api2445582011268.apicast.io/games/1942?fields=*") 
.header("user-key", "*******") 
.header("Accept", "application/json") 
.asJson(); 

這是我試圖在PHP它轉換成捲曲電話:

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "https://api2445582011268.apicast.io/games/1942?fields=*"); 
curl_setopt($ch, CURLOPT_HEADER, array(
    "user-key: *******", 
    "Accept: application/json" 
    )); 
$output = curl_exec($ch); 
echo $output; 
curl_close($ch); 

但是我的PHP是呼應以下輸出:

HTTP/1.1 403 Forbidden Content-Type: text/plain; charset=us-ascii Date: Sat, 02 Sep 2017 02:52:40 GMT Server: openresty/1.9.15.1 Content-Length: 33 Connection: keep-alive Authentication parameters missing1 

有沒有人知道如何解決這個問題?

回答

2

您使用CURLOPT_HEADER(這是一個布爾值,表明要在輸出中的標題),但你需要CURLOPT_HTTPHEADER(這是用來傳遞一個數組與標頭的請求):

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "user-key: *******", 
    "Accept: application/json" 
));