bwad
bwad

Reputation: 81

Curl php request error

I'm trying to set up an API call through php using cURL. The API documentation gives an example for a call to this API from Java:

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

This is my attempt at converting it to a cURL call in 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); 

However my php is echoing the following output:

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

Does anyone know how to solve this?

Upvotes: 2

Views: 134

Answers (1)

ishegg
ishegg

Reputation: 9927

You used CURLOPT_HEADER (which is a Boolean to indicate you want the headers in the output), but you need CURLOPT_HTTPHEADER (which is used to pass an array with the headers for the request):

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

Upvotes: 2

Related Questions