decoding json with php - get url from Facebook profile pic - php

I need to decode this json value using PHP. I need the value 'url'.
Here is what I have but does not work.
$json = file_get_contents('http://graph.facebook.com/{fb-ID}/picture?type=large&redirect=false');
$decoded = json_decode($json,true);
$url = $decoded[???]; // NEED THIS VAR.

Since $decoded is a PHP array you can access it like any other array:
$url = $decoded['data']['url'];

$decoded=(object)json_decode( $json['data'], true );
echo $decoded->url

This worked:
$json = file_get_contents('http://graph.facebook.com/{fb-ID}/picture?type=large&redirect=false');
$decoded = json_decode($json,true);
$url = $decoded['data']['url'];

Related

PHP input to JSON file and JSON file to PHP output

Can anyone please shows me how to get the input of PHP to JSON file (.json) and read data from JSON file and display in PHP (Echo).
for example:
$myObj->name = "John";
$myObj->age= 20;
to result.json
{"name":"John","Age":20}
and retrieve from result.json and display data in PHP as
name=John
Age=20
To convert the object to json use this:
$json = json_encode($myObj);
See the json_encode docs.
To return it back to the format you want try this...
$obj = json_decode($json);
$name = $obj->name; // John
$age = $obj->age; // 20
See the json_decode docs.
To iterate over keys and values do something like this:
foreach($obj as $key=>$value)
{
echo $key . " = " . $value . "\n";
}
json_encode() is used for encoding PHP data into a JSON format and json_decode() is used to decode JSON into a PHP data
json_encode documentation:
http://php.net/manual/en/function.json-encode.php
json_decode documentation
http://php.net/manual/en/function.json-decode.php
if you are having an php array you can convert it into json.
$json_string = json_encode($array);
and write this into a json file.
$fp = fopen('results.json', 'w');
fwrite($fp, json_string);
fclose($fp);
now convert your json string which is in results.json to array.
$str = file_get_contents('./results.json');
$array = json_decode($str, true); // decode the JSON into an associative array

Decode JSON to PHP

I have a .txt file called 'test.txt' that is a JSON array like this:
[{"email":"chrono#gmail.com","createdate":"2016-03-23","source":"email"}]
I'm trying to use PHP to decode this JSON array so I can send my information over to my e-mail database for capture. I've created a PHP file with this code:
<?php
$url = 'http://www.test.com/sweeps/test.txt';
$content = file_get_contents($url);
$json = json_decode($content,true);
echo $json;
?>
For some reason, it's not echoing the decoded JSON when I visit my php page. Is there a reason for this and can anyone shed some light? Thanks!
You use echo to print scalar variables like
$x = 'Fred';
echo $x;
To print an array or object you use print_r() or var_dump()
$array = [1,2,3,4];
print_r($array);
As json_decode() takes a JSON string and converts it to a PHP array or object use print_r() for example.
Also if the json_decode() fails for any reason there is a function provided to print the error message.
<?php
$url = 'http://www.test.com/sweeps/test.txt';
$content = file_get_contents($url);
$json = json_decode($content,true);
if ( json_last_error() !== JSON_ERROR_NONE ) {
echo json_last_error_msg();
exit;
}
You'll need to split that json string into two separate json strings (judging by the pastebin you've provided). Look for "][", break there, and try with any of the parts you end up with:
$tmp = explode('][', $json_string);
if (!count($tmp)) {
$json = json_decode($json_string);
var_dump($json);
} else {
foreach ($tmp as $json_part) {
$json = json_decode('['.rtrim(ltrim($json_string, '['), ']').']');
var_dump($json);
}
}

PHP won't update JSON file

This is what my JSON file currently looks like:
{"coins":"0","uses":"0"}
I'm trying to update it's values by using the following PHP:
$jsonString = file_get_contents('/path/money_maker.json');
$data = json_decode($jsonString);
$data["coins"] = $data["coins"] + $coins;
$data["uses"] = $data["uses"] + 1;
$newJsonString = json_encode($data);
file_put_contents('/path/money_maker.json', $newJsonString);
However, it's not working. How come? I've tried doing some research, but nothing has helped me thus far.
the content of the file is json object.you cant use it like array.try this
$jsonString = file_get_contents('money-maker.json');
$data = json_decode($jsonString);
$data->coins = $data->coins + $coins;
$data->uses = $data->uses + 1;
$newJsonString = json_encode($data);
file_put_contents('money-maker.json', $newJsonString);
or you can convert it first then use it.
The answer by sgt is correct, but you can also have json_decode create arrays instead of objects as seen in the PHP manual (http://be2.php.net/manual/en/function.json-decode.php) by using the assoc parameter: $data = json_decode($jsonString, true);

Using json variable

How do I use json variable in php.
$filePath = '/home/user/public_html/uploads/samplefile.txt'
echo '{"status":"success", "fileName" : "'.$filePath.'"}';
Say I would like to use it this way.
$mail->addattachment($fileName);
Thanks.
You need to first deserialize the json data into a PHP array.
$json_string = "...."; //this is your json string
$json_data = json_decode($json_string); // decode into an array
$mail->addattachment($json_data['filename']);
While #xbonez answer is correct, you could use it the way you specified, but it's not recommended:
$json = <<<JSON
{"status":"success", "fileName" : "/home/user/public_html/uploads/samplefile.txt"}
JSON;
extract(json_decode($json, TRUE));
echo $fileName;

Retrieving App Store application icon URL with PHP

I found that App Store API support additional information about application.
$keyword = $this->input->post('keyword');
$appstore_api = 'http://itunes.apple.com/search?country=US&entity=software&term='.$keyword;
$data = file_get_contents($appstore_api);
echo $data;
here is the PHP code that I wrote.
I get this result.
{ "resultCount":50, "results": [ {"kind":"software", "features":[], "supportedDevices":["all"], "isGameCenterEnabled":false, "artistViewUrl":"http://itunes.apple.com/us/artist/burbn-inc./id389801255?uo=4", "artworkUrl60":"http://a2.mzstatic.com/us/r1000/105/Purple/v4/f3/0e/e2/f30ee271-c564-ec21-02d6-d020bd2ff38b/Icon.png", "screenshotUrls":["http://a3.mzstatic.com/us/r1000/102/Purple/v4/2d/25/d3/2d25d348-74e9-8365-208c-45e64af73ed6/mzl.xnvocmpt.png", "http://a1.mzstatic.com/us/r1000/077/Purple/v4/04/18/b3/0418b375-c0c1-18c1-1aef-07555c99af46/mzl.pkthtqtv.png", "http://a2.mzstatic.com/us/r1000/069/Purple/v4/31/08/65/31086528-2f37-bca0-71f3-c3095e698f35/mza_8774562250786021670.png", "http://a3.mzstatic.com/us/r1000/091/Purple/v4/95/a2/65/95a265bb-b9f0-8732-9fe4-823ffbc9aba0/mza_5807950548098772841.png"], "ipadScreenshotUrls":[], "artworkUrl512":"http://a4.mzstatic.com/us/r1000/089/Purple/v4/44/76/7f/44767fb5-4cb2-25bf-5361-25138b8c2aeb/mzl.ntalagmr.png",
The question is that how can I extract the variable named "artworkUrl512" ?
I tried like this but failed.
$image_url = $data->results->artworkUrl512;
$image_url2 = $data['results']['artworkUrl512'];
Can you help me how to extract icon image url?
$data is json encoded. Use json_decode():
http://php.net/manual/en/function.json-decode.php
$data = json_decode($json, true);
echo $data['results'][0]['artworkUrl512'];

Categories