- Impact
- 32
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
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: