Using Wikipedia API link to get main image about some world known characters/events.
Example : (Stanislao Mattei)
This would show as following
Now my question
I'd like to parse the xml to get image url to be shown up
here is the code i'm willing to use if it right ~ thanks to ccKep ~
<?PHP
ini_set("user_agent","Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
$url = "http://en.wikipedia.org/w/api.php?action=query&list=allimages&aiprop=url&format=xml&ailimit=1&aifrom=Stanislao Mattei";
$xml = simplexml_load_file($url);
$extracts = $xml->xpath("/api/query/allimages");
var_dump($extracts);
?>
It should gives results as array
how i can get among it the exact url of the image to be shown should be :
http://upload.wikimedia.org/wikipedia/en/a/a1/Stanislaus.jpg
to put it in html code
<img src="http://upload.wikimedia.org/wikipedia/en/a/a1/Stanislaus.jpg">
~ Thanks a lot
Did you try $xml->query->allimages->img->attributes()->url
Your code will look like this:
<?php
ini_set("user_agent","Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
$url = "http://en.wikipedia.org/w/api.php?action=query&list=allimages&aiprop=url&format=xml&ailimit=1&aifrom=Stanislao Mattei";
$xml = simplexml_load_file($url);
$url = $xml->query->allimages->img->attributes()->url;
echo "URL: ".$url . "<br/>";
echo '<img src="'.$url.'">';
?>
Related
I am trying to scrape a website using simplehtmldom but it is giving me an empty result.
Here is my code:
<?php
include('../simple_html_dom.php');
ini_set("user_agent","Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0");
echo $html = file_get_html('https://www.bodybuilding.com/store/opt/whey.html');
?>
I am fetching data from kickstarter campaign, when I view it from my browser it displays me "Euro" symbol but when I fetch html content of the same page using CURL it shows me "dollar" symbol. Why is that so ?
Below is my PHP code (using CURL module) :
<?php
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
$data = curl_exec($ch);
return $data;
?>
I want it to display me correct currency symbols like if the project is in "USD" it should return me "USD" and same with "EUR".
For example below is link to a campaign which has "EUR" currency symbol but in CURL fetched data its changing to "USD" , why so ? , is PHP auto converting that based on my server settings ?
Example link : https://www.kickstarter.com/projects/35540661/new-colors-59-stainless-milanaise-loop-for-apple-w
Your useragent is setting the location to en-us
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
I guess that is the reason the currency is set to $.
I do not know how kickstarter defines which currency to use. Maybe the server the cURL request is comming from is located in the US and kickstarter is using the ipaddress to define the currency.
I use php file_get_contents to fetch a page from a location where people speak Chinese. I did test if I use browser directly to visit $path and the website detected my location and showed me this country's currency. Is it possible to let the browser think I am at united states? I tried send header like below but nothing change.
$opts = array(
'http'=>array(
'method' => "GET",
'header' => "Accept-language: en\r\n" .
// "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20120306 Firefox/3.6.28 ( .NET CLR 3.5.30729; .NET4.0E)\r\n"
"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13\r\n"
)
);
$context = stream_context_create($opts);
$html = file_get_contents($path, false, $context);
If the server that you want to load the page from detects your location by your IP you need to use a proxy. You can look for open proxies, or create one for yourself in AWS for example.
what I am missing here? all I get returned is "Location: 0"
ini_set("user_agent","Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
$url = "http://ebird.org/ws1.1/data/notable/region/recent?rtype=subnational1&r=US-AZ";
$xml = simplexml_load_file($url);
$locname = $xml->response->result->sighting->loc-id;
echo "Location: ".$locname . "<br/>";
the probelem is with the "-" because php think that you want to subtract id from $xml->response->result->sighting->loc
the solution is to change :
$locname = $xml->response->result->sighting->loc-id;
to
$locname = $xml->result[0]->sighting[0]->{'loc-id'};
it work with me
i hope this help you
note : i delete response node because it's the root and i choose the first elemet because the file containe many nodes
I am writing a social app where people will use TAGs for organizing their articles. These tags are shared across the site and each tag needs to have some description with it.
I wonder if there is any way I can programmatically fetch it from a resource like wikipedia. (say the first paragraph).
The tags will be typically associated with brands products and services.
Yes you can
<?php
$contents = file_get_contents("http://en.wikipedia.org/wiki/PHP");
preg_match("/<p>(.*?)<\/p>/", $contents, $match);
echo $match[1];
?>
http://sandbox.phpcode.eu/g/45c56.php
EDIT: Looks like they don't like non-validated browser agents. You'll have to do it with curl
EDIT2: curl with browser agent:
<?php
$ch = curl_init("http://en.wikipedia.org/wiki/PHP");
$useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$contents = curl_exec($ch);
preg_match("/<p>(.*?)<\/p>/", $contents, $match);
$match[1] = preg_replace("|\[[0-9]\]|", "", strip_tags($match[1]));
echo (($match[1]));
?>
http://sandbox.phpcode.eu/g/ad578.php