D
D
Denis Rybin2016-03-12 13:14:30
PHP
Denis Rybin, 2016-03-12 13:14:30

How to properly pass headers to curl?

how to properly pass headers to curl via php. I don't understand what's wrong, data is not returned. here is my code:
$myAccessToken = 'my-token';
$url = ' https://api.dropboxapi.com/2/users/get_space_usage ';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Authorization: Bearer '.$myAccessToken, 'Content-Type: application/json']);
$curlData = curl_exec($curl);
curl_close($curl);
var_dump(json_decode($curlData, true));

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Pavel Volintsev, 2016-03-12
@RybinDen

$headers = array("Authorization: Bearer <ACCESS_TOKEN>",
                 "Content-Type: application/json");

$ch = curl_init('https://api.dropboxapi.com/2/users/get_space_usage');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "null");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);

curl_close($ch);
echo $response;

Copied from stackoverflow and checked. Returned:
If you specify an invalid token, you will get the following error:
Error in call to API function "users/get_space_usage": The given OAuth 2 access token is malformed.

D
Dimonchik, 2016-03-12
@dimonchik2013

curl_setopt($curlhandle, CURLOPT_VERBOSE, true); will greatly facilitate the task
or fiddler

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question