IT.COM

[Resolved] Fopen to curl help

Spaceship Spaceship
Watch
fopen to curl help

I've been searching on google, I can't really find a good resource to convert fopen to CURL.

Any one know of a easier to understand resource? Below is the file, any help is much appreciated!

-Ryan

Code:
<?php
if (!defined('SMF'))
	die('Hacking attempt...');

// Globals
$feedcount = 0;
$maxitemcount = 0;
$tag = '';
$insideitem = false;

function verify_rss_url($url)
{
	global $txt;

	// Rss Data storage
	$finalrss = '';
	$failed = true;

	$fp2 = fopen($url, "r");
	if ($fp2)
	{
		$failed = false;
		$contents = '';
		while (!feof($fp2))
		{
		  $contents .= fread($fp2, 8192);
		}

		fclose($fp2);

		$finalrss = $contents;
	}


	// Use Fsockopen
	if($failed == true)
	{
		$url_array = parse_url($url);

		$fp = fsockopen($url_array['host'], 80, $errno, $errstr, 30);
		if (!$fp)
		{

		}
		else
		{
			$failed = false;

		   $out = "GET " . $url_array['path'] . @$url_array['query'] . "  HTTP/1.1\r\n";
		   $out .= "Host: " . $url_array['host'] . "\r\n";
		   $out .= "Connection: Close\r\n\r\n";

		   fwrite($fp, $out);

		   $rssdata = '';

		   while (!feof($fp))
		   {
		       $rssdata .= fgets($fp, 128);
		   }
		   fclose($fp);

		   // Get rid of the stupid header information! Wish the function did it for me.
		   $rss2 = explode("\\r\\", $rssdata);
		   @$finalrss = @$rss2[1];

		}
	}

	// Use cURL
	if($failed == true)
	{
		if(function_exists("curl_init"))
		{
			$failed = false;
			// Last but not least try cUrl
			$ch = curl_init();

			// set URL and other appropriate options
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

			// grab URL, and return output
			$output = curl_exec($ch);

			// close curl resource, and free up system resources
			curl_close($ch);
			return $output;
		}
	}



	// XML Parser functions to verify the XML Feed
	if($failed == false)
	{
		$depth = array();

		$xml_parser = xml_parser_create();
		xml_set_element_handler($xml_parser, "startElement2", "endElement2");


		   if (!xml_parse($xml_parser, $finalrss)) {
		      fatal_error(sprintf($txt['feedposter_err_xmlerror'],
		                   xml_error_string(xml_get_error_code($xml_parser)),
		                   xml_get_current_line_number($xml_parser)), false);
		   }

		xml_parser_free($xml_parser);
	}
	else
	{
		// We were not able to download the feed :(
		fatal_error($txt['feedposter_err_nodownload'], false);
	}


}


function startElement2($parser, $name, $attrs)
{
   global $depth;
   $depth[$parser]++;
}

function endElement2($parser, $name)
{
   global $depth;
   $depth[$parser]--;
}

function UpdateRSSFeedBots()
{
	global $db_prefix, $context, $sourcedir, $feedcount, $func, $maxitemcount, $insideitem, $tag, $modSettings;

	// First get all the enabled bots
	$context['feeds'] = array();
	$request = db_query("
			SELECT
				ID_FEED, ID_BOARD, feedurl, title, postername, updatetime, enabled, html,
				ID_MEMBER, locked, articlelink, topicprefix, numbertoimport, importevery
			FROM {$db_prefix}feedbot
			WHERE enabled = 1", __FILE__, __LINE__);

	while ($row = mysql_fetch_assoc($request))
	{
		$context['feeds'][] = array(
			'ID_FEED' => $row['ID_FEED'],
			'ID_BOARD' => $row['ID_BOARD'],
			'feedurl' => $row['feedurl'],
			'title' => $row['title'],
			'postername' => $row['postername'],
			'enabled' => $row['enabled'],
			'html' => $row['html'],
			'ID_MEMBER' => $row['ID_MEMBER'],
			'locked' => $row['locked'],
			'articlelink' => $row['articlelink'],
			'topicprefix' => $row['topicprefix'],
			'numbertoimport' => $row['numbertoimport'],
			'importevery' => $row['importevery'],
			'updatetime' => $row['updatetime']
		);
	}

	mysql_free_result($request);

	// For the createPost function
	require_once($sourcedir . '/Subs-Post.php');

	// Check if a field expired
	foreach ($context['feeds'] as $key => $feed)
	{

		$current_time = time();


		// If the feedbot time to next import has expired
		if (($current_time + mktime(0, $feed['importevery'])) > $feed['updatetime'])
		//if( 1 == 1)
		{

			$feeddata = GetRSSData($feed['feedurl']);



			if ($feeddata != false)
			{

				// Process the XML
					$xml_parser = xml_parser_create();
					$context['feeditems'] = array();
					$feedcount = 0;
					$maxitemcount = $feed['numbertoimport'];
					$tag = '';
					$insideitem = false;
					$context['feeditems'][0] = array();
					$context['feeditems'][0][] = array();
					$context['feeditems'][0]['title'] = '';
					$context['feeditems'][0]['description'] = '';
					$context['feeditems'][0]['link'] = '';



					xml_set_element_handler($xml_parser, "startElement1", "endElement1");
					xml_set_character_data_handler($xml_parser, "characterData1");

					if (!xml_parse($xml_parser, $feeddata))
					 {
						// Error reading xml data

					     xml_parser_free($xml_parser);
					 }
					else
					{
					   	// Data must be valid lets extra some information from it
					   	// RSS Feeds are a list of items that might contain title, description, and link


					   	// Free the xml parser memory
						xml_parser_free($xml_parser);

						// Loop though all the items

						for ($i = 0; $i < ($maxitemcount); $i++)
						{
							// Check feed Log
							// Generate the hash for the log
							if(!isset($context['feeditems'][$i]['title']) || !isset($context['feeditems'][$i]['description']) || !isset($context['feeditems'][$i]['link']))
								continue;


							$itemhash = md5($context['feeditems'][$i]['title'] . $context['feeditems'][$i]['description']);
							$request = db_query("
							SELECT
								feedtime
							FROM {$db_prefix}feedbot_log
							WHERE feedhash = '$itemhash'", __FILE__, __LINE__);

							mysql_freeresult($request);

							// If no has has found that means no duplicate entry
							if (db_affected_rows() == 0)
							{

								// Create the Post
								$msg_title = $func['htmlspecialchars'](($feed['html'] ? $context['feeditems'][$i]['title'] : strip_tags($context['feeditems'][$i]['title'])), ENT_QUOTES);

								$msg_body =  $func['htmlspecialchars'](($feed['html'] ? $context['feeditems'][$i]['description'] . "\n\n" . $context['feeditems'][$i]['link']  : strip_tags($context['feeditems'][$i]['description'] .  "\n\n" . $context['feeditems'][$i]['link'])), ENT_QUOTES);



								$msgOptions = array(
									'id' => 0,
									'subject' => $feed['topicprefix'] . $msg_title,
									'body' => '[b]' . $msg_title . "[/b]\n\n" . $msg_body,
									'icon' => 'xx',
									'smileys_enabled' => 1,
									'attachments' => array(),
								);
								$topicOptions = array(
									'id' => 0,
									'board' => $feed['ID_BOARD'],
									'poll' => null,
									'lock_mode' => $feed['locked'],
									'sticky_mode' => null,
									'mark_as_read' => true,
								);
								$posterOptions = array(
									'id' => $feed['ID_MEMBER'],
									'name' => $feed['postername'],
									'email' => '',
									'update_post_count' => (($feed['ID_MEMBER'] == 0) ? 0 : 1),
								);

								createPost($msgOptions, $topicOptions, $posterOptions);

								// Add Feed Log
								$fid = $feed['ID_FEED'];
								$ftime = time();

								db_query("
								INSERT INTO {$db_prefix}feedbot_log
									(ID_FEED, feedhash, feedtime)
								VALUES
									($fid,'$itemhash',$ftime)", __FILE__, __LINE__);

							}
						}

					 } // End valid XML check



			}  // End get feed data

		} // End expire check


	} // End for each feed

}

function GetRSSData($url)
{
	$url_array = parse_url($url);

	$fp2 = fopen($url, "r");
	if ($fp2)
	{
		$contents = '';
		while (!feof($fp2))
		{
		  $contents .= fread($fp2, 8192);
		}

		fclose($fp2);

		return $contents;
	}


	$fp = fsockopen($url_array['host'], 80, $errno, $errstr, 30);
	if (!$fp)
	{

	}
	else
	{


	   $out = "GET " . $url_array['path'] . @$url_array['query'] . "  HTTP/1.1\r\n";
	   $out .= "Host: " . $url_array['host'] . "\r\n";
	   $out .= "Connection: Close\r\n\r\n";

	   fwrite($fp, $out);

	   $rssdata = '';

	   while (!feof($fp))
	   {
	       $rssdata .= fgets($fp, 128);
	   }
	   fclose($fp);

	   // Get rid of the stupid header information! Wish the function did it for me.
	   $rss2 = explode("\\r\\", $rssdata);
	   $finalrss = $rss2[1];

	   return  $finalrss;
	}

	if(function_exists("curl_init"))
	{
		// Last but not least try cUrl
		$ch = curl_init();

		// set URL and other appropriate options
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

		// grab URL, and return output
		$output = curl_exec($ch);

		// close curl resource, and free up system resources
		curl_close($ch);
		return $output;
	}


	// Failure return false
	return false;

}

function startElement1($parser, $name, $attrs)
 {
	global $insideitem, $tag;
	if ($insideitem)
	{
		$tag = $name;
	}
	elseif ($name == "ITEM")
	{
		$insideitem = true;
	}
}

function endElement1($parser, $name)
{
	global $insideitem, $tag, $feedcount, $context;

	if ($name == "ITEM")
	{
		$feedcount++;
		$context['feeditems'][$feedcount] = array();
		$context['feeditems'][$feedcount][] = array();
		$context['feeditems'][$feedcount]['title'] = '';
		$context['feeditems'][$feedcount]['description'] = '';
		$context['feeditems'][$feedcount]['link'] = '';
		$insideitem = false;
	}
}

function characterData1($parser, $data)
 {
	global $insideitem, $tag,  $feedcount, $context, $maxitemcount;

	if ($insideitem && $feedcount < $maxitemcount)
 	{
		switch ($tag)
		{
			case "TITLE":
				$context['feeditems'][$feedcount]['title'] .= $data;
			break;

			case "DESCRIPTION":
				$context['feeditems'][$feedcount]['description'] .= $data;

			break;

			case "LINK":
				$context['feeditems'][$feedcount]['link'] .= $data;
			break;
		}
	}
}
?>
 
Last edited:
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Your script tries fopen() first. If that fails, it tries fsockopen(). Finally if both fail, it will try curl.

Now since you want to convert fopen() to curl, I'm assuming you have fopen() disabled on your server and it gives an error when executed.
You don't really have to "convert" cause the curl code is already in there. Just remove the fopen() and fsockopen()

Here's the code with fopen() and fsockopen() removed. Hope it works.
Code:
<?php
if (!defined('SMF'))
	die('Hacking attempt...');

// Globals
$feedcount = 0;
$maxitemcount = 0;
$tag = '';
$insideitem = false;

function verify_rss_url($url)
{
	global $txt;

	// Rss Data storage
	$finalrss = '';
	$failed = true;


	// Use cURL
	if($failed == true)
	{
		if(function_exists("curl_init"))
		{
			$failed = false;
			// Last but not least try cUrl
			$ch = curl_init();

			// set URL and other appropriate options
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

			// grab URL, and return output
			$output = curl_exec($ch);

			// close curl resource, and free up system resources
			curl_close($ch);
			return $output;
		}
	}



	// XML Parser functions to verify the XML Feed
	if($failed == false)
	{
		$depth = array();

		$xml_parser = xml_parser_create();
		xml_set_element_handler($xml_parser, "startElement2", "endElement2");


		   if (!xml_parse($xml_parser, $finalrss)) {
		      fatal_error(sprintf($txt['feedposter_err_xmlerror'],
		                   xml_error_string(xml_get_error_code($xml_parser)),  
		                   xml_get_current_line_number($xml_parser)), false);
		   }

		xml_parser_free($xml_parser);
	}
	else
	{
		// We were not able to download the feed :(
		fatal_error($txt['feedposter_err_nodownload'], false);
	}


}


function startElement2($parser, $name, $attrs)
{
   global $depth;
   $depth[$parser]++;
}

function endElement2($parser, $name)
{
   global $depth;
   $depth[$parser]--;
}

function UpdateRSSFeedBots()
{
	global $db_prefix, $context, $sourcedir, $feedcount, $func, $maxitemcount, $insideitem, $tag, $modSettings;

	// First get all the enabled bots
	$context['feeds'] = array();
	$request = db_query("
			SELECT
				ID_FEED, ID_BOARD, feedurl, title, postername, updatetime, enabled, html,
				ID_MEMBER, locked, articlelink, topicprefix, numbertoimport, importevery
			FROM {$db_prefix}feedbot
			WHERE enabled = 1", __FILE__, __LINE__);

	while ($row = mysql_fetch_assoc($request))
	{
		$context['feeds'][] = array(
			'ID_FEED' => $row['ID_FEED'],
			'ID_BOARD' => $row['ID_BOARD'],
			'feedurl' => $row['feedurl'],
			'title' => $row['title'],
			'postername' => $row['postername'],
			'enabled' => $row['enabled'],
			'html' => $row['html'],
			'ID_MEMBER' => $row['ID_MEMBER'],
			'locked' => $row['locked'],
			'articlelink' => $row['articlelink'],
			'topicprefix' => $row['topicprefix'],
			'numbertoimport' => $row['numbertoimport'],
			'importevery' => $row['importevery'],
			'updatetime' => $row['updatetime']
		);
	}

	mysql_free_result($request);

	// For the createPost function
	require_once($sourcedir . '/Subs-Post.php');

	// Check if a field expired
	foreach ($context['feeds'] as $key => $feed)
	{

		$current_time = time();


		// If the feedbot time to next import has expired
		if (($current_time + mktime(0, $feed['importevery'])) > $feed['updatetime'])
		//if( 1 == 1)
		{

			$feeddata = GetRSSData($feed['feedurl']);



			if ($feeddata != false)
			{

				// Process the XML
					$xml_parser = xml_parser_create();
					$context['feeditems'] = array();
					$feedcount = 0;
					$maxitemcount = $feed['numbertoimport'];
					$tag = '';
					$insideitem = false;
					$context['feeditems'][0] = array();
					$context['feeditems'][0][] = array();
					$context['feeditems'][0]['title'] = '';
					$context['feeditems'][0]['description'] = '';
					$context['feeditems'][0]['link'] = '';



					xml_set_element_handler($xml_parser, "startElement1", "endElement1");
					xml_set_character_data_handler($xml_parser, "characterData1");

					if (!xml_parse($xml_parser, $feeddata))
					 {
						// Error reading xml data

					     xml_parser_free($xml_parser);
					 }
					else
					{
					   	// Data must be valid lets extra some information from it
					   	// RSS Feeds are a list of items that might contain title, description, and link


					   	// Free the xml parser memory
						xml_parser_free($xml_parser);

						// Loop though all the items

						for ($i = 0; $i < ($maxitemcount); $i++)
						{
							// Check feed Log
							// Generate the hash for the log
							if(!isset($context['feeditems'][$i]['title']) || !isset($context['feeditems'][$i]['description']) || !isset($context['feeditems'][$i]['link']))
								continue;


							$itemhash = md5($context['feeditems'][$i]['title'] . $context['feeditems'][$i]['description']);
							$request = db_query("
							SELECT
								feedtime
							FROM {$db_prefix}feedbot_log
							WHERE feedhash = '$itemhash'", __FILE__, __LINE__);

							mysql_freeresult($request);

							// If no has has found that means no duplicate entry
							if (db_affected_rows() == 0)
							{

								// Create the Post
								$msg_title = $func['htmlspecialchars'](($feed['html'] ? $context['feeditems'][$i]['title'] : strip_tags($context['feeditems'][$i]['title'])), ENT_QUOTES);

								$msg_body =  $func['htmlspecialchars'](($feed['html'] ? $context['feeditems'][$i]['description'] . "\n\n" . $context['feeditems'][$i]['link']  : strip_tags($context['feeditems'][$i]['description'] .  "\n\n" . $context['feeditems'][$i]['link'])), ENT_QUOTES);



								$msgOptions = array(
									'id' => 0,
									'subject' => $feed['topicprefix'] . $msg_title,
									'body' => '' . $msg_title . "\n\n" . $msg_body,
									'icon' => 'xx',
									'smileys_enabled' => 1,
									'attachments' => array(),
								);
								$topicOptions = array(
									'id' => 0,
									'board' => $feed['ID_BOARD'],
									'poll' => null,
									'lock_mode' => $feed['locked'],
									'sticky_mode' => null,
									'mark_as_read' => true,
								);
								$posterOptions = array(
									'id' => $feed['ID_MEMBER'],
									'name' => $feed['postername'],
									'email' => '',
									'update_post_count' => (($feed['ID_MEMBER'] == 0) ? 0 : 1),
								);

								createPost($msgOptions, $topicOptions, $posterOptions);

								// Add Feed Log
								$fid = $feed['ID_FEED'];
								$ftime = time();

								db_query("
								INSERT INTO {$db_prefix}feedbot_log
									(ID_FEED, feedhash, feedtime)
								VALUES
									($fid,'$itemhash',$ftime)", __FILE__, __LINE__);

							}
						}

					 } // End valid XML check



			}  // End get feed data

		} // End expire check


	} // End for each feed

}

function GetRSSData($url)
{
	$url_array = parse_url($url);

	if(function_exists("curl_init"))
	{
		// Last but not least try cUrl
		$ch = curl_init();

		// set URL and other appropriate options
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

		// grab URL, and return output
		$output = curl_exec($ch);

		// close curl resource, and free up system resources
		curl_close($ch);
		return $output;
	}


	// Failure return false
	return false;

}

function startElement1($parser, $name, $attrs)
 {
	global $insideitem, $tag;
	if ($insideitem)
	{
		$tag = $name;
	}
	elseif ($name == "ITEM")
	{
		$insideitem = true;
	}
}

function endElement1($parser, $name)
{
	global $insideitem, $tag, $feedcount, $context;

	if ($name == "ITEM")
	{
		$feedcount++;
		$context['feeditems'][$feedcount] = array();
		$context['feeditems'][$feedcount][] = array();
		$context['feeditems'][$feedcount]['title'] = '';
		$context['feeditems'][$feedcount]['description'] = '';
		$context['feeditems'][$feedcount]['link'] = '';
		$insideitem = false;
	}
}

function characterData1($parser, $data)
 {
	global $insideitem, $tag,  $feedcount, $context, $maxitemcount;

	if ($insideitem && $feedcount < $maxitemcount)
 	{
		switch ($tag)
		{
			case "TITLE":
				$context['feeditems'][$feedcount]['title'] .= $data;
			break;

			case "DESCRIPTION":
				$context['feeditems'][$feedcount]['description'] .= $data;

			break;

			case "LINK":
				$context['feeditems'][$feedcount]['link'] .= $data;
			break;
		}
	}
}
?>
 
1
•••
0
•••
you can even disable the error reporting by writing code
error_reporting(0);
to disable the error message only!!
 
0
•••
_khAttAm_ said:
you can even disable the error reporting by writing code
error_reporting(0);
to disable the error message only!!

I think it is not advisable to disable the error reporting...try to fix the errors if there are any...IMHO
 
0
•••
If you want to keep the fopen() code in the script, you could just comment it out. Add // before the lines you want to remove. Or enclose the code as a multiline comment using /* code */
 
0
•••
newsiness said:
I think it is not advisable to disable the error reporting...try to fix the errors if there are any...IMHO

Agreed 100%. If you ignore errors you could find that your script breaks. An error can affect many things.
 
0
•••
Thanks! Rep added, works great.
 
0
•••
In this case

peter@flexiwebhost said:
Agreed 100%. If you ignore errors you could find that your script breaks. An error can affect many things.
Yeah, but In this case, the errors are handled properly and if error occours, another option is automatically chosen, so ignoring errors should not be a problem....
 
0
•••
0
•••
Danltn said:
That's nice code - Is it public domain, and if not - what license?

Dan :tu:
Thank you, Dan. It is my domain. I coded it, i don't know about license (GPL i think). But you can use my curl code :tu:

Kurniawan.
 
Last edited:
0
•••
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back