nackolysis
nackolysis

Reputation: 217

Header section of PHP Curl returns error

Hello great Stackoverflow, am testing my API via PHP Curl. The API requires setting of Accept variables and Authorization as headers.

<?php
$header = array(
 'Accept' => 'application/json',
  'Authorization' => 'ab167you89');
?>

When I run the code below, it shows the error below. it seems that the ACCEPT and AUTHORIZATION variables in the header section is not accessible via the API URL CURL. can someone help me fix that. Thanks

Below is the error it shows

{"errors":[{"status":"415","code":"015","title":"unsupported media type","detail":"Header 'Accept' must be set and must be one of: 'application/vnd.api+json', 'application/json', 'text/csv', 'application/xml'"},{"status":"400","code":"032","title":"requires project name in HTTP header Authorization","detail":"Provide the parameter 'Authorization' in HTTP header like 'your_project_name:your_token'."}]}

Below is the full code

<?php


// 0 means unlimited timeout
ini_set('max_execution_time', 0);

$header = array(
 'Accept' => 'application/json',
  'Authorization' => 'ab167you89'

);

$url='https://myapicurl.com/info=100';
$ch = curl_init();

// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER, false); //if you want headers set it to true otherwise set it to false
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, true);
//curl_setopt($ch, CURLOPT_POSTFIELDS,$postdata );    

//curl_setopt($ch, CURLOPT_POSTFIELDS,$fields_string);


$result=curl_exec($ch);
curl_close($ch);
echo '<pre>' . print_r($result, true) . '</pre>';

var_dump(json_decode($result, true));

?>

Upvotes: 1

Views: 981

Answers (3)

Rohit Dhiman
Rohit Dhiman

Reputation: 2741

Change Your $header values to:

$header = array(
 'Accept:application/json',
  'Authorization:ab167you89'

);

Upvotes: 0

Jakub Krawczyk
Jakub Krawczyk

Reputation: 970

As described in the PHP documentation (http://php.net/curl_setopt):

An array of HTTP header fields to set, in the format array('Content-type: text/plain', 'Content-length: 100')

CURLOPT_HTTPHEADER option accepts an array of headers in the following format:

array(
    'Header: value',
    'Header2: value'
);

There is a mistake in your solution, as you're using key => value notation. In your case $header should look like this:

<?php
$header = array(
  'Accept: application/json',
  'Authorization: ab167you89'
);

Upvotes: 1

Jaydeep Gondaliya
Jaydeep Gondaliya

Reputation: 321

<?php

    $url="your api url";
    $apiKey='XXXXX-xXXXXXXXXXXXX-xXXXXX';
    $Realm='XXXXXXXXXXXXX_xXXXXXXXXXXXXXXXXX';
    $ch = curl_init();  

    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array($apiKey,$Realm,
    'Accept: application/json',
    'Content-type: application/x-www-form-urlencoded'
    ));
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $output=curl_exec($ch);

    if($output === false)
    {
        echo "Error Number:".curl_errno($ch)."<br>";
        echo "Error String:".curl_error($ch);
    }
    curl_close($ch);

    $obj = json_decode($output, TRUE);
    ?>

Upvotes: 0

Related Questions