How to get a object from JSON to PHP [duplicate] - php

This question already has answers here:
How to access object properties with names like integers or invalid property names?
(7 answers)
Closed 2 years ago.
I need to take a img link (https://upload.wikimedia.org/wikipedia/en/5/51/Minecraft_cover.png) from this api https://en.wikipedia.org/w/api.php?action=query&prop=pageimages&format=json&piprop=original&titles=Minecraft&pilicense=any. How to do it?
I wrote code like this, but I can print :
$img_url = "https://en.wikipedia.org/w/api.php?action=query&prop=pageimages&format=json&piprop=original&titles=Minecraft&pilicense=any";
$img_url = str_replace(" ", "%20", $img_url);
$img = json_decode(file_get_contents($img_url));
print_r ($img);
But how to print only img source?

The simplest way would be to use the following.
echo $img->query->pages->{'27815578'}->original->source;
Where 27815578 is the Page ID

Related

How to properly echo a php variable in a string for url href? [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 4 years ago.
I'm trying to change the url of a link on my wordpress site thru a PHP function. So far echoing out the string url is giving me trouble.
I have the following in my PHP file.
$siteurl = site_url(); // gives me: "http://localhost/testsite"
$teststring = "/catalog";
$entireurl = $siteurl.$teststring; //gives me "http://localhost/testsite/catalog"
function change_site($buffer) {
$in = array('<a href="http://localhost/testsite/type/combined/?case=main" title="Catalog">');
$out = array('<a href="'.$entireurl.'" title="Catalog">');
return (str_replace($in, $out, $buffer));
}
ob_start("change_site");
Currently, the link takes on the link of another href link in the same div. (bizzare)
Try this instead. You might be missing the . concatenator
$out = array('<a href="'.$entireurl.'" title="Catalog">');

PHP get JSON content from url [duplicate]

This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 5 years ago.
I'm using picasaweb(google) API to get the profile picture by email address..
I get the output from the URL:
http://picasaweb.google.com/data/entry/api/user/adircohen#gmail.com?alt=json
How can I get the variable with the content:
https://lh3.googleusercontent.com/-9GSeL43L-A4/AAAAAAAAAAI/AAAAAAAAAAA/x8Uy6PTaS1o/s64-c/112285456748585606724.jpg
I've tried this, but it's not working:
$json = file_get_contents('http://picasaweb.google.com/data/entry/api/user/adircohen#gmail.com?alt=json');
$obj = json_decode($json);
echo $obj->gphoto->$thumbnail;
Getting content from URL using this function file_get_contents();
$page = file_get_contents("http://picasaweb.google.com/data/entry/api/user/adircohen#gmail.com?alt=json");
$ap = json_decode($page); // decode the json record
$ap = (array)$ap->entry; // convert object to array for further process
$ap = (array)$ap['gphoto$thumbnail']; // convert object to array for
getting exact output as image url
echo $ap['$t']; // display the output

PHP: get parent node where child = 'value' [duplicate]

This question already has answers here:
SimpleXML: Selecting Elements Which Have A Certain Attribute Value
(2 answers)
Closed 7 years ago.
I have this xml file:
<friends>
<friend>
<name>xxx</name>
<pays>France</pays>
</friend>
<friend>
<name>yyy</name>
<country>France</country>
</friend>
<friend>
<name>zzz</name>
<country>USA</country>
</friend>
</friends>
To get my data, I am using this php code:
$xml = simplexml_load_file('friends.xml');
$friendsXML = $xml->friend;
Which works fine, but returns all of the friends.
Now I want to retrieve only friends who are from France:
country = 'france'.
Can anyone help me doing that?
I'd use XPath for things like this. Try:
$res = $xml->xpath('friend[country = "france"]');
echo $res[0];

Get full website URL? [duplicate]

This question already has answers here:
Get the full URL in PHP
(27 answers)
Closed 8 years ago.
I have a function which looks like this:
function fullURL(){
$domain = $_SERVER['SERVER_NAME'];
$path = $_SERVER['PHP_SELF'];
$raw_url = $domain.$path;
$url = substr($raw_url, 0, -4);
echo $url;
}
This generates an URL which looks like 127.0.0.1/aura/profile
But, what if I would use this on a page which uses a $_GET-tag?
Then, my URL won't pick it up.
For example I have news?id=1, when using the function above it just becomes 127.0.0.1/aura/news.
What do I need to add to the function to make it output 127.0.0.1/aura/news?id=(id)?
What about this?
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
Note that you could also do:
$actual_link = "http://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];
Original answer: https://stackoverflow.com/a/6768831/3150271

PHP json decode with number tag [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to access object properties with names like integers?
I tried to decode the json result from youtube data api by the following code:
$url="http://gdata.youtube.com/feeds/api/videos/$id?v=2&alt=jsonc";
echo "$url".'<BR>';
$json = file_get_contents($url,0,null,null);
$json_output = json_decode($json);
$items=$json_output -> data;
$content = "{$items->content->1}";
echo $content.'<BR>';
Everything works fine but the last two lines. Could someone please help?
And Here is the json result:
{"apiVersion":"2.1","data":{"id":"9jDg3Dh28rE","uploaded":"2012-10-04T03:45:49.000Z",
........
"content":{"5":"http://www.youtube.com/v/9jDg3Dh28rE?version=3&f=videos&app=youtube_gdata",
"1":"rtsp://v5.cache8.c.youtube.com/CiILENy73wIaGQmx8nY43OAw9hMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp",
"6":"rtsp://v5.cache4.c.youtube.com/CiILENy73wIaGQmx8nY43OAw9hMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp"},"duration":2403,......}}
You need to wrap the numeric property with {} to access it.
$content = $items->content->{1};
And you also don't need to use double quotes like below:
$thumbnail = "{$items->thumbnail->sqDefault}";
This should just be
$thumbnail = $items->thumbnail->sqDefault;

Categories