Jump to content

cURL and Javascript


hitech

Recommended Posts

I am using cURL to get RSS files and then Javascript parse the file.

If I save the file and then read with Javascript, works fine, like this:

 

$ch = curl_init('http://news.google.com/news?ned=us&topic=h&output=rss');

$fp = fopen("nvrss.xml", "w");

curl_setopt($ch, CURLOPT_FILE, $fp);

curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch);

curl_close($ch);

fclose($fp);

<script>

    var rssDoc = new ActiveXObject("Microsoft.XMLDOM");

    rssDoc.async = false;

    rssDoc.load('nvrss.xml');

    itemNodes = rssDoc.selectNodes("/rss/channel/item");

</script>

 

But, if I pass variable directly from cURL to Javascript, works only if the RSS file does not have CDATA in title or description.

 

    $ch = curl_init("http://g1.globo.com/Rss2/0,,AS0-5597,00.xml");

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch, CURLOPT_HEADER, 0);

    //$txt = curl_exec($ch);

    $txt = str_replace("\"","'",$txt);

    $txt = str_replace("\n"," ",$txt);

    curl_close($ch);

 

<script>

    var rssDoc = new ActiveXObject("Microsoft.XMLDOM");

    rssDoc.async = false;

    rssDoc.loadXML("<?=$txt;?>");

    itemNodes = rssDoc.selectNodes("/rss/channel/item");

</script>

 

Why when I pass RSS via variable directly from cURL, Javascript can´t read file that contain CDATA?

 

Thanks

Helcio

Brazil

Link to comment
https://forums.phpfreaks.com/topic/94874-curl-and-javascript/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.