Using curl

software development

#1

I need to run something on my domain that
needs file_get_contents and realised that dreamhost recommends using curl instead of file_get_contents.
I went through the documentation for curl on dreamhost wiki and
changed my code like this:
The lines commented ae the old code which i replaced with curl
commands, as you can see below:
The complete file is at http://creditcards.grabline.com/test.html

//begin vivek code
//$str=file_get_contents($file_name);
$ch = curl_init();
$timeout = 20; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, $file_name);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$str = curl_exec($ch);
curl_close($ch);

//$ok=file_get_contents(“http://partners.credit-land.com/upload/$applic_id/show_ content.js”);
$ch2 = curl_init();
$timeout = 20; // set to zero for no timeout
curl_setopt ($ch2, CURLOPT_URL,
http://partners.credit-land.com/upload/$applic_id/show_content.js”);
curl_setopt ($ch2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch2, CURLOPT_CONNECTTIMEOUT, $timeout);
$ok = curl_exec($ch2);
curl_close($ch2);
//end vivek code

I am using php 5.0 on this site and don’t mind using php 4 if that is
what is causing this. How can I get this to work on dreamhost
environment (since file_get_contents is disabled)?
Even after changing my code to use curl, I am getting errors. Please help. to see the error, point your browser to http://creditcards.grabline.com/test.html
Regards
Vivek


#2

It looks like only some of your code is being parsed as PHP. Make sure you have opening and closing PHP tags where they are supposed to be.


Simon Jessey | Keystone Websites
Save $97 on yearly plans with promo code [color=#CC0000]SCJESSEY97[/color]


#3

Hope it is ok to post it here. the code in the prior post was a partial listing. Here is the complete code:

<?php unset($error); if($applic_id=='') { $COOK_VIS_ID = $_COOKIE['COOK_VIS_ID']; $COOK_VST_ID = $_COOKIE['COOK_VST_ID']; $COOK_LINK_ID = $_COOKIE['COOK_LINK_ID']; $COOK_SPEC_CLICK = $_COOKIE['COOK_SPEC_CLICK']; $COOK_CL_APN = $_COOKIE['COOK_CL_APN']; } $applic_id=1190; $path='http://sites.credit-land.com/239534/'; $path=trim($path); if(substr($path, -1)!='/') $path.='/'; $file_name=$path.'content.php'; $cont=$_GET['cont']; if($cont!='') $file_name.='?cont='.$cont.'&applic_id='.$applic_id. '&COOK_VIS_ID='.$COOK_VIS_ID.'&COOK_VST_ID='.$COOK_VST_ID.'&COOK_LINK_ID='.$COOK_LINK_ID. '&COOK_SPEC_CLICK='.$COOK_SPEC_CLICK.'&COOK_CL_APN='.$COOK_CL_APN; else $file_name.='?applic_id='. $applic_id.'&COOK_VIS_ID='.$COOK_VIS_ID.'&COOK_VST_ID='.$COOK_VST_ID.'&COOK_LINK_ID='.$COOK_LINK_ID. '&COOK_SPEC_CLICK='.$COOK_SPEC_CLICK.'&COOK_CL_APN='.$COOK_CL_APN; ob_start(); //begin vivek code //$str=file_get_contents($file_name); $ch = curl_init(); $timeout = 20; // set to zero for no timeout curl_setopt ($ch, CURLOPT_URL, $file_name); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $str = curl_exec($ch); curl_close($ch); //$ok=file_get_contents("http://partners.credit-land.com/upload/$applic_id/show_content.js"); $ch2 = curl_init(); $timeout = 20; // set to zero for no timeout curl_setopt ($ch2, CURLOPT_URL, "http://partners.credit-land.com/upload/$applic_id/show_content.js"); curl_setopt ($ch2, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch2, CURLOPT_CONNECTTIMEOUT, $timeout); $ok = curl_exec($ch2); curl_close($ch2); //end vivek code $tmp=str_replace('
', chr(13), ob_get_contents()); $error[]=$tmp; ob_end_clean(); if($ok) { if($tmp) { if(strpos($tmp, 'failed to open stream')>0) echo '

Open stream error. Please contact administrator partners@credit-land.com .

'; if(strpos($tmp, 'URL file-access is disabled')>0) echo '

Your server isn\'t able to work with remote files.

'; } $fp = @fopen($file_name, 'r'); @fclose($fp); if(count($GLOBALS['http_response_header'])) { foreach ($GLOBALS['http_response_header'] as $cook) { if(strpos($cook, 'COOK_VIS_ID=')>0) $COOK_VIS_ID = substr($cook, strpos($cook, 'COOK_VIS_ID=')+12, strpos($cook, ';') - strpos( $cook, 'COOK_VIS_ID=') - 12); if(strpos($cook, 'COOK_VST_ID=')>0) $COOK_VST_ID = substr($cook, strpos($cook, 'COOK_VST_ID=')+12, strpos($cook, ';') - strpos($cook, 'COOK_VST_ID=') - 12); if(strpos($cook, 'COOK_LINK_ID=')>0) $COOK_LINK_ID = substr($cook, strpos($cook, 'COOK_LINK_ID=')+13, strpos($cook, ';') - strpos($cook, 'COOK_LINK_ID=') - 13); if(strpos($cook, 'COOK_SPEC_CLICK=')>0) $COOK_SPEC_CLICK = substr($cook, strpos($cook, 'COOK_SPEC_CLICK=')+16, strpos($cook, ';') - strpos($cook, 'COOK_SPEC_CLICK=') - 16); if(strpos($cook, 'COOK_CL_APN=')>0) $COOK_CL_APN = substr($cook, strpos($cook, 'COOK_CL_APN=')+12, strpos($cook, ';') - strpos($cook, 'COOK_CL_APN=') - 12); } } if($str) { $str=preg_replace(array("/apply.php\?page_no=([0-9]+)&prd_id=([0-9]+)/", "/apply.php\?page_no=([0-9]+)&bnr_id=([0-9]+)/", "/[\.\/]*index.php/", "/[\.\/]*details\/details_page_([0-9]+)_([0-9]+)_([0-9]+).php\?page_no=([0-9]+)&prd_id=([0-9]+)/", "/[\.\/]*([a-z_0-9]+)\/([a-z_0-9]+)_page_([0-9]+)_([0-9]+).php/", "/[\.\/]*images\/logo.gif/", "/[\.\/]*images\/([a-zA-Z_0-9\/_-]+)\.gif/", "/[\.\/]*images\/([a-zA-Z_0-9\/_-]+)\.jpg/", "/[\.\/]*images\/([a-zA-Z_0-9\/_-]+)\.pic/", "/[\.\/]*default.css/", "/[\.\/]*images\/common.js/", "/prd_([0-9]+)_([0-9]+).pic/", "/[ ]{2,}/" ), array($path."apply.php?page_no=\\1&prd_id=\\2&applic_id=$applic_id". ($COOK_VIS_ID!='' ? '&COOK_VIS_ID='.$COOK_VIS_ID : ''). ($COOK_VST_ID!='' ? '&COOK_VST_ID='.$COOK_VST_ID : ''). ($COOK_LINK_ID!='' ? '&COOK_LINK_ID='.$COOK_LINK_ID : ''). ($COOK_SPEC_CLICK!='' ? '&COOK_SPEC_CLICK='.$COOK_SPEC_CLICK : ''). ($COOK_CL_APN!='' ? '&COOK_CL_APN='.$COOK_CL_APN : ''), $path."apply.php?page_no=\\1&bnr_id=\\2&applic_id=$applic_id". ($COOK_VIS_ID!='' ? '&COOK_VIS_ID='.$COOK_VIS_ID : ''). ($COOK_VST_ID!='' ? '&COOK_VST_ID='.$COOK_VST_ID : ''). ($COOK_LINK_ID!='' ? '&COOK_LINK_ID='.$COOK_LINK_ID : ''). ($COOK_SPEC_CLICK!='' ? '&COOK_SPEC_CLICK='. $COOK_SPEC_CLICK : ''). ($COOK_CL_APN!='' ? '&COOK_CL_APN='.$COOK_CL_APN : ''), "?cont=index.php&applic_id=$applic_id". ($COOK_VIS_ID!='' ? '&COOK_VIS_ID='.$COOK_VIS_ID : ''). ($COOK_VST_ID!='' ? '&COOK_VST_ID='.$COOK_VST_ID : ''). ($COOK_LINK_ID!='' ? '&COOK_LINK_ID='. $COOK_LINK_ID : ''). ($COOK_SPEC_CLICK!='' ? '&COOK_SPEC_CLICK='.$COOK_SPEC_CLICK : ''). ($COOK_CL_APN!='' ? '&COOK_CL_APN='.$COOK_CL_APN : ''), "?cont=details/details_page_\\1_\\2_\\3.php&page_no=\\4&prd_id=\\5&applic_id=$applic_id". ($COOK_VIS_ID!='' ? '&COOK_VIS_ID='.$COOK_VIS_ID : ''). ($COOK_VST_ID!='' ? '&COOK_VST_ID= '.$COOK_VST_ID : ''). ($COOK_LINK_ID!='' ? '&COOK_LINK_ID='.$COOK_LINK_ID : ''). ($COOK_SPEC_CLICK!='' ? '&COOK_SPEC_CLICK='.$COOK_SPEC_CLICK : ''). ($COOK_CL_APN!='' ? '&COOK_CL_APN='.$COOK_CL_APN : ''), "?cont=\\1/\\2_page_\\3_\\4.php&applic_id=$applic_id". ($COOK_VIS_ID!='' ? '&COOK_VIS_ID='.$COOK_VIS_ID : ''). ($COOK_VST_ID!='' ? '&COOK_VST_ID='. $COOK_VST_ID : ''). ($COOK_LINK_ID!='' ? '&COOK_LINK_ID='.$COOK_LINK_ID : ''). ($COOK_SPEC_CLICK!='' ? '&COOK_SPEC_CLICK='.$COOK_SPEC_CLICK : ''). ($COOK_CL_APN!='' ? '&COOK_CL_APN='.$COOK_CL_APN : ''), "http://partners.credit-land.com/upload/$applic_id/logo.gif", $path."images/\\1.gif", $path."images/\\1.jpg", $path."images/\\1.pic", $path."default.css", $path."images/common.js", '', ' ' ), $str); echo $str; } if(count($error)>1) { $error[]='FILE: '.$file_name; $error[]='PATH: '.$path; $error[]='APPLIC_ID: '.$applic_id; $error[]='VIS_ID: '.$COOK_VIS_ID; $error[]='VST_ID: '.$COOK_VST_ID; $error[]='PHP: '.phpversion(); $error[]='HTTP: '.@join(chr(13).chr(9), $GLOBALS['http_response_header']); $error[]='COOK: '.@join(chr(13).chr(9), $_COOKIE); @mail('partners@credit-land.com', 'Failed to open partners content '.$path, @join(chr(13), $error)); } echo " ";}; ?>

#4

Fixed the problem.
The website is now live at http://creditcards.grabline.com