"Comment not terminated" XML parsing error in Box API response - php

For months I've been running the "Box Rest Client" lib by Angela R that employs the following code to parse curl responses from the box API:
$xml = simplexml_load_string($res);
Today, after the code loops through dozens of request/responses I generate this following error:
ErrorException [ Warning ]: simplexml_load_string(): Entity: line 9:
parser error : Comment not terminated
This happened in 2 straight attempts to run the code - and now seems to have gone away without any changes to anything.
Interested if anyone knows what is up with that?
I have put a catch for this case if its useful to anyone using this lib (for the next month or so before its deprecated by box api 2.0)
private function parse_result($res) {
try {
$xml = simplexml_load_string($res);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
return $array;
} catch (Exception $e){
$error = 'xml parsing error: '. $e->getMessage(). "<br>";
return array('status' => $error );
}
}

It's possible it is related to including two minus signs -- inside of an HTML comment. For example:
<!-- this is my comment--but not a very good one. -->
The two dashes in the middle of the comment causes problems with the parser.

Related

Google Api PHP unable to pass variable to call('list')

I am trying to run the following code with the new Google Business Profile API:
$mybusinessbusinessinformationService = new Google\Service\MyBusinessBusinessInformation($client);
try
{
$locations = $mybusinessbusinessinformationService->accounts_locations->call('list',['parent'=>'accounts/1111111111111111111']);
var_dump($locations);
} catch(Exception $e){
var_dump(print_r($e->getMessage(),1));
}
But I am getting the following error:
(list) missing required param: 'parent'
As far as I can tell it seems that I am parsing the param properly.
For people that are searching for an answer:
Since Google updated its API, it's a requirement to parse readMask as well:
$mybusinessbusinessinformationService = new Google\Service\MyBusinessBusinessInformation($client);
$params = array('readMask'=> "title,name");
$locations = $mybusinessbusinessinformationService->accounts_locations->listAccountsLocations($getAccountName,$params);
var_dump($locations);
You can find the mask parameters here:
https://developers.google.com/my-business/reference/businessinformation/rest/v1/locations

PHP simple HTML DOM parser errors

I've started writing a scraper for one site that will also have a crawler, since I need to go through some links, but I'm getting this error :
PHP Fatal error: Uncaught Error: Call to a member function find() on
null in D:\Projekti\hemrank\simple_html_dom.php:1129 Stack trace:
0 D:\Projekti\hemrank\scrapeit.php(37): simple_html_dom->find('ul')
1 D:\Projekti\hemrank\scrapeit.php(19): ScrapeIt->getAllAddresses()
2 D:\Projekti\hemrank\scrapeit.php(55): ScrapeIt->run()
3 {main} thrown in D:\Projekti\hemrank\simple_html_dom.php on line 1129
When I var_dump the $html variable I get the full html with all the tags, etc, that's why it's strange to me that it says "Call to a member function find() on null", when there's actually value in the $html. Here's the part of the code that's not working :
$html = new simple_html_dom();
$html->load_file($baseurl);
if(empty($html)){echo "HTTP Response not received!<br/>\n";exit;}
$links = array();
foreach ($html->find('ul') as $ul) {
if(!empty($ul) && (count($ul)>0))
foreach ($ul->find('li') as $li) {
if(!empty($li) && (count($li)>0))
foreach ($li->find('a') as $a) {
$links[] = $a->href;
}
else
die("NOT AVAILABLE");
}
}
return $links;
}
Is this a common problem with PHP simple HTML DOM parser, is there a solution or should I switch to some other kind of scraping?
I just searched for the lib you are using, this is line 1129:
return $this->root->find($selector, $idx, $lowercase);
So your error message is telling you that $this->root inside the class is null, therefore no find() method exists!
I'm no expert on the lib, as I use the awesome DOMDocument for parsing HTML, but hopefully this should help you understand what has happened.
Also, $html will never be empty in that code of yours, you already populated it when you instantiated it!
I suggest the following change:
$html->load_file($baseurl); to $html = file_get_html($baseurl);
On my VPS server it works with $html->load_file($baseurl); but on my dedicated local server it only works with $html = file_get_html($baseurl);
This solved my problem:
- Call to a member function find() on null
- simple_html_dom.php on line 1129

Handle SimpleXMLElement error if bad url given

I am getting some products via some xml files daily.
Sometimes there are 4 files, sometimes there are up to 10.
I want to process them in a loop with SimpleXMLElement from an URL.
Here is what I try:
for ($i = 1; $i <= 10; $i++) {
try {
$SimpleXML = new \MFF_System\SimpleXMLExtended($file, LIBXML_NOCDATA, true);
} catch (Exception $e) {
var_dump($e);
}
}
But unluckily I've got warning:
SimpleXMLElement::__construct(): http://example.com/file_6.xml:42:
parser error : Input is not proper UTF-8, indicate encoding ! Bytes:
0xE1 0x6D 0x6F 0x67
In this case, there were only 5 files, so when I try to get the 6th, that gaves me the main page from the site. I've also tried with to surpress the warning without success.
$SimpleXML = #new \MFF_System\SimpleXMLExtended($file, LIBXML_NOCDATA, true);
Is there any way to handle these errors in order to avoid them stopping my script?
EDIT
I can not use file_get_contents because in this case, I am getting memory limit error, and I can not increase the memory. These are so big files, one of it is 1GB. Guess what, the developer of these files are put the product images binaries into the file :((((((((( (I can not speak with him).
EDIT2
In php documentation (here) I've read $var = #new some_class(); should work.
If I am using:
$SimpleXML = #new \MFF_System\SimpleXMLExtended($file, LIBXML_NOCDATA, true);
I get this:
Exception: String could not be parsed as XML in ........\Parser.php on line 19
I thought, yeah, it is much more better, because this is an Expection. If I wrap with try/catch, I just get the same error.
try {
$SimpleXML = #new \MFF_System\SimpleXMLExtended($file, LIBXML_NOCDATA, true);
} catch (Exception $e) {
die('Exception');
}
\MFF_System\SimpleXMLExtended is just extends the SimpleXMLElement class, and use a method to add CDATA, nothing special.

PHP Zend Gdata error 400 when inserting row in spreadsheet

I'm doing an small class to load spreadsheets and insert data in them but when I insert a row i retrieve an error:
Expected response code 200, got 400 We're sorry, a server error
occurred. Please wait a bit and try reloading your spreadsheet.
I have looking in this site and google and some people has this error when they trying to insert data with capital letters or spaces... i'm not trying to insert data like this so i suppose that this is not my error. I the code that is giving me problems:
try {
echo $this->_spreadId . '<br>';
echo $this->_defaultWorksheetId . '<br>';
echo get_class($this->_spreadClient) . '<br>';
$rowData = array('stuff' => 'smurf');
$this->_spreadClient->insertRow($rowData, $this->_spreadId, $this->_defaultWorksheetId);
} catch (Exception $exc) {
echo '<br><pre>' . $exc->getTraceAsString() . '</pre>';
echo '<br>' . $exc->getMessage();
}
The output of this script is:
tVe4Mr82qD3LRhubQrcLxMQ
od6
Zend_Gdata_Spreadsheets
#0 /home/sergi/projects/quiniela2gdocs/lib/Zend/Gdata.php(219): Zend_Gdata_App->performHttpRequest('POST', 'https://spreads...', Array, 'performHttpRequest('POST', 'https://spreads...', Array, 'post('insertEntry('insertRow(Array, 'tVe4Mr82qD3LRhu...', 'od6')
Expected response code 200, got 400 We're sorry, a server error occurred. Please wait a bit and try reloading your spreadsheet.
Another method of this class is creating the spreadsheet and it's doing well, so i think there aren't connecting issues with gDocs.
Some help is welcome.
Lots of thanks !!
I had a similar problem which has arisen due to the presence of spaces in the array. Remove spaces helped me:
$rowData = array('stuff' => 'smurf'); - your code
$rowData = array('stuff'=>'smurf'); - my code

xslt_create() with PHP5

I am currently trying to display a RSS feed in a PHP page, but it seems to have a problem between the xslt_create() function and PHP5.
<?php
$xh = xslt_create();
$file=fopen(WEB_DIR . 'assets/_xml/rss.xml','r');
$xml=fread($file,16384);
fclose($file);
$file=fopen(WEB_DIR . 'assets/_xml/rss.xslt','r');
$xsl=fread($file,16384);
fclose($file);
$arguments = array(
'/_xml' => $xml,
'/_xsl' => $xsl
);
$result = xslt_process($xh, 'arg:/_xml', 'arg:/_xsl', NULL, $arguments);
xslt_free($xh);
print "$result";
?>
I get the error
Fatal error: Call to undefined
function xslt_create() in
What do I have to do to correct this issue ?
xslt_create only exists in PHP 4 - as this approach to parsing XML was removed in PHP 5.
As such, I'd recommend updating your code to use one of the current PHP 5 approaches as listed within the XML Manipulation section of the manual. (The XSL extension provides a XSLTProcessor class that is probably the nearest direct equivalent.)

Categories