Get JSON object from url

I have a url that returns a JSON object as follows:

{ "expires_in":5180976, "access_token":"AQXzQgKTpTSjs-qiBh30aMgm3_Kb53oIf-VA733BpAogVE5jpz3jujU65WJ1XXSvVm1xr2LslGLLCWTNV5Kd_8J1YUx26axkt1E-vsOdvUAgMFH1VJwtclAXdaxRxk5UtmCWeISB6rx6NtvDt7yohnaarpBJjHWMsWYtpNn6nD87n0syud0" } 

I want to get the access_token value. So how can I get it through PHP?

+120
json php
Mar 25 '13 at 14:31
source share
10 answers
 $json = file_get_contents('url_here'); $obj = json_decode($json); echo $obj->access_token; 

For this, file_get_contents requires allow_url_fopen be enabled. This can be done at runtime by including:

 ini_set("allow_url_fopen", 1); 

You can also use curl to get the url. To use curl, you can use the example found here :

 $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_URL, 'url_here'); $result = curl_exec($ch); curl_close($ch); $obj = json_decode($result); echo $obj->access_token; 
+311
Mar 25 '13 at 14:33
source share
 $url = 'http://.../.../yoururl/...'; $obj = json_decode(file_get_contents($url), true); echo $obj['access_token']; 

Php can also use properties with dashes:

 garex@ustimenko ~/src/ekapusta/deploy $ psysh Psy Shell v0.4.4 (PHP 5.5.3-1ubuntu2.6 β€” cli) by Justin Hileman >>> $q = new stdClass; => <stdClass #000000005f2b81c80000000076756fef> {} >>> $q->{'qwert-y'} = 123 => 123 >>> var_dump($q); class stdClass#174 (1) { public $qwert-y => int(123) } => null 
+25
Mar 25 '13 at 14:35
source share

You can use the json_decode PHP function :

 $url = "http://urlToYourJsonFile.com"; $json = file_get_contents($url); $json_data = json_decode($json, true); echo "My token: ". $json_data["access_token"]; 
+13
Mar 25 '13 at 14:36
source share
 // Get the string from the URL $json = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452'); // Decode the JSON string into an object $obj = json_decode($json); // In the case of this input, do key and array lookups to get the values var_dump($obj->results[0]->formatted_address); 
+7
Nov 19 '15 at 23:06
source share

You need to read about json_decode function http://php.net/manual/en/function.json-decode.php

Here you go

 $json = '{"expires_in":5180976,"access_token":"AQXzQgKTpTSjs-qiBh30aMgm3_Kb53oIf-VA733BpAogVE5jpz3jujU65WJ1XXSvVm1xr2LslGLLCWTNV5Kd_8J1YUx26axkt1E-vsOdvUAgMFH1VJwtclAXdaxRxk5UtmCWeISB6rx6NtvDt7yohnaarpBJjHWMsWYtpNn6nD87n0syud0"}'; //OR $json = file_get_contents('http://someurl.dev/...'); $obj = json_decode($json); var_dump($obj-> access_token); //OR $arr = json_decode($json, true); var_dump($arr['access_token']); : $json = '{"expires_in":5180976,"access_token":"AQXzQgKTpTSjs-qiBh30aMgm3_Kb53oIf-VA733BpAogVE5jpz3jujU65WJ1XXSvVm1xr2LslGLLCWTNV5Kd_8J1YUx26axkt1E-vsOdvUAgMFH1VJwtclAXdaxRxk5UtmCWeISB6rx6NtvDt7yohnaarpBJjHWMsWYtpNn6nD87n0syud0"}'; //OR $json = file_get_contents('http://someurl.dev/...'); $obj = json_decode($json); var_dump($obj-> access_token); //OR $arr = json_decode($json, true); var_dump($arr['access_token']); qiBh30aMgm3_Kb53oIf-VA733BpAogVE5jpz3jujU65WJ1XXSvVm1xr2LslGLLCWTNV5Kd_8J1YUx26axkt1E-vsOdvUAgMFH1VJwtclAXdaxRxk5UtmCWeISB6rx6NtvDt7yohnaarpBJjHWMsWYtpNn6nD87n0syud0"}'; $json = '{"expires_in":5180976,"access_token":"AQXzQgKTpTSjs-qiBh30aMgm3_Kb53oIf-VA733BpAogVE5jpz3jujU65WJ1XXSvVm1xr2LslGLLCWTNV5Kd_8J1YUx26axkt1E-vsOdvUAgMFH1VJwtclAXdaxRxk5UtmCWeISB6rx6NtvDt7yohnaarpBJjHWMsWYtpNn6nD87n0syud0"}'; //OR $json = file_get_contents('http://someurl.dev/...'); $obj = json_decode($json); var_dump($obj-> access_token); //OR $arr = json_decode($json, true); var_dump($arr['access_token']); 
+6
Mar 25 '13 at 14:37
source share
 $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_URL, 'url_here'); $result = curl_exec($ch); curl_close($ch); $obj = json_decode($result); echo $obj->access_token; 
+3
Jan 11 '16 at 7:31
source share

file_get_contents() does not extract data from url, then I tried curl and worked fine.

+2
Apr 28 '17 at 5:27
source share

When you use curl sometimes produces 403 (access is denied), deciding to add this line to emulate the browser.

 curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)'); 

Hope this helps someone.

0
Jul 6 '18 at 9:46
source share

Our solution, adding some checks to the answer, so we are sure that we have a properly formed json object in the $ json variable

 $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_URL, $url); $result = curl_exec($ch); curl_close($ch); if (! $result) { return false; } $json = json_decode(utf8_encode($result)); if (empty($json) || json_last_error() !== JSON_ERROR_NONE) { return false; } 
0
Dec 18 '18 at 17:28
source share

my solution only works for the following cases: if you mistakenly accept a multidimensional array into a single

 $json = file_get_contents('url_json'); //get the json $objhigher=json_decode($json); //converts to an object $objlower = $objhigher[0]; // if the json response its multidimensional this lowers it echo "<pre>"; //box for code print_r($objlower); //prints the object with all key and values echo $objlower->access_token; //prints the variable 

I know that the answer has already been answered, but for those who came here in search of something, I hope this can help you

0
Apr 01 '19 at 20:02
source share



All Articles