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
Related
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
This question already has answers here:
Decode gzipped web page retrieved via cURL in PHP
(2 answers)
Closed 2 years ago.
I'm struggling to retrieve data from an API using PHP.
My PHP code:
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://api.coronavirus.data.gov.uk/v1/data?filters=areaName=United%2520Kingdom;areaType=overview&structure={%22areaName%22:%22areaName%22,%22date%22:%22date%22,%22newCasesByPublishDate%22:%22newCasesByPublishDate%22,%22cumCasesByPublishDate%22:%22cumCasesByPublishDate%22}");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
echo $output;
?>
Sadly this just outputs a random list of characters like this:
��9|_��M�\�E�JЀ���"�H*p�Kx���,:VK�`>���m��a˒��܆�V^��gt�d�e]���������������f�ӷ�7�?_�\=�^Z)�b�j��߽�����������{����������tu�������7����+/J���~���������>������o>|�y�v�����s��Et{���i��L;�+>�Pq�F7��ĵQ�u�q9s��e��*͈f hkh�T����Ӵn�ճ�4�Dk�4��E��q��J����XUi�4-r��j�9UZB���s)�J9��a&�z�� �$�Ͳ��n &��4����� �a* ��UP�:Gz�4В:�Z�Pc�#KZsښJU'���mĔ3�#Kj�4KSU�#Kꜧ7�ڇ��ZR����ZUyݨ2I����`�֭�ӴQ�u-Y{JZX�i�%5*dwZ�;����4�Ű�FI�%A��3�������E�u��{#�e#TZm�uWidsVm� j�˱�`�V$��hrz�\���4�������Fǜ��%˩e�xQ��J-F�*�к�m��.��Q��y�m��2�4�+ݚJ��Ts�R�(!Ge�t���\��F�e���)� �kȤ�ZN4Tf��-���ςhَ&*���T�ɕ����ZA �R."��0�Y�#��Ԓ-��ֿ0�l#?թL�Uj��l,$�+md��t0�ȒLv�VM����f�$Abm�L#%)�hU�d�Sf�ݬ�vL�FǼ�k�VE�ܔ�%Y�/z�n��c�=UE&35��yd $3`�ʲ>jf��֬t�E��TvY����(#"]�9%�u��4�'E���U�utQcК�}�T��*�浨0�J%T�Z� ��AkVr�kFj����iL ��0�+S��Y�H9�>u���S�O�S�I0��|�95-���g���Q +1d(HHԮ�Y�M=���IM�i8���T�PA��Ta�Ԇ��O���$*%�2����o�aU<��h��RR�2�.�#��cf�aT�8��JkF �!�O#y&����h$˔ #���yς�&è� ��|DO�;Z��$X3U��2yX�b��e��0��Ȩ�4���ZT<�iCK�D�t�i��D���m���Pa� k!����D3&^��������e֔i�2:��s�4�j��*��]z�3�G�tT�g<.�&7����V����^yY٦N$9�%���TF�a?ÿ�T��Q���e��6�w2�~L��m� �C�E��#wԏV^�7l5�uk�DPixZ\˅Ny��6�'���"�d�ͭ�5lj��}�J��88��Z�J���S�Ir5V��g�M��#[cx�5�}M�:��k{Suo� \7˔i�%��[�=BUe�U�p���%U��V]#[my�Z��.�V�*���z�4����݇�]�R^&�V�� o� �ɜ&^stS^(&���ʁv�W�p}l�\T)��]�#;�f *��S�U�YD�������\%��ft��*�(���ZO�z���#G�͐6L9*O�W�-[Qu�S������L��n��݊��B�sJ���M�Q����>�+ �F�)9�|��f7?�D�ó�%��Ҁ�������al6m�c#GUDŽ(�pyl䨖Z��cVS�q�O�t�PV��1I�j��%Ŭ��F���(�*�)�7�h%�*�U��\����VA�ڠ�~Fл��{k����Rh�b{���i�nv��M� |Ƙhf-TZ����{���&6�z��uW��UC��&�7kC�����D�n��T3�i�.�'��6��w1���,M��B{7e�r#M��81��м�Tc���]TiDR�G�Y K5���`���d��9�G�%����=�r(Й �W���Zx���q�#�^�P���b Oi�S1� &���sB#��sF#G�oB�M(e U�_�26��M(� %6��M(�99�Y�?T�mB)爲J�']N3*��W���7��93&S��}W)mJ݄�P��O1l=����U]�>]_� �[��V��(��V���n�#�h+e�����R�Jl+e�����R�Jl+e�����R���2����l�e+e([)C�J��������7W��ǫ����w������_��}u�������������ot�����������������˻����W��?<�?}��t�|ֿ~����_��_/��}����_��>~۩ώ?����S�=��<}w��������5-��_��_fk��������ç�����m��3W��ߦ���x����nx
Putting the url directly into a browser works fine, giving a nice JSON, see see here.
I'm really scratching my head to know where to go from here to get this to parse properly in PHP.
Can anyone help, I'm completely out of my depth here.
Excellent....thank you soooo much !!!!!, you have just solved a problem I have been trying to sort out on-and-off for some time.
The retrieved data is encoded, to enable decoding, add this line.
curl_setopt($curl, CURLOPT_ENCODING, '');
This enables the decoding of the retrieved data, by using and empty string as parameter, it enables all encoding types.
With the line added, your code will look like this.
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://api.coronavirus.data.gov.uk/v1/data?filters=areaName=United%2520Kingdom;areaType=overview&structure={%22areaName%22:%22areaName%22,%22date%22:%22date%22,%22newCasesByPublishDate%22:%22newCasesByPublishDate%22,%22cumCasesByPublishDate%22:%22cumCasesByPublishDate%22}");
curl_setopt($curl, CURLOPT_ENCODING, ''); // <--- ADDED LINE!
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
echo $output;
you need just encoding, this is working code:
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://api.coronavirus.data.gov.uk/v1/data?
filters=areaName=United%2520Kingdom;areaType=overview&structure{%22areaName%22:%22areaName%22,%22date%22:%22date%22,%22newCasesByPublishDate%22:%22newCasesByPublishDate%22,%22cumCasesByPublishDate%22:%22cumCasesByPublishDate%22}");
curl_setopt($curl, CURLOPT_ENCODING, '');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
echo $output;
?>
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'];
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);
This question already has answers here:
Reading JSON POST using PHP
(3 answers)
Closed 7 years ago.
I am trying to develop small API code in php, which will send POST request in json format using PHP curl (from client side). And at receiving side(at server side) I want to parse the request and process it. but at receiving side it gives me empty array in post field.
Below is my code
Client side:
<?php
$data = array("name" => "Hagrid", "age" => "36");
$data_string = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, "xdata=".$data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
echo $result = curl_exec($ch);
?>
At server side:
<?php
print_r($_POST);
?>
Blockquote
Array ( )
Blockquote
It always gives me empty POST array at server side.
Am I doing things wrong or is there another way to parse the request?
Please guide me.
You can use php://input read-only stream to access JSON post data instead of $_POST like this
<?php
$json = file_get_contents('php://input');
?>
It will give you POST data as is. You will be able to decode it using json_decode() later.
Example code -
<?php
$json = json_decode(file_get_contents('php://input'));
?>
You are setting wrong content length with the CURLOPT_HTTPHEADER header. Your overall post string is strlen("xdata=".$data_string) whereas you are using strlen($data_string).
Also, you are using application/x-www-form-urlencoded for posting, but mentioning the content type forcefully as application/json
So remove the following block from your code and you should be fine.
// Remove it
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);