How to modify the "Fopen" to use "CURL"

I have a peice of code that utilizes Fopen to import RSS news feeds onto my website.
I switch my hosting from Yahoo to GoDaddy.
GoDaddy does not support Fopen, they instead recommend I use CURL to import the page.

How can I rewrite this code to utililze CURL?

Here is the error "Warning: fopen(): URL file-access is disabled in the server configuration in /home/content/n/e/t/netwrkengeertd/html/free/attraction/inc/rss.inc on line 81

Warning: fopen(http://news.google.com/news?sourceid=navclient&ie=UTF-8&rls=GGLG,GGLG:2005-22,GGLG:en&q=Attraction&output=rss): failed to open stream: no suitable wrapper could be found in /home/content/n/e/t/html/free/inc/rss.inc on line 81
could not open XML input"
}
 
$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>");

Open in new window

marcumAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hernst42Commented:
Would go like this (untested):
$ch = curl_init("http://news.google.com/news?sourceid=navclient&ie=UTF-8&rls=GGLG,GGLG:2005-22,GGLG:en&q=Attraction&output=rss)");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURN_TRANSFER, 1);
$data = curl_exec($ch);
 
if (!xml_parse($xml_parser, $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);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
marcumAuthor Commented:
great I will test it.
0
marcumAuthor Commented:
I'm having a hard time integrating this section, here is the entire script, where what would I change?

Thanks
<?php
 
/*
Created by Global Syndication's RSS Parser
http://www.globalsyndication.com/rss-parser
*/
 
set_time_limit(0);
 
$file = "http://news.google.com/news?sourceid=navclient&ie=UTF-8&rls=GGLG,GGLG:2005-22,GGLG:en&q=Attraction&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++) {
			if (isset($rss_channel["ITEMS"][$i]["LINK"])) {
			print ("\n<div class=\"itemtitle\"><a href=\"" . "go.php?url=" . $rss_channel["ITEMS"][$i]["LINK"] . "\">" . $rss_channel["ITEMS"][$i]["TITLE"] . "</a></div>");
			} else {
			print ("\n<div class=\"itemtitle\">" . $rss_channel["ITEMS"][$i]["TITLE"] . "</div>");
			}
			 print ("<div class=\"itemdescription\">" . $rss_channel["ITEMS"][$i]["DESCRIPTION"] . "</div><br />"); 		}
	} else {
		print ("<b>There are no articles in this feed.</b>");
	}
}
 
?>

Open in new window

0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

hernst42Commented:
replace lin6 - 17 with my posted ocde
0
marcumAuthor Commented:
Here is how I intergrated it. and here is the error..

302 MovedThe document has moved here. XML error: syntax error at line 1

However the resource is not moved I can access it manually.
<?php
 
/*
Created by Global Syndication's RSS Parser
http://www.globalsyndication.com/rss-parser
*/
 
set_time_limit(0);
 
$file = "http://news.google.com/news?sourceid=navclient&ie=UTF-8&rls=GGLG,GGLG:2005-22,GGLG:en&q=Attraction&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");
$fp = curl_init("$file");
curl_setopt($fp, CURLOPT_HEADER, 0);
curl_setopt($fp, CURLOPT_RETURN_TRANSFER, 1);
$data = curl_exec($fp);
 
if (!xml_parse($xml_parser, $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++) {
			if (isset($rss_channel["ITEMS"][$i]["LINK"])) {
			print ("\n<div class=\"itemtitle\"><a href=\"" . "go.php?url=" . $rss_channel["ITEMS"][$i]["LINK"] . "\">" . $rss_channel["ITEMS"][$i]["TITLE"] . "</a></div>");
			} else {
			print ("\n<div class=\"itemtitle\">" . $rss_channel["ITEMS"][$i]["TITLE"] . "</div>");
			}
			 print ("<div class=\"itemdescription\">" . $rss_channel["ITEMS"][$i]["DESCRIPTION"] . "</div><br />"); 		}
	} else {
		print ("<b>There are no articles in this feed.</b>");
	}
}
 
?>

Open in new window

0
marcumAuthor Commented:
OK so the problem is that google is not allowing the request. Here is the error..

Google  Error  
We're sorry...
... but your query looks similar to automated requests from a computer virus or spyware application. To protect our users, we can't process your request right now.

Is there anyway around this?
0
marcumAuthor Commented:
Great job and fast answer
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.