note 83541 modified in curl.examples by danbrown

From: Date: Sat, 07 Nov 2009 18:47:51 +0000
Subject: note 83541 modified in curl.examples by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-162495@lists.php.net to get a copy of this message
Basic cURL file or page download with basic error trapping. <?php function cURLcheckBasicFunctions() { if( !function_exists("curl_init") && !function_exists("curl_setopt") && !function_exists("curl_exec") && !function_exists("curl_close") ) return false; else return true; } /* * Returns string status information. * Can be changed to int or bool return types. */ function cURLdownload($url, $file) { if( !cURLcheckBasicFunctions() ) return "UNAVAILABLE: cURL Basic Functions"; $ch = curl_init(); if($ch) { $fp = fopen($file, "w"); if($fp) { if( !curl_setopt($ch, CURLOPT_URL, $url) ) { fclose($fp); // to match fopen() curl_close($ch); // to match curl_init() return "FAIL: curl_setopt(CURLOPT_URL)"; } if( !curl_setopt($ch, CURLOPT_FILE, $fp) ) return "FAIL: curl_setopt(CURLOPT_FILE)"; if( !curl_setopt($ch, CURLOPT_HEADER, 0) ) return "FAIL: curl_setopt(CURLOPT_HEADER)"; if( !curl_exec($ch) ) return "FAIL: curl_exec()"; curl_close($ch); fclose($fp); return "SUCCESS: $file [$url]"; } else return "FAIL: fopen()"; } else return "FAIL: curl_init()"; } // Download from 'example.com' to 'example.txt' echo cURLdownload("http://www.example.com", "example.txt"); ?> - JLèé --was-- Basic cURL file or page download with basic error trapping. <?php function cURLcheckBasicFunctions() { if( !function_exists("curl_init") && !function_exists("curl_setopt") && !function_exists("curl_exec") && !function_exists("curl_close") ) return false; else return true; } /* * Returns string status information. * Can be changed to int or bool return types. */ function cURLdownload($url, $file) { if( !cURLcheckBasicFunctions() ) return "UNAVAILABLE: cURL Basic Functions"; $ch = curl_init(); if($ch) { $fp = fopen($file, "w"); if($fp) { if( !curl_setopt($ch, CURLOPT_URL, $url) ) return "FAIL: curl_setopt(CURLOPT_URL)"; if( !curl_setopt($ch, CURLOPT_FILE, $fp) ) return "FAIL: curl_setopt(CURLOPT_FILE)"; if( !curl_setopt($ch, CURLOPT_HEADER, 0) ) return "FAIL: curl_setopt(CURLOPT_HEADER)"; if( !curl_exec($ch) ) return "FAIL: curl_exec()"; curl_close($ch); fclose($fp); return "SUCCESS: $file [$url]"; } else return "FAIL: fopen()"; } else return "FAIL: curl_init()"; } // Download from 'example.com' to 'example.txt' echo cURLdownload("http://www.example.com", "example.txt"); ?> - JLèé http://php.net/manual/en/curl.examples.php

« previous php.notes (#162495) next »