Unable to fetch Price - php

I am trying to fetch price from a online store.
Here i am using this code..
<?php
function getPrice($site){
$html = file_get_contents($site);
$dom = new DOMDocument();
$dom->loadHTML($html);
$contents = $dom->document.getElementsByTagName("span");
$price = "";
for($i = 0; $i < $contents->length; $i++){
$content= $contents->item($i);
if($content->getAttribute('class') == "fk-font-verybig pprice vmiddle fk-bold"){
$price = $content->getAttribute('value');
}
}
echo $price;
}
$website = "http://www.flipkart.com/sogo-ss-5365-750-w-pop-up-toaster/p/itmdz3hgfjzgfp4v?pid=PUTDYWT2UHPCDCG8&offer=DOTDOnPopUpToaster_Sep2.&icmpid=hp_dotd_3_DOTDOnPopUpToaster_Sep2.";
getPrice($website);
?>
my script return error
Warning: DOMDocument::loadHTML(): Unexpected end tag : span in Entity, line: 261 in E:\Local server\htdocs\store\scripts\getprice.php on line 5
Warning: DOMDocument::loadHTML(): htmlParseEntityRef: no name in Entity, line: 293 in E:\Local server\htdocs\store\scripts\getprice.php on line 5
...................................................................
Warning: DOMDocument::loadHTML(): htmlParseEntityRef: expecting ';' in Entity, line: 6160 in E:\Local server\htdocs\store\scripts\getprice.php on line 5
Notice: Undefined property: DOMDocument::$document in E:\Local server\htdocs\store\scripts\getprice.php on line 6
Fatal error: Call to undefined function getElementsByTagName() in E:\Local server\htdocs\store\scripts\getprice.php on line 6
Is it ok to fetch price like this because the store keep changing price of its product.
Is there any other alternative way to do so?
Will this script affect my server performance because ever time user visit a product page on my website it will fetch prices from 5 different stores to comapare prices.

$contents = $dom->document.getElementsByTagName("span");
Your $dom->document is failing because DOMDocument class does not have a property named 'document'.
Notice: Undefined property: DOMDocument::$document in E:\Local server\htdocs\store\scripts\getprice.php on line 6
So this might work
$contents = $dom->getElementsByTagName("span");
The above should work.
I recommend iterating over $contents instead of echo.
Even print_r would help you see the structure of the nodes in $contents.

Related

Error In Wordpress On All Pages Just Below The Header (Using Theme elumine)

The error is in below lines of the addressed file below (Line 186 & 187):
Website (Live): www.healthyminds.world
Tried restoring an earlier back-up(24 hours back), but that didn't work also.
Line 186 & 187 from source line:
$collections = get_option('google_typography_collections');
$google_fonts_all = $this->get_fonts();
$import_fonts = array();
if ($collections) {
foreach ($collections as $collection) {
$font_family = $collection['font_family'];
$font_index = array_search($font_family,
array_column($google_fonts_all,'family'));//Line 186
$font_variants = implode(',', $google_fonts_all[$font_index ['variants']); //Line 187
array_push(
$import_fonts,
array(
'font_family' => $collection['font_family'],
'font_variant' => $font_variants,
Error Messages (On All Pages Of Website, these are just few of the lines):
content/themes/elumine/includes/integrations/google-typography/google-typography.php on line 186
Warning: array_search() expects parameter 2 to be array, null given in /home/customer/www/healthyminds.world/public_html/wp-content/themes/elumine/includes/integrations/google-typography/google-typography.php on line 186
Warning: Illegal string offset 'variants' in /home/customer/www/healthyminds.world/public_html/wp-content/themes/elumine/includes/integrations/google-typography/google-typography.php on line 187
Warning: implode(): Invalid arguments passed in /home/customer/www/healthyminds.world/public_html/wp-content/themes/elumine/includes/integrations/google-typography/google-typography.php on line 187
You are getting the errors because on line 186 the $google_fonts_all either does not contain an array with 'family' key or it is not an array. In either case the expression, array_column($google_fonts_all, 'family'), returns empty.
You can extract the later expression into a variable:
$family = array_column((array)$google_fonts_all, 'family');
On the line 187, check for the isset( $google_fonts_all[$font_index]['variants'] ) either in a conditional statement or itenary.
Regards
Eliasu
Wisdom Labs has posted a patch to fix the problem.

I want to add x customers on Prestashop via code but with extra fields

I'm trying to add customers with a code but the PrestaShop is giving me a bug.
I'm using PHP and XML
$XMLRQString = '<?xml version="1.0" encoding="utf-8"?>'.
'<x:Winmax4GetEntitiesRQ xmlns:x="urn:Winmax4GetEntitiesRQ">'.
'</x:Winmax4GetEntitiesRQ >';
$Params=array(
'CompanyCode'=>'',
'UserLogin'=>'',
'UserPassword'=>'',
'Winmax4GetEntitiesRQXML'=> $XMLRQString
);
$return = $client->GetEntities($Params);
$XMLRSString = new SimpleXMLElement($return->GetEntitiesResult);
foreach ($XMLRSString->Entities->Entity as $entity)
{
$default_lang= Configuration::get('PS_LANG_DEFAULT');
$customer=new Customer();
$customer->email= $entity->Email;
$customer->lastname= $entity->EntityType;
$customer->firstname= [$default_lang => $entity->Name];
$customer->contribuinte= $entity->TaxPayerID;
$customer->passwd= $entity->TaxPayerID;
$customer->active = 1;
$customer->add();
}
ERROR: (1/1) ContextErrorException Warning: preg_match() expects
parameter 2 to be string, array given
in Validate.php line 172
at ValidateCore::isCustomerName(array(object(SimpleXMLElement))) in
ObjectModel.php line 1149
at ObjectModelCore->validateField('firstname',
array(object(SimpleXMLElement))) in ObjectModel.php line 981
at ObjectModelCore->validateFields() in ObjectModel.php line 284
at ObjectModelCore->getFields() in ObjectModel.php line 551
at ObjectModelCore->add(true, true) in Customer.php line 264
at CustomerCore->add() in create_clients.php line 66
When storing values from SimpleXML, if you just refer to the element itself by it's tag name - this will be an instance of SimpleXMLElement. As you want the actual content of the element, the simplest way to do this is to cast it to a string...
$customer->firstname= (string)$entity->Name;

simplexml_load_string warnings and error with a cdata string

I have a very long XML composed of articles and I use
$stream = new Stream\File($file, 1024);
$parser = new Parser\StringWalker();
$streamer = new XmlStringStreamer($parser, $stream);
while ($node = $streamer->getNode()) {
$simpleXmlNode = simplexml_load_string($node);
//little debug here
echo (string)$simpleXmlNode->codiceOD;
//
if(isset($simpleXmlNode->gruppo)) {
foreach($simpleXmlNode->gruppo->children() as $child) {
echo ' - ' . ($child);
}
}
echo '<br>';
}
It works very well except for a single line inside an article.
It's Italian language
<datiTecnici><![CDATA[POL<->SCHIENALE. MODULATORE DI SPINTA REGOLABILE. MULTIPOSIZIONATORE DI INCLINAZIONE. RIVESTIMENTO IN PELLE SMERIGLIATA, INTERNO IN SPUGNA HR ALTA DENSITA'. SCOCCA IN FAGGIO MULTISTRATO. BASE IN ALLUMINIO PRESSOFUSO VERNICIATO. BRACCIOLI IN POLIURETANO INTEGRALE CON ANIMA IN ACCIAIO. SEDILE REGOLABILE IN ALTEZZA MEDIANTE PISTONE A GAS (CORSA 10CM). PORTATA MAX 150KG.MISURE: H 117-127CM; L 63CM; P 60CM. (H TERRA<->SEDUTA 44-54CM). 21KG.]]></datiTecnici>
It raises
Warning: simplexml_load_string(): Entity: line 62: parser error : Extra content at the end of the document in /web/htdocs/www.site.net/home/import/iterator.php on line 81
Warning: simplexml_load_string(): in /web/htdocs/www.site.net/home/import/iterator.php on line 81
Warning: simplexml_load_string(): ^ in /web/htdocs/www.site.net/home/import/iterator.php on line 81
Notice: Trying to get property 'codiceOD' of non-object in /web/htdocs/www.site.net/home/import/iterator.php on line 82
Then stops.
I noticed that it has something to do with the length of the content inside CDATA. In fact it works fine if I cut the string a little (don't know how much yet).
What can I do?
I """solved""" it by splitting the big xml into much smaller xmls.
I don't know why it works now

PHP RSS FEED - Reading different feed types

I'm not sure that the title of my question is correct, but I'm trying to read some RSS Feeds (Podcasts feeds) and with the same PHP code, some rss feeds cannot be parsed, and others are parsed. I don't know why and I don't know how to track to find the error.
I'm using simplexml_load_file - code below:
<?php
//$xml = simplexml_load_file("http://hipsters.tech/feed/podcast/"); # it works
$xml = simplexml_load_file("https://jovemnerd.com.br/feed-nerdcast/"); # it doesn't works
# <channel> data
echo $xml->channel->title . "<br>\n";
echo $xml->channel->link . "<br>\n";
echo $xml->channel->language . "<br>\n";
echo $xml->channel->description . "<br>\n";
echo $xml->channel->image->url . "<br>\n";
$otherNode = $xml->channel->children('http://www.itunes.com/dtds/podcast-1.0.dtd');
echo $otherNode->author . "<br>\n";
echo "<br>#########################<br><br>\n";
# <item> data
for ($i = 0; $i < count($xml->channel->item); $i++ ) {
echo $xml->channel->item[$i]->title . "<br>\n";
echo $xml->channel->item[$i]->link . "<br>\n";
echo $xml->channel->item[$i]->pubDate . "<br>\n";
echo $xml->channel->item[$i]->description . "<br>\n";
echo "<br>#########################<br><br>\n";
}
?>
Inside my code, you can see that there are 2 links, the commented one is the link that works, the other doesn't works.
I've seen other posts on stack and other sites/forums, but I really don't know how to parse any RSS feed with one code, without headaches.
Please, I need some help. Thank you.
EDIT
Error:
Warning: simplexml_load_file(https://jovemnerd.com.br/feed-nerdcast/): failed to open stream: Connection timed out in /var/www/html/thundercast-api/FeedReader.php on line 7
Warning: simplexml_load_file(): I/O warning : failed to load external entity "https://jovemnerd.com.br/feed-nerdcast/" in /var/www/html/thundercast-api/FeedReader.php on line 7
Notice: Trying to get property of non-object in /var/www/html/thundercast-api/FeedReader.php on line 10
Notice: Trying to get property of non-object in /var/www/html/thundercast-api/FeedReader.php on line 10
Notice: Trying to get property of non-object in /var/www/html/thundercast-api/FeedReader.php on line 11
Notice: Trying to get property of non-object in /var/www/html/thundercast-api/FeedReader.php on line 11
Notice: Trying to get property of non-object in /var/www/html/thundercast-api/FeedReader.php on line 12
Notice: Trying to get property of non-object in /var/www/html/thundercast-api/FeedReader.php on line 12
Notice: Trying to get property of non-object in /var/www/html/thundercast-api/FeedReader.php on line 13
Notice: Trying to get property of non-object in /var/www/html/thundercast-api/FeedReader.php on line 13
Notice: Trying to get property of non-object in /var/www/html/thundercast-api/FeedReader.php on line 14
Notice: Trying to get property of non-object in /var/www/html/thundercast-api/FeedReader.php on line 14
Notice: Trying to get property of non-object in /var/www/html/thundercast-api/FeedReader.php on line 14
#########################
Notice: Trying to get property of non-object in /var/www/html/thundercast-api/FeedReader.php on line 21
Notice: Trying to get property of non-object in /var/www/html/thundercast-api/FeedReader.php on line 21

php/ xml Test is xml contains element

can you test if an xml file has a specific element. I tried this below:
$restrict = ($xml_content->xpath('//app:control//yt:state'));
$name = $restrict[0][0];
$restrictTest = $name->attributes();
if ($restrictTest == "restricted"){
$_SESSION['array']['restricted'] = 1;
}
but it displays this error:
Warning: SimpleXMLElement::xpath() [simplexmlelement.xpath]: Undefined namespace prefix in C:\xampp\htdocs\ytm\resources\func.php on line 178
Warning: SimpleXMLElement::xpath() [simplexmlelement.xpath]: xmlXPathEval: evaluation failed in C:\xampp\htdocs\ytm\resources\func.php on line 178
Fatal error: Call to a member function attributes() on a non-object in C:\xampp\htdocs\ytm\resources\func.php on line 181

Categories