php doesn't get the json values i send from curl - php

curl -i -X POST -d 'json={"Amount":1000,"idPlayers":1}' http://www.myurl.com/updateamount.php
My php code:
<?php
$json = urldecode($_GET['jsonSendData']);
$json = str_replace("\\", "", $json);
$data = json_decode($json, true);
// if i hard code it here it works fine, but need from a post from curl
//$json = '{"Amount":1000,"idPlayers":1}';
$data = json_decode($json, true);
echo "json = ", $json;
$amount = mysql_real_escape_string($data['Amount']);
$id = mysql_real_escape_string($data['idPlayers']);
echo "Amount = ",$amount;
return;

Try to change to read post data and using the correct name:
$json = urldecode($_POST['json']);

Related

JSON to PHP using file_get_contents don't working

My api json
{"success":true,"result":{"id":"1017fw436","title":"Leading with NLP","author":"David","url":"abc.com","image":"/abc.img","info":{"pages":"258 Pages","year":"2001","size":"2.74 MB","downloads":"151,185 Downloads","language":"English"},"url_url":"https://drive.google.com/file/d/1GDA9N9SQLHdXBKih8c_GEwcUL"}}
I used
<?php $json_url = "my link api";
$json = file_get_contents($json_url);
$data = json_decode($json, TRUE);
echo $data['id'];
?>
But it doesn't work
Please check Link
try this line of code:
file_get_contents("http://example.com", false, ['ignore_errors' => true]);
var_dump($http_response_header);

Php json decode array

i have a json string. But i can not access values.
$json_string : '{"05526":[{"name":"rapertuar","surname":"xyz","notes":[{"mat1":"59","eng2":"60"},{"mat2":"59","eng2":"60"}]}]}';
$content = file_get_contents($json_string);
$json_a = json_decode($content, true);
echo $json_a['05526']['0']['name'];
echo $json_a['05526']['0']['name']['notes']['0']['mat1'];
How can i fix this code? Thank you
$json_string : '{"05526":[{"name":"rapertuar","surname":"xyz","notes":[{"mat1":"59","eng2":"60"},{"mat2":"59","eng2":"60"}]}]}';
// you don't need this line
//$content = file_get_contents($json_string);
$json_a = json_decode($json_string, true);
echo $json_a['05526']['0']['name'];
echo $json_a['05526']['0']['name']['notes']['0']['mat1'];
There's no need to use file_get_contents if you're storing the JSON in a string and then decoding it. Follow the below approach:
$json_string = '{"05526":[{"name":"rapertuar","surname":"xyz","notes":[{"mat1":"59","eng2":"60"},{"mat2":"59","eng2":"60"}]}]}';
$json_a = json_decode($json_string, true);
echo $json_a['05526']['0']['name']; // rapertuar
echo $json_a['05526']['0']['notes']['0']['mat1']; // 59

php save value in cURL response

I made a cURL to Salesforce and got a responce like:
{"access_token":"XXXXXXXXX","instance_url":"https://na25.salesforce.com","id":"XXXXXXXXX","token_type":"Bearer","issued_at":"1458059021745","signature":"XXXXXXXX="}
I am trying to save the "access_token" like so but not working:
$result = curl_exec($ch2);
$session_id = $result['access_token'];
I have also tried:
$result = curl_exec($ch2);
$parsed = array();
parse_str($result, $parsed);
$session_id = $parsed['access_token'];
the API is returning a json string. you need to run it through json_decode first.
$result = curl_exec($ch2);
$resultArray = json_decode($result, true);
$session_id = $resultArray['access_token'];
you can try with below code
$result = curl_exec($ch2);
$result_aray = json_decode($result,true);
$session_id = $result_aray ['access_token'];
Let me know still anything.

Can't print a simple JSON?

I know it's basic, but I couldn't get what's wrong because it's not my area.
I am sending some request to a server and print the response like this:
$rest = curl_init();
curl_setopt($rest,CURLOPT_URL,$url);
curl_setopt($rest,CURLOPT_GET,1);
curl_setopt($rest,CURLOPT_HTTPHEADER,$headers);
curl_setopt($rest,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($rest,CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($rest);
$json = json_decode($response, true);
echo $response;
Where I get this:
{"results":[{"Devices":["52401E7E-C5D7","AE80C0F8-999E","764BFD92-9753","78A23379-2C14","EEA03545-5EB9","E18DDFEC-C3C9"],"UserID":"433011AC-228A-4931-8700-4D050FA18FC1","createdAt":"2015-11-04T15:06:33.564Z","objectId":"3os7BGxRoG","updatedAt":"2015-11-04T17:08:57.635Z"}]}
Then, I am trying to print the JSON or its fields, but I then get nothing:
echo $json;
echo $json['UserID'];
echo $json['Devices'];
To clarify the comments a bit:
$str = '{"results":[{"Devices":["52401E7E-C5D7","AE80C0F8-999E","764BFD92-9753","78A23379-2C14","EEA03545-5EB9","E18DDFEC-C3C9"],"UserID":"433011AC-228A-4931-8700-4D050FA18FC1","createdAt":"2015-11-04T15:06:33.564Z","objectId":"3os7BGxRoG","updatedAt":"2015-11-04T17:08:57.635Z"}]}';
$json = json_decode($str, true); // to an associative array
// to echo the UserID
echo "userID : " . $json['results'][0]['UserID'];
// output: userID : 433011AC-228A-4931-8700-4D050FA18FC1
// to get the structure of the json array in general use print_r() as AbraCadaver pointed out
print_r($json);
In your attempt you were missing the results[0] part.

get data from JSON file (with cURL)

I would like to trnsform some JSON data to a php array, this is my code:
<?php
$obj1=json_decode('http://ddragon.leagueoflegends.com/cdn/3.15.5/data/en_US/champion/Aatrox.json', true);
$championname = $obj1[data][aatrox][name];
echo $championname;
?>
The problem is that i don't know how to get the data from http://ddragon.leagueoflegends.com/cdn/3.15.5/data/en_US/champion/Aatrox.json
How can i make this code work?
Try with file_get_contents() like this:
$json = file_get_contents('http://ddragon.leagueoflegends.com/cdn/3.15.5/data/en_US/champion/Aatrox.json');
$obj1 = json_decode($json , true);
$championname = $obj1['data']['aatrox']['name'];
echo $championname;
Use file_get_contents to download a file.
$json = file_get_contents('http://ddragon.leagueoflegends.com/cdn/3.15.5/data/en_US/champion/Aatrox.json');
$obj1 = json_decode($json, true);

Categories