Output from New York Time API to JSON file [duplicate] - php

This question already has answers here:
How to write into a file in PHP?
(9 answers)
Closed 5 years ago.
I am a complete noob when working with APIs and PHP.
I want to have the following code dump results into a file called books.json
and I have no idea how to do it. Any help is greatly appreciated!
// Built by LucyBot. www.lucybot.com
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$query = array(
"api-key" => "MY APRI KEY"
);
curl_setopt($curl, CURLOPT_URL,
"https://api.nytimes.com/svc/books/v3/lists/best-sellers/history.json" . "?" . http_build_query($query)
);
$result = json_decode(curl_exec($curl));
echo json_encode($result);

Have a look at file_put_contents:
file_put_contents($path_to_file, $result);

Related

php access array from POST response [duplicate]

This question already has answers here:
Get JSON object from URL
(11 answers)
Closed 11 months ago.
i use a php post to get the number of users in the telegram group
<?php
$post = array(
'chat_id'=>$chat_id);
$ch = curl_init("https://api.telegram.org/bot$tokken/getChatMembersCount?");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_ENCODING,"");
header('Content-Type: text/html');
$posres = curl_exec($ch);
$data1 = json_encode($posres);
echo $data1;
?>
the response are array :
{"ok":true,"result":3}
how can i access the result value i tried echo $data1[19]; this only give the index position value
thank you
The response is JSON, so you need to use json_decode() to convert it to a PHP array or object.
$data1 = json_decode($posres);
echo $data1->result;
Just in case Your response is array of object
echo $data1['19']->result; this should give 3 according to your question

How to echo json data in WordPress plugin using php [duplicate]

This question already has answers here:
Get JSON object from URL
(11 answers)
Closed 2 years ago.
I am creating a Courier tracking plugin and get data using their API. The output they are returned is in JSON format. Here is the courier API
$curl_handle = curl_init();
// For Direct Link Access use below commented link
//curl_setopt($curl_handle, CURLOPT_URL, 'http://new.leopardscod.com/webservice/trackBookedPacket/?api_key=XXXX&api_password=XXXX&track_numbers=XXXXXXXX'); // For Get Mother/Direct Link
curl_setopt($curl_handle, CURLOPT_URL, 'http://new.leopardscod.com/webservice/trackBookedPacket/format/json/'); // Write here Test or Production Link
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, array(
'api_key' => 'your_api_key'
'api_password' => 'your_api_password'
'track_numbers' => 'string' // E.g. 'XXYYYYYYYY' OR 'XXYYYYYYYY,XXYYYYYYYY,XXYYYYYY' 10 Digits each number
));
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
How to echo the values get from this curl command?
Regards
Please use json_decode function after $buffer = curl_exec($curl_handle);
$buffer = curl_exec($curl_handle);
$json = json_decode($buffer, true);
print_r($json);
You can get PHP object array

Unable to execute one PHP file from other using CURL [duplicate]

This question already has answers here:
Receive JSON POST with PHP
(12 answers)
How can I get useful error messages in PHP?
(41 answers)
Closed 3 years ago.
I need to execute some code present inside one PHP file and this file will invoked from other PHP file using CURL but simply as per my code its not happening like that.
I am explaining my code below.
test.php:
<?php
include('db.php');
$adminUrl1='http://example.com/api/product/test1.php';
$data1 = array("user_id" => 54, "org_ord_id" => 82);
$data_string1 = json_encode($data1);
$ch1 = curl_init($adminUrl1);
curl_setopt($ch1, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch1, CURLOPT_POSTFIELDS, $data_string1);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch1, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string1))
);
$res = curl_exec($ch1);
$array = json_decode($res, 1);
echo($array);exit;
?>
Here I am trying to execute the test1.php file which is present inside api/product/ directory.
test1.php:
include('db.php');
$user_id=$_POST['user_id'];
$order_id=$_POST['org_ord_id'];
echo $user_id.'::::'.$order_id;
Here I need to print those post param value those are passed from test.php but as per my current code its not printing anything.
Remove this 'Content-Type: application/json', because you need to get values like application/x-www-form-urlencoded for $_POST[...].
And put application/json to CURLOPT_HEADERFUNCTION for response.
CURL_OPT DOC

Running shell commands inside curl call in php [duplicate]

This question already has answers here:
How can I debug exec() problems?
(4 answers)
sudo in php exec()
(8 answers)
How can I get useful error messages in PHP?
(41 answers)
PHP - Debugging Curl
(8 answers)
Closed 3 years ago.
I am using curl as like below.
http://99.88.77.121/sample/curl.php
$ch = curl_init();
$ip = "Some IP address";
curl_setopt($ch, CURLOPT_URL, 'http://99.88.77.121/test/index.php');
curl_setopt($ch, CURLOPT_POST, 1);
$payload = json_encode(
array(
'username' => 'abc',
'password' => '1234'
)
);
curl_setopt($ch, CURLOPT_POSTFIELDS,$payload);
curl_setopt( $ch, CURLOPT_HTTPHEADER, array("REMOTE_ADDR: $ip", "HTTP_X_FORWARDED_FOR: $ip"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec ($ch);
The above code sends the data to the Url.
I have code like below in my http://99.88.77.121/test/index.php
<?php
$fp = fopen('php://input', 'r');
$rawData = stream_get_contents($fp);
$data = json_decode($rawData);
$username = $data->username;
$password = $data->password;
$projectName = "testFolder";
chdir('../');
//chdir($projectName);
$command = "sudo chmod -R 777 ".$projectName;
shell_exec($command);
?>
I am trying give full permission for testFolder using above code,
is chdir() and shel_exec() will work here? In my case its not working
If i run the index.php in browser, its working fine. but not from the curl call.
Can anyone look into it and update me your thoughts please.

Array navigation in PHP [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
I am trying to get an api(steam api) response as a variable.
{"response":{"players":[{"steamid":"XXXXXXXXXXXXXXXXXXXX","communityvisibilitystate":3,"profilestate":1,"personaname":"The value I want to get","lastlogoff":XXXXXXXXXX,"profileurl":"XXX","avatar":"X","avatarmedium":"X","avatarfull":"X","personastate":1,"realname":"X","primaryclanid":"X","timecreated":XXXXXXXXXX,"personastateflags":0}]}}
Formatted: https://pastebin.com/8QtLzwVX
Currently I am using the following code to get the array:
$url="http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&steamids=" . $sid;
// Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// 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);
I don't know how to get the personaname value from the JSON I get in $result and put it in a variable like $personaname.
try with this one:
<?php
$json_array = json_decode($result, true);
echo $json_array['response']['players'][0]['personaname'];

Categories