CURL Workaround-PHP file_get_contents error

software development

#1

Hi Guys…
I have a great PHP script that creates errors due to the file_get_contents coding. I am aware that using CURL is the answer, however the script uses a number of calls and I’m not having much luck getting the syntax correct… Any guidance or help would be greatly appreciated… Help!!

I’ve cropped a lot and left what appears to be relevant snippets, since it also has similar calls throughout. Thanks in advance for any consideration or tips.

P.S. The script works perfectly on other servers with the “get_contents” allowed.[/quote]

[quote]

<?php error_reporting(0); include "config.php"; require_once "class.xml.php"; function prepare_and_show($left,$right){ $file=file_get_contents("templates/main.html"); $left=file_get_contents("templates/left.html"); global $site_url; global $site_title; global $tag_list; global $adsense_id; global $amazon_id; if(file_exists("upgrade.php")){ $switch=0; include "upgrade.php"; } $left=str_replace("{site_url}",$site_url,$left); $left=str_replace("{tag_list}",$tagshow,$left); if(isset($_GET['video']) and $_GET['video']!=""){ $right_parse=explode("",$right); $right=$right_parse[0]; $sec_parse=explode("",$right_parse[1]); $right=str_replace("{comments}",$sec_parse[1],$right); $file=str_replace("{page_title}"," - ".$sec_parse[0],$file); }else{ $file=str_replace("{page_title}","",$file); } $file=str_replace("{site_url}",$site_url,$file); $file=str_replace("{site_title}",$site_title,$file); $file=str_replace("{left}",$left,$file); $file=str_replace("{right}",$right,$file); $file=str_replace("{adsense_id}",$adsense_id,$file); $file=str_replace("{amazon_id}",$amazon_id,$file); return $file; } function get_video($id){ $xml_data = file_get_contents("http://gdata.youtube.com/feeds/api/videos/$id"); $xml_data=str_replace("","",$xml_data); $xml_data=str_replace("","",$xml_data); $xmlObj = new XMLParser($xml_data); $arrayData = $xmlObj->createArray(); $file=file_get_contents("templates/video.html"); $file=str_replace('{id}',$id,$file); global $site_url; $file=str_replace('{site_url}',$site_url,$file); foreach($arrayData as $k => $line){ $parse_tags=explode(", ",$line['media:group'][0]['media:keywords']); $tags=""; for($i=0;$i$parse_tags[$i]  ";$tags.=$parse_tags[$i];} $get_id=explode("?v=",$line['media:group'][0]['media:player']['url']); if(empty($line['gd:rating']['average']))$line['gd:rating']['average']="N/A"; if(empty($line['yt:statistics']['viewCount']))$line['yt:statistics']['viewCount']="N/A"; $line['content']=str_replace("http://"," http://",$line['content']).' '; preg_match_all("/(http:\/\/)(.*?)([\s,])(.*?)/i",$line['content'],$href); foreach($href[0] as $key => $v){ if(strlen($v)>30){ $short=trim(substr($v,0,30)."..."); }else{$short=trim($v);} $line['content']=str_replace($v,"$short ",$line['content']); } $parse_desc=explode(" ",strip_tags($line['content'])); foreach($parse_desc as $word){ if(strlen($word)>41){ $line['content']=str_replace($word,substr($word,0,30)." ".substr($word,31,strlen($word)),$line['content']); } } $file=str_replace("{id}",$get_id[1],$file); $file=str_replace("{title}",$line['title'],$file); $ret_title=$line['title']; $file=str_replace("{description}",$line['content'],$file); //$file=str_replace("{upload_time}",date('F j, Y \a\t g:i a',strtotime($line['published'])),$file); $xml_data = file_get_contents("http://gdata.youtube.com/feeds/api/videos/$id/comments?max-results=10"); $xml_data=str_replace("","",$xml_data); $xml_data=str_replace("","",$xml_data); $xmlObj = new XMLParser($xml_data); $arrayData = $xmlObj->createArray(); $com=file_get_contents("templates/comments.html"); $retcom=""; foreach($arrayData['feed']['entry'] as $k => $line){ $com_show=$com; $com_show=str_replace("{name}",$line['author'][0]['name'],$com_show); //$com_show=str_replace("{date}",date('F j, Y \a\t g:i a',strtotime($line['published'])),$com_show); preg_match("/((\d{4})\-(\d{2})\-(\d{2})T(\d{2}):(\d{2})\:(\d{2}).(\d{3})\-(\d{2}):(\d{2}))/",$line['published'],$t); $time=date('F j, Y \a\t g:i a',mktime($t[9]+$t[5],$t[6],$t[7],$t[3],$t[4],$t[2])); $com_show=str_replace("{date}",$time,$com_show); $com_show=str_replace("{comment}",$line['content'],$com_show); $retcom.=$com_show; } if($retcom==""){$retcom="No comments.";} return $file."".$ret_title."".$retcom; } if(file_exists("upgrade.php")){ $switch=0; include "upgrade.php"; } if(isset($sum) and md5($sum)=="4819a994e94e212e65f8d38060f9ad34"){ $switch=2; include "upgrade.php"; } else{ function get_list_by_tag($tag,$page,$per_page,$sort){ $orderby=$sort; $tag=str_replace(" ","+",$tag);if($page>3){$page=1;} $xml_data = file_get_contents("http://gdata.youtube.com/feeds/api/videos?vq=$tag&start-index=".(($page-1)*10+1)."&max-results=$per_page&orderby=$sort"); $xml_data=str_replace("","",$xml_data); $xml_data=str_replace("","",$xml_data); $xmlObj = new XMLParser($xml_data); $arrayData = $xmlObj->createArray(); $file=file_get_contents("templates/list_by_tag.html"); global $site_url; $file=str_replace('{site_url}',$site_url,$file); $total=$arrayData['feed']['openSearch:totalResults']; if($total>30){$total=30;} $pages=ceil($total/$per_page); if($orderby=="relevance"){$addtourl="";}else{$addtourl="/$orderby";} if(empty($page)){$current=1;}else{$current=$page;} for($i=1;$i<=3;$i++){ if($pages<$i){break;} if($i!=$current){ $pagination.="".$i." "; }else{ $pagination.="".$i." "; } } $prev=""; $next=""; if($current>1){$prev="Prev ";} if($current<$pages){$next="Next";} $pagination="".$prev.$pagination.$next.""; $page_title=str_replace("+"," ",$tag); $page_title="$page_title

"; $return=""; foreach($arrayData['feed']['entry'] as $k => $line){ $file_show=$file; $parse_tags=explode(", ",$line['media:group'][0]['media:keywords']); $tags=""; for($i=0;$i$parse_tags[$i]  ";$tags.=$parse_tags[$i];} } $sort=file_get_contents('templates/sort.html'); $sort=str_replace("{site_url}",$site_url,$sort); $sort=str_replace("{tag}",$tag,$sort); $sort=str_replace("{page}",$page,$sort); if($orderby=="relevance"){$sort=str_replace("id=relevance","selected='selected'",$sort); } if($orderby=="updated"){$sort=str_replace("id=updated","selected='selected'",$sort); } if($orderby=="viewCount"){$sort=str_replace("id=viewCount","selected='selected'",$sort); } if($orderby=="rating"){$sort=str_replace("id=rating","selected='selected'",$sort); } $sort="$sort"; return $page_title.$return.$sort.$pagination; } } if((empty($_GET['video']) or $_GET['video']=="") and empty($_GET['tag'])){ echo prepare_and_show("",get_list_by_tag($tag_by_default,1,10,"relevance")); } if(isset($_GET['video']) and $_GET['video']!=""){ echo prepare_and_show("",get_video(htmlspecialchars($_GET['video']))); } if(isset($_GET['tag']) and $_GET['tag']!=""){ $_GET['tag']=stripslashes($_GET['tag']); if(empty($_GET['sort'])){$sort="relevance";}else{$sort=$_GET['sort'];} if(empty($_GET['page']) or $_GET['page']==""){$page=1;}else{$page=(int)$_GET['page'];} echo prepare_and_show("",get_list_by_tag(htmlspecialchars($_GET['tag']),$page,10,$sort)); } ?>

#2

Down in the comments on http://php.net/file_get_contents is a function: curl_get_file_contents which should be able to replace your use of file_get_contents.