How to use cURL to get jSON data and decode the data?
I think this one will answer your question :P
$url="https://.../api.php?action=getThreads&hash=123fajwersa&node_id=4&order_by=post_date&order=desc&limit=1&grab_content&content_limit=1";
Using cURL
// Initiate curl
$ch = curl_init();
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);
// Will dump a beauty json :3
var_dump(json_decode($result, true));
Using file_get_contents
$result = file_get_contents($url);
// Will dump a beauty json :3
var_dump(json_decode($result, true));
Accessing
$array["threads"][13/* thread id */]["title"/* thread key */]
And
$array["threads"][13/* thread id */]["content"/* thread key */]["content"][23/* post id */]["message" /* content key */];
GET JSON data using CURL
I added the curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json'));
line to make sure that we accept data in JSON format.
$datasearch
is an array that holds one element. That one element is an array of all properties you want. So, to access those properties, we need to access the element first and then the properties. You do this by $datasearch[0]["id"];
. To avoid constantly typing $datasearch[0]
you can just reset the $datasearch
value to it's first element ($datasearch = $datasearch[0];
). Afterwards, you can use it like $datasearch["id"]
.
<?php
$url = 'https://api.flightplandatabase.com/search/plans?fromICAO=EHAM&toName=Kennedy&limit=1';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
$datasearch = json_decode($data, true);
if(!empty($datasearch)) {
$datasearch = $datasearch[0];
echo $datasearch["id"];
} else {
echo "Data not fetched.";
}
curl_close($curl);
?>
How to get json from cURL with PHP
You forgot to use the CURLOPT_RETURNTRANSFER
option. So curl_exec()
printed the response instead of returning it into $result
, and $result
just contains the value TRUE
that was returned by curl_exec
to indicate that it was successful. Add:
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
How to decode json file produced from curl in php
You need to reference $var
before using $data->account_number
:
<?php
$response= '{"status":true,"message":"Account number resolved","data":{"account_number":"xxxxxxx","account_name":"ONY xxxx xxxx","bank_id":21}}';
$var = json_decode($response);
echo($var->data->account_number); // prints xxxxxxx
Related Topics
How to Write SQL For a Table That Shares the Same Name as a Protected Keyword in MySQL
How to Get the File Extension in PHP
Laravel Eloquent Query Using Where With or and Or
Show Ajax Upload Status on Progress Element
PHP With MySQL 8.0+ Error: the Server Requested Authentication Method Unknown to the Client
What's the Difference Between Isset() and Array_Key_Exists()
Are Arrays in PHP Copied as Value or as Reference to New Variables, and When Passed to Functions
Correctly Determine If Date String Is a Valid Date in That Format
Replacing Accented Characters PHP
Strict Standards: MySQLi_Next_Result() Error With MySQLi_Multi_Query
Does PHP Allow Named Parameters So That Optional Arguments Can Be Omitted from Function Calls
Having Issue With Matching Rows in the Database Using Pdo
Print Numeric Values to Two Decimal Places
Ajax and PHP to Enter Multiple Forms Input to Database
Examples of SQL Injections Through Addslashes()