auto Update page every day or hour with curl? - php

Hello How can do auto Update page every day whit curl to get data?
I don't know How do.
I write this code.
<?php
include_once ("xpath.php");
$ch = curl_init ("http://wpu.ir/1hp9j");
$baseUrl ="http://www.digionline.ir";
$dom = new DOMdocument();
#$dom->loadHTML($cl);
$xpath = new DOMXpath($dom);
$produsttitleQuery = $xpath->query("//span[#class='product-page-detail-wrapper- text']/text()");
$produstpriceQuery = $xpath->query("//span[#class='product-detail-wrapper- price']/text()");
$produstlinkQuery = $xpath->query("//ul[#id='product-page-product']/li/a/#href");
$data = array();
for ($x=0; $x<20; $x++){
$data[$x]['title'] = $produsttitleQuery->item($x)->nodeValue;
$data[$x]['price'] = $produstpriceQuery->item($x)->nodeValue;
$data [$x]['link'] =$baseUrl.$produstlinkQuery->item($x)->nodeValue;
}
echo "<pre>";
print_r($data);
?>
thank's

Related

Why array result is duplicated?

Can anyone help test this code and tell me what the error is?
The expected result is 01223658060102111111. but it is duplicated like this
01223658060102111111012236580601021111110122365806010211111101223658060102111111
here is my code
<?php
ini_set('user_agent', 'My-Application/2.5'); //without this file_get_content would not work
$saveURL = fopen("url.txt", "w");
$html = file_get_contents("http://www.carlist.my/used-cars/2592832/2004-toyota-camry-2-0.html");
$dom = new DOMDocument();
#$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$cont = $xpath->evaluate("//ul[contains(#class, 'list-contact')]/li");
foreach($cont as $con){
echo $con->nodeValue;
}
Replace this line to
$cont = $xpath->evaluate("//ul[contains(#class, 'list-contact')]/li");
to
$cont = $xpath->evaluate("//ul[contains(#class, 'list-contact')][1]/li");
#july77: How can I split them to get result 0122365806 and 060102111111
$arr = array();
foreach($cont as $con){
$arr[] = $con->nodeValue;
}
$first = $arr[0];
$second = $arr[1];

Crawling through Amazon Bestsellers page

<?php
$i=1;
while ($i<=5) {
# code...
$url = 'http://www.amazon.in/gp/bestsellers/electronics/ref=zg_bs_nav_0#'.$i;
echo $url;
$html= file_get_contents($url);
$dom = new DOMDocument();
#$dom->loadHTML($html);
$xPath = new DOMXPath($dom);
$classname="zg_title";
$elements = $xPath->query("//*[contains(#class, '$classname')]");
foreach ($elements as $e)
{
$lnk = $e->getAttribute('href');
$e->setAttribute("href", "http://www.amazon.in".$lnk);
$newdoc = new DOMDocument;
$e = $newdoc->importNode($e, true);
$newdoc->appendChild($e);
$html = $newdoc->saveHTML();
echo $html;
}
$i++;
}
?>
I am trying to crawl through the Amazon bestsellers page which has a list of top 100 bestseller items which have 20 items in each page. In every loop the $i value is changed and appended to URL. But only the first 20 items are being displayed 5 times, I think this has something to do with the ajax pagination, but i am not able to figure out what it is.
Try this:
<?php
$i=1;
while ($i<=5) {
# code...
$url = 'http://www.amazon.in/gp/bestsellers/electronics/ref=zg_bs_electronics_pg_'.$i.'?ie=UTF8&pg='.$i;
echo $url;
$html= file_get_contents($url);
$dom = new DOMDocument();
#$dom->loadHTML($html);
$xPath = new DOMXPath($dom);
$classname="zg_title";
$elements = $xPath->query("//*[contains(#class, '$classname')]");
foreach ($elements as $e)
{
$lnk = $e->getAttribute('href');
$e->setAttribute("href", "http://www.amazon.in".$lnk);
$newdoc = new DOMDocument;
$e = $newdoc->importNode($e, true);
$newdoc->appendChild($e);
$html = $newdoc->saveHTML();
echo $html;
}
$i++;
}
?>
Change your $url

Fetch Date form other website...in php

I want to fetch all phone from this website (olx.com.pk).
I have found that function but they will fetch date single phone number from single link of this site (olx.com.pk)
<?php
error_reporting(0);
$ch = curl_init("http://olx.com.pk/item/samsung-galaxy-tab3-16gb-white-IDSUu7h.html#7aae8d1c9a");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$cl = curl_exec($ch);
$dom = new DOMDocument();
#$dom->loadHTML($cl);
//$links = $dom->getElementsByTagName('a');
$xpath = new DOMXpath($dom);
$number = $xpath->query("//strong[#class='xx-large']//text()");
echo "<h1>". $number->item(0)->nodeValue ."</h1>";
?>
I want to fetch all phone number at one...
is it possible to get all number?
Here is slightly simplified version of same code:
// suppress DOM warnings
libxml_use_internal_errors(true);
$url = "http://olx.com.pk/item/samsung-galaxy-tab3-16gb-white-IDSUu7h.html#7aae8d1c9a";
$dom = new DOMDocument();
$dom->loadHTMLfile($url);
$xpath = new DOMXpath($dom);
$items = $xpath->query("//strong[#class='xx-large']");
// loop through items to retrieve node values
foreach ($items as $item) {
echo "<h1>". $item->nodeValue ."</h1>";
}
This code will fetch URL and select all strong[#class='xx-large'] nodes. Values for individual nodes are retrieved inside foreach loop.
P.S.
There is only one phone number on indicated URL and as a final result you can only see one phone number.

Print an array after DOM extraction?

I need to print out my array, but print_r($test) doesn't work at last...
Here is a simple code :
$code = '<html><head></head><body><div class="list"><img src="http://google.com/564308080517287.jpg" alt="my title"></div></body></html>'; // Code is simplified here, but imagine you've got much more contents inside
$doc = new DOMDocument();
$doc->loadHTML( $code );
//
$test = array();
foreach($doc->getElementsByTagName('div') as $div){
if($div->getAttribute('class') == "list"){
$ads_count = $div->getElementsByTagName('a')->length;
for ($i=0; $i<=$ads_count; $i++) {
$ad = $div->getElementsByTagName('a')->item($i);
$ad_img = trim($ad->getElementsByTagName('img')->item(0)->getAttribute('src'));
$test[$i]['img'] = $ad_img;
}
}
}
print_r($test); // doesn't work !!
Any idea ?
<?php
$code = '<html><head></head><body><div class="list">
<img src="http://google.com/564308080517287.jpg" alt="my title"></div></body></html>'; // Code is simplified here, but imagine you've got much more contents inside
$dom = new DOMDocument();
$dom->loadHtml($code);
$selector = new DOMXPath($dom);
$parceiltable = $selector->query("//div[#class='list']/a/img");
foreach($parceiltable as $key=>$tds){
$test[]['img'] = $tds->getAttribute('src');
}
print_r($test);
?>

Getting XML attributes in PHP

Looked at a few other SO posts on this but no joy.
I've got this code:
$url = "http://itunes.apple.com/us/rss/toppaidapplications/limit=10/genre=6014/xml";
$string = file_get_contents($url);
$string = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $string);
$xml = simplexml_load_string($string);
foreach ($xml->entry as $val) {
echo "RESULTS: " . $val->attributes() . "\n";
but I can't get any results.
I'm specifically interested in getting the ID value which would be 549592189 in this fragment:
<id im:id="549592189" im:bundleId="com.activision.wipeout">http://itunes.apple.com/us/app/wipeout/id549592189?mt=8&uo=2</id>
Any suggestions?
SimpleXML gives you can easy way to drill down in the XML structure and get the element(s) you want. No need for the regex, whatever it does.
<?php
// Load XML
$url = "http://itunes.apple.com/us/rss/toppaidapplications/limit=10/genre=6014/xml";
$string = file_get_contents($url);
$xml = new SimpleXMLElement($string);
// Get the entries
$entries = $xml->entry;
foreach($entries as $e){
// Get each entriy's id
$id = $e->id;
// Get the attributes
// ID is in the "im" namespace
$attr = $id->attributes('im', TRUE);
// echo id
echo $attr['id'].'<br/>';
}
DEMO: http://codepad.viper-7.com/qNo7gs
Try with xpath:
$doc = new DOMDocument;
#$doc->loadHTML($string);
$xpath = new DOMXpath($doc);
$r = $xpath->query("//id/#im:id");
$id = $r->item(0)->value;
Try:
$sxml = new SimpleXMLElement($url);
for($i = 0;$i <=10;$i++){
$appid= $sxml->entry[$i]->id->attributes("im",TRUE);
echo $appid;
}

Categories