Titoalvi
Titoalvi

Reputation: 29

PHP Get JSON data from API with access token

I've tried writing a code to get JSON data from API like this

<?php
$url = "http://api.jakarta.go.id/v1/kota/?format=json";
$ch = curl_init();
$accesstoken = "my_token";
$headr = array();
$headr[] = 'Content-length: 0';
$headr[] = 'Content-type: application/json';
$headr[] = 'Authorization: OAuth '.$accesstoken;

curl_setopt($ch, CURLOPT_HTTPHEADER,$headr);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result=curl_exec($ch);

curl_close($ch);

var_dump(json_decode($result, true));
?>

but not working and i get NULL data. please help me :)

Upvotes: 2

Views: 3131

Answers (1)

Hendra Santoso
Hendra Santoso

Reputation: 11

Try this, it works for me.

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://api.jakarta.go.id/v1/kota');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Authorization:my_token'
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>

Upvotes: 1

Related Questions