2

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?

1 Answer 1

2

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" 
));
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.