Solved

use curl instead of fopen on this script

Posted on 2006-06-15
12
928 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
12 Comments
 
LVL 10

Expert Comment

by:ClickCentric
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:andreni78
Comment Utility
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:RQuadling
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:andreni78
Comment Utility
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
Comment Utility
nobody knows the fix?
0
 
LVL 40

Expert Comment

by:RQuadling
Comment Utility
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
Comment Utility
what's the complete code i can use? i have very little programming experience :-/
0
 
LVL 40

Accepted Solution

by:
RQuadling earned 500 total points
Comment Utility
<?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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now