0

What option for cURL needs to be changed in order to retrieve the XML content, as I have added the curlopt
option for xml content but all that happens is the JS error of "no data received"

<?php
    $agent = $_SERVER["HTTP_USER_AGENT"];
    $timeout = 5; // set to zero for no timeout

    if (isset($_GET['url'])) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $_GET['url']);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
    curl_setopt($ch, CURLOPT_PROXY, "http://192.168.xx.xx:xxxx");
    curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $file_contents_curl = curl_exec($ch);
    curl_close($ch);

    //header('Content-Type: text/xml');
    echo $file_contents_curl("http://".SanitizeString($_GET['url']));
    }
    function SanitizeString($var) {
            $var = strip_tags($var);
            $var = htmlentities($var);
            return stripslashes($var);
        }

Now the JS part

<html>
  <head>
    <title>Ajax XML Example</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
      <h2>Loading XML content into a DIV</h2>
      <div id='info'> This sentence will be replaced</div>
      <script>

      nocache = "&nocache=" + Math.random() * 1000000
      //url = "rss.news.yahoo.com/rss/topstories"
      url = "news.yahoo.com/rss/topstories"
      request = new ajaxRequest()
      request.open("GET", "xmlget.php?url=" + url + nocache, true )
      out = "";

      request.onreadystatechange = function()
      {
          if (this.readyState == 4)
          {
              if (this.status == 200)
              {
                 if (this.responseXML != null)
                 {
                   titles = this.responseXML.getElementsByTagName('title')

                   for (j = 0 ; j < titles.length ; ++j)
                   {
                       out += titles[j].childNodes[0].nodeValue + '<br />'
                   }
                   document.getElementById('info').innerHTML = out              
                 }
                 else alert("Ajax error: No data received")
              }
              else alert("Ajax error: " + this.statusText)
          }
      }


       request.send(null)

       function ajaxRequest() {
           try
           {
               var request = new XMLHttpRequest()
           }
           catch(e1)
           {
               try
               {
                   request = new ActiveXObject("Msxml2.XMLHTTP")
               }
               catch(e2)
               {
                   try
                   {
                       request = new ActiveXObject("Microsoft.XMLHTTP")
                   }
                   catch(e3)
                   {
                       request = false
                   }
               }
           }
           return request
       }

      </script>
  </body>
</html>
2
Contributors
4
Replies
13
Views
4 Years
Discussion Span
Last Post by shasta90
0

What option for cURL needs to be changed in order to retrieve the XML content

None, the code you had working in the previous thread should work fine for xml too. It should work without the HTTPHEADER. Just change line 19 to:

echo $file_contents_curl;

and enable line 18.

0

Made the following changes but still receive the error message "No data received"

$agent = $_SERVER["HTTP_USER_AGENT"];
    $timeout = 5; // set to zero for no timeout

    if (isset($_GET['url'])) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $_GET['url']);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    //curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
    curl_setopt($ch, CURLOPT_PROXY, "http://192.168.xx.xx:XXXX");
    curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $file_contents_curl = curl_exec($ch);
    curl_close($ch);

    header('Content-Type: text/xml');
    echo $file_contents_curl;
    //echo $file_contents_curl("http://".SanitizeString($_GET['url']));
    }
    function SanitizeString($var) {
            $var = strip_tags($var);
            $var = htmlentities($var);
            return stripslashes($var);
        }

The proxy I guess is causing the trouble and curl maybe needs the header type to be specified for xml?

0

I return xml output if I pass the url directly into curl instead of calling it from the JS file:

$agent = $_SERVER["HTTP_USER_AGENT"];
    $timeout = 5; // set to zero for no timeout
    $url = "news.yahoo.com/rss/topstories";

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
    curl_setopt($ch, CURLOPT_PROXY, "http://192.168.3xx.xx.XXXX");
    curl_setopt($ch, CURLOPT_PROXYPORT, xxxx);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $file_contents_curl = curl_exec($ch);
    curl_close($ch);

    echo $file_contents_curl("http://".SanitizeString($url));

The issue seems to be with the referencing of the "url" from the JS file to the PHP file:

javascript

      url = "news.yahoo.com/rss/topstories"
      request = new ajaxRequest()
      request.open("GET", "xmlget.php?url=" + url + nocache, true )
      out = "";

php script

      if (isset($_GET['url'])) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $_GET['url']);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
    curl_setopt($ch, CURLOPT_PROXY, "http://192.168.xx.xx.XXXX");
    curl_setopt($ch, CURLOPT_PROXYPORT, XXXX);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $file_contents_curl = curl_exec($ch);
    curl_close($ch);

    echo $file_contents_curl("http://".SanitizeString($_GET['url']));

How can I correct curl to reference the XML url correctly?

0

Still the same error about no receiving the XML data (url is not being loaded). Trying the example without any proxy info or curl.
Issue I think lies somewhere here:

nocache = "&nocache=" + Math.random() * 1000000
      url = "rss.news.yahoo.com/rss/topstories"
      request = new ajaxRequest()
      request.open("GET", "xmlget.php?url=" + url + nocache, true )
      out = "";
      request.onreadystatechange = function()
      {
          if (this.readyState == 4)
          {
              if (this.status == 200)
              {
                 if (this.responseXML != null)
                 {
                   titles = this.responseXML.getElementsByTagName('title')
                   for (j = 0 ; j < titles.length ; ++j)
                   {
                       out += titles[j].childNodes[0].nodeValue + '<br />'
                   }
                   document.getElementById('info').innerHTML = out              
                 }
                 else alert("Ajax error: No data received")
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.