Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
After trying for a long period of time I am a bit stuck... I want to echo a value (url) in a json array with PHP and get it into a variable.
PHP:
$coverphotos = $facebook->api('http://graph.facebook.com/' . $album['id'] . '/picture?type=album&redirect=false&access_token=' . $access_token);
JSON:
{
"data": {
"url": "http://photos-c.ak.fbcdn.net/hphotos-ak-ash4/480080_567808343263483_176928672_a.jpg"
}
}
I want to get the actual url into a php variable... how would I go about doing this?
HOLY THAT TOOK LONG ENOUGH BUT I FIGURED IT OUT:
$coverphotos2 = $coverphotos['id'] . '?type=album&redirect=false&access_token=' . $access_token;
$content = file_get_contents($coverphotos2);
$photoarray = json_decode($content, TRUE);
$coverurl = $photoarray['data']['url'];
From the $coverphotos variable, you can store the URL of the picture with:
$picture = $coverphoto['id'];
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
In my PHP app I am retrieving data from a REST API (namely PokeAPI) and want to be able to cache the information I retrieve from it in order to cut down on time and API requests. What would be the best way to go about doing this?
Here is the code I am using the API for:
<?php
$base = "http://pokeapi.co/api/v2/pokemon/";
if (isset($_POST["dexno"])) {
$dexarray = $_POST["dexno"];
foreach( $dexarray as $d ) {
$data = #file_get_contents($base.$d);
if ($data != "") {
$pokemon = json_decode($data);
$img = $pokemon->sprites->front_default;
$imageData = base64_encode(file_get_contents($img));
echo '<img src="data:image/png;base64,'.$imageData.'">'.'<br>';
echo $pokemon->name.'<br>';
} }
}
?>
This simply takes numerical data from an array, puts it into a URL and pulls information from the URL generated. Any help would be much appreciated, as I have only just started learning PHP.
Like this:
$json = ""; //your json string.
$fh = fopen("myCacheFile.jsoN" , "w+");
fwrite($fh , $json);
fclose($fh);
If you want short code:
file_put_contents("myCacheFile.json" , $json);
To Retrieve:
$arr = json_decode(file_get_contents("myCacheFile.json") , true);
You can simply store the data in file on disk, and next time before making the call read from disk.
You can store it in database, and read from there.
You can use redis / memcached to store it in memory.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
hi i want to create a json array in codeigniter of the following format . correct me if i am wrong .
// my json format
$data={"records":[{"name1":[{"id1":"value","subject","subname"},{"id2":"value","subject2","subname"},{"id3":"value","subject3","subname"},{"id4":"value","subject4","subname"}]},
{"name2":[{"id1":"value","subject","subname"},{"id2":"value","subject2","subname"},{"id3":"value","subject3","subname"},{"id4":"value","subject4","subname"}]};
i am trying to create it in php using codeigniter ..... really need your help
Use json_encode() like this.
<?php
$array = array('name'=> 'value', 'name1', $some_var);
$json = json_encode($array);
// or an object
$obj = new stdClass();
$obj->name = 'Hello';
$obj->name1 = 'world';
$json2 = json_encode($obj);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have A Json data But I am unable to get desired response.
$content='{"success":true,"results":[{"id":"21390fb46e92","msisdncountrycode":"DE","msisdn":"+491788735000","statuscode":"HLRSTATUS_UNDELIVERED","hlrerrorcodeid":9,"subscriberstatus":"SUBSCRIBERSTATUS_ABSENT","imsi":null,"mccmnc":"26203","mcc":"262","mnc":"03","msin":null,"servingmsc":null,"servinghlr":null,"originalnetworkname":"E-Plus","originalcountryname":"Germany","originalcountrycode":"DE","originalcountryprefix":"+49","originalnetworkprefix":"178","roamingnetworkname":null,"roamingcountryname":null,"roamingcountrycode":null,"roamingcountryprefix":null,"roamingnetworkprefix":null,"portednetworkname":null,"portedcountryname":null,"portedcountrycode":null,"portedcountryprefix":null,"portednetworkprefix":null,"isvalid":"Yes","isroaming":"No","isported":"No","usercharge":"0.0100","inserttime":"2015-11-24 18:56:42.048693+08","storage":"CURL-TEST","route":"IP1"}]}';
i want to capture some contents like
id , msisdncountrycode , msisdncountrycode , statuscode , hlrerrorcodeid
And Other Values As Well Please Help To Solve This problem .
check out this:
$result = json_decode($content);
var_dump($result->results[0]->id);//will get id
var_dump($result->results[0]->msisdncountrycode);//will get Country code
use this(works with multiple results).
$d = json_decode($content);
foreach($d->results as $row){
echo $row->id; //echoes 21390fb46e92
echo $row->msisdncountrycode; //echoes DE
echo $row->statuscode; //echoes HLRSTATUS_UNDELIVERED
echo $row->hlrerrorcodeid; //echoes 9
}
Echoes are based on current input. Since its in loop, it will print all values.
You can try this. Its working fine for me.
json_decode($content)
Try this:
<?php
$content='{"success":true,"results":[{"id":"21390fb46e92","msisdncountrycode":"DE","msisdn":"+491788735000","statuscode":"HLRSTATUS_UNDELIVERED","hlrerrorcodeid":9,"subscriberstatus":"SUBSCRIBERSTATUS_ABSENT","imsi":null,"mccmnc":"26203","mcc":"262","mnc":"03","msin":null,"servingmsc":null,"servinghlr":null,"originalnetworkname":"E-Plus","originalcountryname":"Germany","originalcountrycode":"DE","originalcountryprefix":"+49","originalnetworkprefix":"178","roamingnetworkname":null,"roamingcountryname":null,"roamingcountrycode":null,"roamingcountryprefix":null,"roamingnetworkprefix":null,"portednetworkname":null,"portedcountryname":null,"portedcountrycode":null,"portedcountryprefix":null,"portednetworkprefix":null,"isvalid":"Yes","isroaming":"No","isported":"No","usercharge":"0.0100","inserttime":"2015-11-24 18:56:42.048693+08","storage":"CURL-TEST","route":"IP1"}]}';
echo '<pre>';
$jsonArr = json_decode($content, TRUE);
$outputArr = $jsonArr['results'][0];
print_r($outputArr);
$id = $outputArr['id'];
$msisdncountrycode = $outputArr['msisdncountrycode'];
$statuscode = $outputArr['statuscode'];
$hlrerrorcodeid = $outputArr['hlrerrorcodeid'];
?>
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm a php beginner. I've got this kind of JSON ?
{
"rows":
[
["/page1.php","568"],
["/","78"],["/page2.php","4"],
["/page2.php","2"],
["/page3.php","1"],
["/search.php","1"]
]
}
to parse json i use
<?php
$json_file = file_get_contents('test.json');
$parsed_json = json_decode($json_file);
?>
But i don't know to parse each page and number. Anyone can help me on this surely basic issue ?
thanks a lot
$parsed_json will be an object, whose rows property is an array. The elements of this array are arrays, whose first element is the page name, the second element is the page number:
foreach ($parsed_json->rows as $page) {
$page_name = $page[0];
$page_number = $page[1];
echo "Page #" . $page_number . " on " . $page_name . "<br>";
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
i want a simple php code that will get data from user e.g( https :// fbcdn-profile-a.akamaihd.net/hprofile-ak-ash3/c0.0.180.180/s160x160/1525356_685076001544625_985694702_a.jpg)
i want just this code from user from above example (1525356_685076001544625_985694702_a.jpg)
when he will put all code into my website then my website will show him his/her large image.
-Note:i just want selective part(1525356_685076001544625_985694702_a.jpg) from user
and show whole result like this - https : // fbcdn-profile-a.akamaihd.net/hprofile-ak-ash3/1525356_685076001544625_985694702_n.jpg
any help will be greatly appreciated...
Try this:
<?php
$input = 'https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash3/c0.0.180.180/s160x160/1525356_685076001544625_985694702_a.jpg';
$code = explode('/', $input);
$code = $code[sizeof($code)-1];
$code = substr($code, 0, -5);
echo 'https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash3/' . $code . 'n.jpg';
Output:
https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash3/1525356_685076001544625_985694702_n.jpg
In addition to #Dave Chen anwser (shorter).
$input = 'https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash3/c0.0.180.180/s160x160/1525356_685076001544625_985694702_a.jpg';
$code = explode('/', $input);
$code = substr(end($code), 0, -4);
// or version < PHP 5.4
$code = substr(end(explode('/', $input)), 0, -4);
echo 'https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash3/' . $code . 'n.jpg';