converting fopen to cURL

Status
Not open for further replies.

cachemoney

New member
Sep 30, 2007
82
0
0
USA a OK
I have some PHP scripts that deal with fopen on a URL, however dreamhost has disabled fopen commands on URLs due to security reasons but can allow the use of cUrl functions to acheive the same results. Can some point me to a cheatsheet on how to rewrite an fopen call into a cURL call?

Code:
if ($f_cache and                          //are we caching?
    is_readable($cache_file_name) and     //file already readable in cache?
    $hf=fopen($cache_file_name,'r')) {    //can it be opened?
  $A=unserialize(fread($hf,filesize($cache_file_name)));
  fclose($hf);
}
else {
  $mtime1=getmicrotime();                    //time before Amazon
  if ($hf=fopen($file,'r')) {                //else open the file from Amazon
    for ($sfile='';$buf=fread($hf,1024);) {  //read the complete file (binary safe) 20040715: $sfile='' added
      $sfile.=$buf;
    }
    fclose($hf);
    $mtime2=getmicrotime();                 //time after Amazon
    if ($show_xml) {                        //print the raw XML?
      echo "<pre>\n";
      echo   htmlentities($sfile);          //print the file
      echo "</pre>\n";
      echo "<hr />\n";
    }

Thanks!
 


Code:
		$url="http://www.url.com/file-to-open";
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$output = curl_exec($ch);
		curl_close($ch);
		
		echo $output;

This should get you started.

Check out curl on PHP.net for more in depth info
 
PHP:
<?php

$ch = curl_init("http://www.example.com/");
$fp = fopen("example_homepage.txt", "w");

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch);
curl_close($ch);
fclose($fp);
?>
fetches Example Web Page
writes it to example_homepage.txt
you can then fopen example_homepage.txt locally
 
Status
Not open for further replies.