deviloper
deviloper

Reputation: 7240

header not received in CURL GET request

I am trying the following:

$get_string = '?latitude='.$lat.'&longitude='.$lng.'&type='.$tsp;

$headers = array();
//$headers[] = "x-auth-token: myToken";
$headers['x-auth-token'] = "myToken";
$state_ch = curl_init();
curl_setopt($state_ch, CURLOPT_URL,'myUrl'.$get_string);

curl_setopt($state_ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($state_ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($state_ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($state_ch, CURLOPT_POSTFIELDS, $get_string);
$state_result = curl_exec($state_ch);
//$state_result = json_decode($state_result);

echo "<pre>"; print_r($state_result);

But I get:

<pre>{"data":{"status":"failed","message":"Token is required"}}

What am i missing guys?

Upvotes: 0

Views: 167

Answers (2)

Professor Abronsius
Professor Abronsius

Reputation: 33804

Hopefully the following might help you identify what is going on with your request - use the output shown to aid debugging the request, change the relevant parameters in the config. One thing - is the endpoint https or regular http?

/* config */
$url='http://www.example.com';
$token='banana';

$params=array(
    'latitude'  =>  $lat,
    'longitude' =>  $lng,
    'type'      =>  $tsp
);
$headers = array(
    sprintf('X-AUTH-TOKEN: %s', $token )
);




/* prepare url */
$url=sprintf( '%s?%s', $url, http_build_query( $params ) );
/* stream for advanced debug info */
$vbh = fopen('php://temp', 'w+');


$curl = curl_init( $url );
curl_setopt( $curl, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
/* enhanced debug info */
curl_setopt( $curl,CURLOPT_VERBOSE,true );
curl_setopt( $curl,CURLOPT_NOPROGRESS,true );
curl_setopt( $curl,CURLOPT_STDERR,$vbh );

/* results */
$res=(object)array(
    'response'  =>  curl_exec( $curl ),
    'info'      =>  (object)curl_getinfo( $curl ),
    'errors'    =>  curl_error( $curl )
);
rewind( $vbh );
$res->verbose=stream_get_contents( $vbh );
fclose( $vbh );
curl_close( $curl );



printf( '<pre>%s</pre>', print_r( $res->verbose, true ) );
printf( '<pre>%s</pre>', print_r( $res->info, true ) );

Upvotes: 1

Fernando Bevilacqua
Fernando Bevilacqua

Reputation: 673

curl_setopt expects the headers array to have an entry per header, e.g.

$headers = array(
    'X-Auth-Token: myToken'
);

Since you are using an associative array for your headers, probably only the string "myToken" is being sent.

Docs: http://www.php.net/manual/en/function.curl-setopt.php

Upvotes: 0

Related Questions