I am trying to incorporate my pin feed into my site. I have got it working but I need to amend what it shows as it's not quite working as intended.
What I need is to extract a certain piece of data from the description bit of the date.
This is the code I use to grab my XML feed:
<?php
$ch = curl_init("http://pinterest.com/1234/feed.rss");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$doc = new SimpleXmlElement($data, LIBXML_NOCDATA);
if(isset($doc->channel))
{
parseRSS($doc);
}
function parseRSS($xml)
{
$cnt = 9;
for($i=0; $i<$cnt; $i++)
{
$url = $xml->channel->item[$i]->link;
$img = $xml->channel->item[$i]->description;
$title = $xml->channel->item[$i]->title;
echo '<p>'.$img.'</p>';
}
}
?>
The problem is that description looks like below and all I want is the src value from it:
<description><p><a href="/pin/1785432765530/"><img src="http://media-cache-ec1.pinterest.com/upload/27099622222548513383_qJV62266Pf_b.jpg"></a></p><p>What it takes to Google’s.</p></description>
Is there a way to just get src="http://media-cache-ec1.pinterest.com/upload/270996666522513383_qJV6666Pf_b.jpg" out of the description and store it in $img or another variable?
html_entity_decode and Simple HTML DOM parser could solve your problem.
(http://stackoverflow.com/questions/138313/how-to-extract-img-src-title-and-alt-from-html-using-php)
Some RegExp would help you (PHP Manual, Wikipedia)
eg.: .*(src=".*[^"]").*
thanks all i used
$cnt = 9;
for($i=0; $i<$cnt; $i++)
{
$url = $xml->channel->item[$i]->link;
$img = $xml->channel->item[$i]->description;
$title = $xml->channel->item[$i]->title;
$pattern = '/src="([^"]*)"/';
preg_match($pattern, $img, $matches);
$src = $matches[0];
unset($matches);
//echo $src;
echo '<p><img '.$src.'</img></p>';
}
}
?>
thanks for the tips
Related
I am trying to pull values from the following url: https://api.binance.com/api/v1/ticker/allPrices
What I want to accomplish:
Obtain all the "symbol" values printed in the website (one under the other)
The result I am looking for, would be:
ETHBTC
LTCBTC
...
This is what I have done so far:
<?php
$url = "https://api.binance.com/api/v1/ticker/allPrices";
$content = file_get_contents($url);
$json_data = json_decode($content, true);
for ($i=0; $i < count($json_data); $i++) {
# code...
print_r($json_data);
}
?>
The result it brings is the entire data (the symbols, prices, plus other irrelevant data).
Thanks in advance.
You can use foreach loop, then echo the value by symbol key
$url = "https://api.binance.com/api/v1/ticker/allPrices";
$content = file_get_contents($url);
$json_data = json_decode($content, true);
foreach ($json_data as $value) {
echo $value["symbol"] . '<br>';
}
This ought to do it:
<?php
$url = "https://api.binance.com/api/v1/ticker/allPrices";
$content = file_get_contents($url);
$json_data = json_decode($content, true);
for ($i=0; $i < count($json_data); $i++) {
echo $json_data[$i]['symbol'] . "\n";
}
?>
json_data["symbol"]
instead in the print
So far what i have been trying to do is output a video response i know it wont work if i dont have the $id after the tag but before the tag it wont even work if the str_replace doesn't work.
<?php
$html = "";
$url = "https://www.youtube.com/feeds/videos.xml?user=Fliberjig1";
$xml = simplexml_load_file($url);
for($i = 0; $i < 6; $i++) {
$id = $xml->entry[$i]->id;
$id = str_replace("YT:VIDEO:","", $id);
$title = $xml->entry[$i]->title;
$html .= "<li><h3>$title</h3>
<p>$id</p>
<iframe width='854' height='480' src='https://www.youtube.com/embed/$id' frameborder='0' allowfullscreen></iframe></li>";
}
echo $html;
?>
the result will be like this
YT:VIDEO:WOCIEMNSI4C
but i want it to be like this
WOCIEMNSI4C
Can you please help in any way to help me with my problem
If I understand you correctly, you just want to parse the string. This can be done using:
$pieces = explode(":", $id);
$clean_id = $pieces[2];
It seems that the feed contains yt:video:WOCIemNSI4c and not YT:VIDEO:WOCIEMNSI4C so what you actually need is the str_ireplace
function instead of str_replace you're currently using.
So if you change your 7th line of code to this:
$id = str_ireplace("YT:VIDEO:","", $id);
you should be ok.
$id = "YT:VIDEO:WOCIEMNSI4C";
$rest = substr("$id", -11); //result WOCIEMNSI4C
Im stuck trying to make PHP output the content within a set tag id.
Example: <div id="myID" class="section">...Content to output...</div>
But i only manage to make PHP output from the tag.
Example: <div>...Content...</div>
Here is the code ive used:
$theTag = $_REQUEST['tag'];
$url = $_REQUEST['url'];
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($curl);
curl_close($curl);
$DOM = new DOMDocument;
$DOM->loadHTML( $output);
//get all H1
$items = $DOM->getElementsByTagName("$theTag");
//display all H1 text
for ($i = 0; $i < $items->length; $i++)
echo $items->item($i)->nodeValue . "<br/>";
I would use a query similiar to http://example.com/get-content?url=http://example.com/page.file&tag=myID
How can i make this work ?
Thanks!
This question already has answers here:
How to get attribute of node with namespace using SimpleXML? [closed]
(4 answers)
Closed 8 years ago.
I need to display such info from a rss feed using PHP
Title , Link / url , description and image
I have already done this code below but i am unable to fetch the image from a feed
I checked many site but still yet unable to solve this problem
<?php
$ch = curl_init("http://economico.sapo.pt/rss/ultimas");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$doc = new SimpleXmlElement($data);
//print_r($doc);
if(isset($doc->channel))
{
parseRSS($doc);
}
if(isset($doc->entry))
{
parseAtom($doc);
}
function parseRSS($xml)
{
// echo "<strong>".$xml->channel->title."</strong>";
$cnt = count($xml->channel->item);
for($i=0; $i<$cnt; $i++)
{
$postdate = $xml->channel->item[$i]->pubDate;
//pubDate
$url = $xml->channel->item[$i]->link;
$title = $xml->channel->item[$i]->title;
$desc = $xml->channel->item[$i]->description;
echo $postdate."<br/>".''.$title.'<br/>'.$desc.'<br/>';
}
}
function parseAtom($xml)
{
echo "<strong>".$xml->author->name."</strong>";
$cnt = count($xml->entry);
for($i=0; $i<$cnt; $i++)
{
$urlAtt = $xml->entry->link[$i]->attributes();
$url = $urlAtt['href'];
$title = $xml->entry->title;
$desc = strip_tags($xml->entry->content);
echo ''.$title.''.$desc.'';
}
}
?>
You are already on that right track with using ->attributes(), with regards to those with namespaces, just use ->children(). Simple example:
$url = 'http://economico.sapo.pt/rss/ultimas';
$rss = simplexml_load_file($url, null, LIBXML_NOCDATA);
foreach($rss->channel->item as $item) {
$title = (string) $item->title;
$link = (string) $item->link;
$description = (string) $item->description;
$pubDate = (string) $item->pubDate;
$media_image_url = '';
$media_title = '';
$media = $item->children('media', 'http://search.yahoo.com/mrss/');
if(isset($media->content)) {
$media_image_url = (string) $media->content->attributes()->url;
$media_title = (string) $media->content->title;
}
echo "
Title: $title <br/>
Link: $link <br/>
Description: $description <br/>
Pub Date: $pubDate <br/>
Image URL: $media_image_url <br/>
Media Title: $media_title <br/>
<hr/>
";
}
Sample Output
I would like to do parse XML for the url:
http://www.opencellid.org/cell/get?key=myapikey&mcc=250&mnc=99&cellid=29513&lac=0
I am the beginner of PHP and tried the code as below:
$url = "http://www.opencellid.org/cell/get?key=myapikey&mcc=250&mnc=99&cellid=29513&lac=0";
$xml = simplexml_load_file($url) or die("feed not loading");
print_r($xml);
var_dump($xml);
I would like to do echo for each attribute e.g. lat, lon, range.. for this XML url.
I found many resource in stackoverflow which the XML is quite standard. I cannot find an example which is used for this format of XML:
Anyone could give me an idea? Thanks.
$url = "http://www.opencellid.org/cell/get?key=myapikey&mcc=250&mnc=99&cellid=29513&lac=0";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
$status = curl_getinfo($ch,CURLINFO_HTTP_CODE);
curl_close($ch);
if($status==200){
$x = new SimpleXMLElement($data,LIBXML_NOCDATA);
echo 'lat: '.$x->cell['lat'];
echo 'lon: '.$x->cell['lon'];
echo 'range: '.$x->cell['range'];
}
Update:
This is an alternative to curl. If you have curl installed, use curl, it is faster.
$url = "http://www.opencellid.org/cell/get?key=myapikey&mcc=250&mnc=99&cellid=29513&lac=0";
$data = file_get_contents($url);
$x = new SimpleXMLElement($data,LIBXML_NOCDATA);
$lat = $x->cell['lat'];
$lng = $x->cell['lon'];
$range = $x->cell['range'];
echo 'lat: '.$lat.'<br>';
echo 'lng: '.$lng.'<br>';
echo 'range: '.$range.'<br>';
Please try this
$content = file_get_contents('http://www.opencellid.org/cell/get?key=myapikey&mcc=250&mnc=99&cellid=29513&lac=0');
$x = new SimpleXmlElement($content);
foreach($x->cell as $entry) {
echo $entry['lat']."==".$entry['lon']; exit;
}
$url = "http://www.opencellid.org/cell/get?key=myapikey&mcc=250&mnc=99&cellid=29513&lac=0";
$xml = simplexml_load_file($url) or die("feed not loading");
$attr = array("lat", "lon"); //list attr you require
foreach($xml->cell as $cell){
$data = $cell->attributes();
foreach ($attr as $key) {
// echo "<br>key: $key, value: " . $data[$key];
//edit to insert
$columns[] = $key;
$values[] = $data[$key];
}
echo $query = "insert into table_name(".implode(',',$columns).") values (".implode(',',$values).")";
}