Skip to content

Instantly share code, notes, and snippets.

@baamenabar
Created September 10, 2012 13:59
Show Gist options
  • Save baamenabar/3691096 to your computer and use it in GitHub Desktop.
Save baamenabar/3691096 to your computer and use it in GitHub Desktop.
PHP cURL get or save simple stuff
<?php
/****************************************************************************
PHP CURL functions for getting data or asytnchronously saving it to file.
It is header redirect tolerant. like the 302 http status.
Save data to file with cURL: save_data_to_file_w_curl($url,$pathToFile);
Get data with cURL get_data_w_curl($url);
/**
* MIT License
* ===========
*
* Copyright (c) 2012 Agustin Amenabar <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* @package [email protected]
* @subpackage
* @copyright 2012 Agustin Amenabar.
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* @link http://medula.cl/
*
****************************************************************************/
function save_data_to_file_w_curl($url,$fname) {
$timeout=5;
$path = $fname;
$fp = fopen($path, 'w');
$ch = curl_init($url);
//curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );//sometimes you may need this.
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_ENCODING, "" );
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ); # required for https urls
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );
curl_setopt( $ch, CURLOPT_MAXREDIRS, 10 );
$response = curl_exec($ch);
//echo print_r(curl_getinfo($ch),true);//just for checking
if(!$response)$response=print_r(curl_getinfo($ch),true);
curl_close($ch);
fclose($fp);
return $response;//true or array with CURL INFO
}
function get_data_w_curl($url) {
$timeout=5;
$ch = curl_init($url);
//curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );//you might need this in some ocations
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_ENCODING, "" );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ); # required for https urls
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );
curl_setopt( $ch, CURLOPT_MAXREDIRS, 10 );
$data = curl_exec($ch);
//echo print_r(curl_getinfo($ch),true);//for debugging
$toReturn=$data;
if(!$data)$toReturn=print_r(curl_getinfo($ch),true);
curl_close($ch);
return $data;
}
/*****************************************
USE EXAMPLE (un comment to test)
*******************************************/
//echo get_data_w_curl('http://www.example.com');//this gets the contents of http://www.iana.org/domains/example/ which is a redirect from example.com
//echo get_data_w_curl('http://www.example.com','./iana-example.html');//this gets the contents of http://www.iana.org/domains/example/ which is a redirect from example.com and saves them to iana-example.html
?>
@sim3dia
Copy link

sim3dia commented Oct 21, 2021

Useful, thanks

@AhmadRaza9
Copy link

Both functions are working well.
save_data_to_file_w_curl('http://www.example.com','./iana-example.html') //this gets the contents of http://www.iana.org/domains/example/ which is a redirect from example.com and saves them to iana-example.html

Just update your second fun name ๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜œ๐Ÿ˜œ๐Ÿ˜œ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment