Curl php请求错误
问题描述:
我试图通过使用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
有没有人知道如何解决这个问题?
答
您使用CURLOPT_HEADER
(这是一个布尔值,表明要在输出中的标题),但你需要CURLOPT_HTTPHEADER
(这是用来传递一个数组与标头的请求):
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"user-key: *******",
"Accept: application/json"
));