[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP escape characters

Posted on 2010-01-02
17
Medium Priority
?
700 Views
Last Modified: 2013-11-18
Hi Experts,

I am getting a DOM error when I pass this url into the load method.  No error when I remove the method call with the single quotes.  Pretty sure the quotes are the problem.  \ preceding the quotes doesn't help.  magic_quotes_gpc is on, magic_quotes_runtime is off,  and it's PHP 5.2.11

How can I frame the quotes so $dom->load doesn't throw an error?

 Here is the URL string passed to $dom->load:  http://adcd.com/external/helloworld.jsp?<LightsLookup method='getNumberOfLights'><xmlInfo>blue,white,green</xmlInfo></LightsLookup>

Thanks,

HNM
0
Comment
Question by:HelpNearMe
  • 8
  • 8
17 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26164242
Try using urlencode?
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26164245
I tried this URL and got 404.  What is the right URL?
http://adcd.com/external/helloworld.jsp?%3CLightsLookup%20method=%27getNumberOfLights%27%3E%3CxmlInfo%3Eblue,white,green%3C/xmlInfo%3E%3C/LightsLookup%3E

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26164253
Hmm... Maybe one more idea - "entitize" the apostrophes with &apos;  Check the refs here:
http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 40

Assisted Solution

by:Richard Quadling
Richard Quadling earned 600 total points
ID: 26165013
I just realised you are calling a JSP page. PHP's magic quotes won't have any impact on that.
0
 

Author Comment

by:HelpNearMe
ID: 26166070
Hi Ray & RQuadling,

I used &apos instead as suggested and $dom->load() didn't throw an error but the server at the other end reported that the XML was not well formed.  The &apos appeared in the actual URL request string so the remote server couldn't parse the attribute properly.

The original code (single quotes/apostrophes included) worked fine for two months on a PHP 5.2.9 box.  Our production server is running 5.2.11 and seems to fall apart with these single quotes.  I have 5.3 running on WAMP locally and I have the same problem.  

I am going to try urlencode now, thanks for the help so far.  I'll be back with an update.

HNM
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26166128
Try htmlspecialchars() - I think it might be the right thing.  I always get it confused - sorry about that :-(
http://us.php.net/manual/en/function.htmlspecialchars.php

~Ray
0
 

Author Comment

by:HelpNearMe
ID: 26166246
I was reading about htmlspecialchars and htmlentities when you typed that ;)  I'll let you know how it works.

Thanks,

HNM
0
 

Author Comment

by:HelpNearMe
ID: 26167396
Hi Experts,

I just threw in the towel.  I can't get this to work.  Like I said ... worked great on 5.2.9 now on 5.2.11 dom->load errors.  Here is the actual URL, if you try it you will get an ip address authentication error.  Regardless you will get XML back in some form... unless of course you have the save problem I do.  It seems to be the single quotes..... Browser should return XML message of some type.

Please let me know if you have any ideas :)

http://axml.travelnow.com/external/xmlinterface.jsp?cid=22212064&resType=car200820&intfc=ws&xml=<CarSessionRequest method= 'getCarAvailability'><CarAvailabilityQuery><cityCode>AGU</cityCode><pickUpDate>01/29/2010</pickUpDate><dropOffDate>01/31/2010</dropOffDate><pickUpTime>12PM</pickUpTime><dropOffTime>12PM</dropOffTime><specialEquipmentCodes>BBS</specialEquipmentCodes></CarAvailabilityQuery></CarSessionRequest>
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26167404
Trimmed of unnecessary whitespace, I got this XML.
<Exception>
  <itineraryId>-1</itineraryId>
  <handling>1</handling>
  <category>4</category>

  <presentationMessage>TravelNow.com cannot service this request.</presentationMessage>
  <verboseMessage>Authentication failure. (cid=22212064; ipAddress= 74.9.128.130)</verboseMessage>
</Exception>

Open in new window

0
 

Author Comment

by:HelpNearMe
ID: 26167449
By the way I was using:
$doc = new DOMDocument();
$doc->load($urlXML);

I was getting this big ugly error:
Warning: DOMDocument::load() [domdocument.load]: I/O warning : failed to load external entity "/home/abc123/public_html/xmlTest/http:/axml.travelnow.com/external/xmlinterface.jsp?cid=22212064&resType=car200820&intfc=ws&xml=<CarSessionRequest method='getCarAvailability'><CarAvailabilityQuery><cityCode>ACA</cityCode><pickUpDate>01/22/2010</pickUpDate><dropOffDate>01/26/2010</dropOffDate><pickUpTime>12PM</pickUpTime><dropOffTime>12PM</dropOffTime><specialEquipmentCodes>BBS</specialEquipmentCodes></CarAvailabilityQuery></CarSessionRequest>" in /home/abc123/public_html/xmlTest/processXML.php on line 24
0
 

Author Comment

by:HelpNearMe
ID: 26167459
You got that from the browser or inside PHP?  And whitespace in the [method= ] part?

I sincerely appreciate your help with this.. :)

hnm
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26167470
Do you mean to have a blank in the URL?
0
 

Author Comment

by:HelpNearMe
ID: 26167483
No the blank was a result of my inserting \ and other characters attempting to fix the problem.  Without the space after method= it still throws the error.  If I remove the quotes all together the dom error disappears.  Of course the xml string is not valid but dom seems to behave well without the quotes.
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1400 total points
ID: 26167485
Try running this - it seems to make some sense, inasmuch as it provides an XML response that can be parsed into an object.
<?php // RAY_temp_HelpNearMe.php
error_reporting(E_ALL);
echo "<pre>\n";


function my_curl($url)
{
    $curl = curl_init();

// HEADERS FROM FIREFOX - APPEARS TO BE A BROWSER REFERRED BY GOOGLE

    $header[] = "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
    $header[] = "Cache-Control: max-age=0";
    $header[] = "Connection: keep-alive";
    $header[] = "Keep-Alive: 300";
    $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
    $header[] = "Accept-Language: en-us,en;q=0.5";
    $header[] = "Pragma: "; // browsers keep this blank.

    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6');
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    curl_setopt($curl, CURLOPT_REFERER, 'http://www.google.com');
    curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
    curl_setopt($curl, CURLOPT_AUTOREFERER, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_TIMEOUT, 2); // GIVE UP AFTER TWO SECONDS

    if (!$html = curl_exec($curl))
    {
        return FALSE;
    }

    curl_close($curl);
    return $html;
}


// TEST DATA
$url = <<<EOURL
http://axml.travelnow.com/external/xmlinterface.jsp?cid=22212064&resType=car200820&intfc=ws&xml=<CarSessionRequest%20method='getCarAvailability'><CarAvailabilityQuery><cityCode>AGU</cityCode><pickUpDate>01/29/2010</pickUpDate><dropOffDate>01/31/2010</dropOffDate><pickUpTime>12PM</pickUpTime><dropOffTime>12PM</dropOffTime><specialEquipmentCodes>BBS</specialEquipmentCodes></CarAvailabilityQuery></CarSessionRequest>
EOURL;

// READ FROM THE URL
$xml = my_curl($url);
$xml = trim($xml);

// MAKE AN OBJECT FROM THE XML
$obj = SimpleXML_Load_String($xml);

// DISPLAY THE OBJECT
var_dump($obj);

Open in new window

0
 

Author Comment

by:HelpNearMe
ID: 26167642
Line 40 is causing problems Parse error: syntax error, unexpected T_SL

Not sure what the <<<EOURL is.  Should I modify it?

Thanks,

HNM
0
 

Author Comment

by:HelpNearMe
ID: 26167712
I removed the <<EOURL stuff and put quotes around the string.  It returns XML..... so considering the time spent on this.. I may move over to cURL from DOM->Load.  I am using XSLT after this step so I'll work on glueing cURL together with my XSLT templates to output HTML.

Ray and RQuadling, thanks so much.  Problem not solved but avoided. :)

Thanks,

HNM
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26171057
Thanks for the points.  What level of PHP are you running?  I cannot figure why HEREDOC worked on my PHP 5.2 and would not work for you.  Please see:
http://us2.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I made this because I wanted to get e-mail with a attached csv file so I'd would be able to import user input into a MS Excel template, but I also wanted to register/save all inputs from each day in a file on the server. 1st - It creates a temp C…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month20 days, 14 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question