Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

use curl instead of fopen on this script

Posted on 2006-06-15
12
Medium Priority
?
939 Views
Last Modified: 2012-08-14
Hi, since Dreamhost(my host) doesn't allow fopen... is there a way to use curl or some other method to get this script working? Thanks! no javascript please!

<?php

/*
Created by Global Syndication's RSS Parser
http://www.globalsyndication.com/rss-parser
*/

set_time_limit(0);

$file = "http://news.google.com/news?hl=en&ned=us&ie=UTF-8&q=soccer+moms&output=rss";

$rss_channel = array();
$currently_writing = "";
$main = "";
$item_counter = 0;

function startElement($parser, $name, $attrs) {
         global $rss_channel, $currently_writing, $main;
         switch($name) {
               case "RSS":
               case "RDF:RDF":
               case "ITEMS":
                     $currently_writing = "";
                     break;
               case "CHANNEL":
                     $main = "CHANNEL";
                     break;
               case "IMAGE":
                     $main = "IMAGE";
                     $rss_channel["IMAGE"] = array();
                     break;
               case "ITEM":
                     $main = "ITEMS";
                     break;
               default:
                     $currently_writing = $name;
                     break;
         }
}

function endElement($parser, $name) {
         global $rss_channel, $currently_writing, $item_counter;
         $currently_writing = "";
         if ($name == "ITEM") {
               $item_counter++;
         }
}

function characterData($parser, $data) {
      global $rss_channel, $currently_writing, $main, $item_counter;
      if ($currently_writing != "") {
            switch($main) {
                  case "CHANNEL":
                        if (isset($rss_channel[$currently_writing])) {
                              $rss_channel[$currently_writing] .= $data;
                        } else {
                              $rss_channel[$currently_writing] = $data;
                        }
                        break;
                  case "IMAGE":
                        if (isset($rss_channel[$main][$currently_writing])) {
                              $rss_channel[$main][$currently_writing] .= $data;
                        } else {
                              $rss_channel[$main][$currently_writing] = $data;
                        }
                        break;
                  case "ITEMS":
                        if (isset($rss_channel[$main][$item_counter][$currently_writing])) {
                              $rss_channel[$main][$item_counter][$currently_writing] .= $data;
                        } else {
                              $rss_channel[$main][$item_counter][$currently_writing] = $data;
                        }
                        break;
            }
      }
}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fsockopen($file)) {
      die("could not open XML input");
}

while ($data = fread($fp, 4096)) {
      if (!xml_parse($xml_parser, $data, feof($fp))) {
            die(sprintf("XML error: %s at line %d",
                              xml_error_string(xml_get_error_code($xml_parser)),
                              xml_get_current_line_number($xml_parser)));
      }
}
xml_parser_free($xml_parser);

// output HTML
 //print ("<div class=\"channelname\">" . $rss_channel["TITLE"] . "</div>");

if (isset($rss_channel["ITEMS"])) {
      if (count($rss_channel["ITEMS"]) > 0) {
            // for($i = 0;$i < count($rss_channel["ITEMS"]);$i++) {
            for($i = 0;$i < 5;$i++) {
                  if (isset($rss_channel["ITEMS"][$i]["LINK"])) {
                  //print ("\n<div class=\"itemtitle\"><a href=\"" . $rss_channel["ITEMS"][$i]["LINK"] . "\">" . $rss_channel["ITEMS"][$i]["TITLE"] . "</a></div>");
                  } else {
                  print ("\n<div class=\"itemtitle\"><b>" . $rss_channel["ITEMS"][$i]["TITLE"] . "</b></div>");
                  }
                   print ("<div class=\"itemdescription\">" . $rss_channel["ITEMS"][$i]["DESCRIPTION"] . "</div>");             }
      } else {
            print ("<b>There are no new articles.</b>");
      }
}

?>
0
Comment
Question by:andreni78
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 10

Expert Comment

by:ClickCentric
ID: 16917409
I don't see fopen in the script...do they block fsockopen too?  Or is it that they block url filenames?
0
 
LVL 9

Expert Comment

by:blue_hunter
ID: 16917809
check http://my.php.net/manual/en/function.file-get-contents.php
file_get_contents(), see if it's supported in your host.
0
 

Author Comment

by:andreni78
ID: 16918227
they block fopen/fsockopen

Here's the wiki on this issue on DH:
http://wiki.dreamhost.com/index.php/Allow_url_fopen

You're right about the code not having fopen.. i wonder why it got removed?


<?php

/*
Created by Global Syndication's RSS Parser
http://www.globalsyndication.com/rss-parser
*/

set_time_limit(0);

$file = "http://news.google.com/news?hl=en&ned=us&ie=UTF-8&q=school++fundraising&output=rss";

$rss_channel = array();
$currently_writing = "";
$main = "";
$item_counter = 0;

function startElement($parser, $name, $attrs) {
         global $rss_channel, $currently_writing, $main;
         switch($name) {
               case "RSS":
               case "RDF:RDF":
               case "ITEMS":
                     $currently_writing = "";
                     break;
               case "CHANNEL":
                     $main = "CHANNEL";
                     break;
               case "IMAGE":
                     $main = "IMAGE";
                     $rss_channel["IMAGE"] = array();
                     break;
               case "ITEM":
                     $main = "ITEMS";
                     break;
               default:
                     $currently_writing = $name;
                     break;
         }
}

function endElement($parser, $name) {
         global $rss_channel, $currently_writing, $item_counter;
         $currently_writing = "";
         if ($name == "ITEM") {
               $item_counter++;
         }
}

function characterData($parser, $data) {
      global $rss_channel, $currently_writing, $main, $item_counter;
      if ($currently_writing != "") {
            switch($main) {
                  case "CHANNEL":
                        if (isset($rss_channel[$currently_writing])) {
                              $rss_channel[$currently_writing] .= $data;
                        } else {
                              $rss_channel[$currently_writing] = $data;
                        }
                        break;
                  case "IMAGE":
                        if (isset($rss_channel[$main][$currently_writing])) {
                              $rss_channel[$main][$currently_writing] .= $data;
                        } else {
                              $rss_channel[$main][$currently_writing] = $data;
                        }
                        break;
                  case "ITEMS":
                        if (isset($rss_channel[$main][$item_counter][$currently_writing])) {
                              $rss_channel[$main][$item_counter][$currently_writing] .= $data;
                        } else {
                              $rss_channel[$main][$item_counter][$currently_writing] = $data;
                        }
                        break;
            }
      }
}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($file, "r"))) {
      die("could not open XML input");
}

while ($data = fread($fp, 4096)) {
      if (!xml_parse($xml_parser, $data, feof($fp))) {
            die(sprintf("XML error: %s at line %d",
                              xml_error_string(xml_get_error_code($xml_parser)),
                              xml_get_current_line_number($xml_parser)));
      }
}
xml_parser_free($xml_parser);

// output HTML
 //print ("<div class=\"channelname\">" . $rss_channel["TITLE"] . "</div>");

if (isset($rss_channel["ITEMS"])) {
      if (count($rss_channel["ITEMS"]) > 0) {
            // for($i = 0;$i < count($rss_channel["ITEMS"]);$i++) {
            for($i = 0;$i < 5;$i++) {
                  if (isset($rss_channel["ITEMS"][$i]["LINK"])) {
                  //print ("\n<div class=\"itemtitle\"><a href=\"" . $rss_channel["ITEMS"][$i]["LINK"] . "\">" . $rss_channel["ITEMS"][$i]["TITLE"] . "</a></div>");
                  } else {
                  print ("\n<div class=\"itemtitle\"><b>" . $rss_channel["ITEMS"][$i]["TITLE"] . "</b></div>");
                  }
                   print ("<div class=\"itemdescription\">" . $rss_channel["ITEMS"][$i]["DESCRIPTION"] . "</div>");             }
      } else {
            print ("<b>There are no new articles.</b>");
      }
}

?>
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:andreni78
ID: 16918231
Here's the fopen alternative example... i just don't know how to implement it because i can't code :/

http://wiki.dreamhost.com/index.php/CURL
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 16918235
http://www.experts-exchange.com/Web/Web_Languages/PHP/PHP_Windows/Q_21877569.html

For a second, I thought the same question had somehow re-appeared.

<?php
$s_URL ='http://news.google.com/news?hl=en&ned=us&ie=UTF-8&q=soccer+moms&output=rss';

$r_curl = curl_init();
curl_setopt($r_curl, CURLOPT_URL, $s_URL);
curl_setopt($r_curl, CURLOPT_RETURNTRANSFER, 1);
$s_data = curl_exec($r_curl);
curl_close($r_curl);

// $s_data should not contain the data.

// Include your xml startup code here.
if (!xml_parse($xml_parser, $s_data, True))
 {
 die(sprintf("XML error: %s at line %d",
  xml_error_string(xml_get_error_code($xml_parser)),
  xml_get_current_line_number($xml_parser)));
 }

?>
0
 

Author Comment

by:andreni78
ID: 16921754
I entered that in a test.php file and it gave me this:
is that the only code I need to enter?
check it out here: www.bestultramobilepc.com/test.php

Warning: xml_parse(): supplied argument is not a valid XML Parser resource in /home/.papyrus/myaccount/mysite.COM/test.php on line 13

Warning: xml_get_error_code(): supplied argument is not a valid XML Parser resource in /home/.papyrus/myaccount/mysite.COM/test.php on line 16

Warning: xml_get_current_line_number(): supplied argument is not a valid XML Parser resource in /home/.papyrus/myaccount/mysite.COM/test.php on line 17
XML error: No error at line 0
0
 

Author Comment

by:andreni78
ID: 16929009
nobody knows the fix?
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 16932742
You didn't include your XML_parser creation code (assuming you just ran my script!).

All the start_element, end_element stuff.

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");

and the functions MAY be enough.
0
 

Author Comment

by:andreni78
ID: 16948687
what's the complete code i can use? i have very little programming experience :-/
0
 
LVL 40

Accepted Solution

by:
Richard Quadling earned 2000 total points
ID: 16949297
<?php

/*
Created by Global Syndication's RSS Parser
http://www.globalsyndication.com/rss-parser
*/

set_time_limit(0);

$file = "http://news.google.com/news?hl=en&ned=us&ie=UTF-8&q=soccer+moms&output=rss";

$rss_channel = array();
$currently_writing = "";
$main = "";
$item_counter = 0;

function startElement($parser, $name, $attrs) {
        global $rss_channel, $currently_writing, $main;
        switch($name) {
             case "RSS":
             case "RDF:RDF":
             case "ITEMS":
                  $currently_writing = "";
                  break;
             case "CHANNEL":
                  $main = "CHANNEL";
                  break;
             case "IMAGE":
                  $main = "IMAGE";
                  $rss_channel["IMAGE"] = array();
                  break;
             case "ITEM":
                  $main = "ITEMS";
                  break;
             default:
                  $currently_writing = $name;
                  break;
        }
}

function endElement($parser, $name) {
        global $rss_channel, $currently_writing, $item_counter;
        $currently_writing = "";
        if ($name == "ITEM") {
             $item_counter++;
        }
}

function characterData($parser, $data) {
     global $rss_channel, $currently_writing, $main, $item_counter;
     if ($currently_writing != "") {
          switch($main) {
               case "CHANNEL":
                    if (isset($rss_channel[$currently_writing])) {
                         $rss_channel[$currently_writing] .= $data;
                    } else {
                         $rss_channel[$currently_writing] = $data;
                    }
                    break;
               case "IMAGE":
                    if (isset($rss_channel[$main][$currently_writing])) {
                         $rss_channel[$main][$currently_writing] .= $data;
                    } else {
                         $rss_channel[$main][$currently_writing] = $data;
                    }
                    break;
               case "ITEMS":
                    if (isset($rss_channel[$main][$item_counter][$currently_writing])) {
                         $rss_channel[$main][$item_counter][$currently_writing] .= $data;
                    } else {
                         $rss_channel[$main][$item_counter][$currently_writing] = $data;
                    }
                    break;
          }
     }
}

$s_URL ='http://news.google.com/news?hl=en&ned=us&ie=UTF-8&q=soccer+moms&output=rss';

$r_curl = curl_init();
curl_setopt($r_curl, CURLOPT_URL, $s_URL);
curl_setopt($r_curl, CURLOPT_RETURNTRANSFER, 1);
$s_data = curl_exec($r_curl);
curl_close($r_curl);


$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!xml_parse($xml_parser, $s_data, True))
 {
 die(sprintf("XML error: %s at line %d",
  xml_error_string(xml_get_error_code($xml_parser)),
  xml_get_current_line_number($xml_parser)));
 }
xml_parser_free($xml_parser);

// output HTML
 //print ("<div class=\"channelname\">" . $rss_channel["TITLE"] . "</div>");

if (isset($rss_channel["ITEMS"])) {
     if (count($rss_channel["ITEMS"]) > 0) {
          // for($i = 0;$i < count($rss_channel["ITEMS"]);$i++) {
          for($i = 0;$i < 5;$i++) {
               if (isset($rss_channel["ITEMS"][$i]["LINK"])) {
               //print ("\n<div class=\"itemtitle\"><a href=\"" . $rss_channel["ITEMS"][$i]["LINK"] . "\">" . $rss_channel["ITEMS"][$i]["TITLE"] . "</a></div>");
               } else {
               print ("\n<div class=\"itemtitle\"><b>" . $rss_channel["ITEMS"][$i]["TITLE"] . "</b></div>");
               }
                print ("<div class=\"itemdescription\">" . $rss_channel["ITEMS"][$i]["DESCRIPTION"] . "</div>");           }
     } else {
          print ("<b>There are no new articles.</b>");
     }
}

?>

Produces output of ...

<div class="itemdescription"><br><table border=0 width= valign=top cellpadding=2 cellspacing=7><tr><td valign=top><a href="http://news.google.com/news/url?sa=T&ct=us/0-0&fd=R&url=http://politics.wizbangblog.com/2006/06/19/soccer-moms-then-nascar-dads-now.php&cid=1107385242&ei=IwKZRNnNFriOwQG3zZGRDw"><b>Soccer</b> <b>Moms</b>, then Nascar Dads, Now...</a><br><font size=-1><font color=#6f6f6f>Wizbang,&nbsp;DC&nbsp;-</font> <nobr>Jun 19, 2006</nobr></font><br><font size=-1>Are Weekly Wal-Mart Shoppers the new voting block candidates will be courting this election? In 2004, it was NASCAR dads. Before that, <b>soccer</b> <b>moms</b>. <b>...</b>  </font><br><font size=-1><a href="http://news.google.com/news/url?sa=T&ct=us/0-1&fd=R&url=http://www.thehill.com/thehill/export/TheHill/News/Campaign/062006_walmart.html&cid=1107385242&ei=IwKZRNnNFriOwQG3zZGRDw">Weekly Wal-Mart shoppers are midterms’ NASCAR dads</a> <font size=-1 color=#6f6f6f><nobr>The Hill</nobr></font></font><br><font class=p size=-1><a class=p href=http://news.google.com/news?hl=en&ned=us&ie=UTF-8&ncl=http://politics.wizbangblog.com/2006/06/19/soccer-moms-then-nascar-dads-now.php><nobr>all 2 related</nobr></a></font></table></div><div class="itemdescription"><br><table border=0 width= valign=top cellpadding=2 cellspacing=7><tr><td valign=top><a href="http://news.google.com/news/url?sa=T&ct=us/1-0&fd=R&url=http://www.sheboygan-press.com/apps/pbcs.dll/article%3FAID%3D/20060524/SHE04/605240620/1097/SHElife&cid=0&ei=IwKZRNnNFriOwQG3zZGRDw">Kids Today: Attention <b>soccer</b> <b>moms</b> and dads!</a><br><font size=-1><font color=#6f6f6f>Sheboygan Press,&nbsp;WI&nbsp;-</font> <nobr>May 24, 2006</nobr></font><br><font size=-1>We are planning to create photo galleries each day of the Sheboygan Area Youth <b>Soccer</b> Organization tournament May 27 and 28. Our <b>...</b>  </font><br></table></div><div class="itemdescription"><br><table border=0 width= valign=top cellpadding=2 cellspacing=7><tr><td valign=top><a href="http://news.google.com/news/url?sa=T&ct=us/2-0&fd=R&url=http://www.business-standard.com/common/storypage.php%3Fautono%3D95709%26leftnm%3D5%26subLeft%3D0%26chkFlg%3D&cid=0&ei=IwKZRNnNFriOwQG3zZGRDw">Business <b>moms</b></a><br><font size=-1><font color=#6f6f6f>Business Standard,&nbsp;India&nbsp;-</font> <nobr>13 hours ago</nobr></font><br><font size=-1>EDUCATION: Just as the world was getting used to <b>soccer</b> <b>moms</b>, we have power <b>moms</b> of another kind. Nidhi Singhania had a problem <b>...</b>  </font><br></table></div><div class="itemdescription"><br><table border=0 width= valign=top cellpadding=2 cellspacing=7><tr><td valign=top><a href="http://news.google.com/news/url?sa=T&ct=us/3-0&fd=R&url=http://www.crossrhythms.co.uk/articles/music/Mars_Ill_Rapping_And_The_Pain_Of_Sample_Clearing/22127/p1/&cid=0&ei=IwKZRNnNFriOwQG3zZGRDw">Mars Ill: Rapping And The Pain Of Sample Clearing</a><br><font size=-1><font color=#6f6f6f>Cross Rhythms,&nbsp;UK&nbsp;-</font> <nobr>22 hours ago</nobr></font><br><font size=-1><b>...</b> Most of them are directed towards, for lack of a better term, at &#39;<b>soccer</b> <b>moms</b>&#39;. You have a few like Radio U that really say, &#39;Okay <b>...</b>  </font><br></table></div><div class="itemdescription"><br><table border=0 width= valign=top cellpadding=2 cellspacing=7><tr><td valign=top><a href="http://news.google.com/news/url?sa=T&ct=us/4-0&fd=R&url=http://www.popmatters.com/columns/peterson/060620.shtml&cid=1107410024&ei=IwKZRNnNFriOwQG3zZGRDw">FROM THE CHEAP SEATS: The &quot;Other&quot; Football: Watching America <b>...</b></a><br><font size=-1><font color=#6f6f6f>PopMatters,&nbsp;IL&nbsp;-</font> <nobr>21 hours ago</nobr></font><br><font size=-1><b>...</b> league. It&#39;s so popular, in fact, that an entire demographic — the nation&#39;s &quot;<b>soccer</b> <b>moms</b>&quot; — has been coined by its practice. And <b>...</b>  </font><br></table></div>
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

670 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