game.onenall.com,play free game online game.onenall.com,play free game online game.onenall.com,play free game online game.onenall.com,play free game online game.onenall.com,play free game online game.onenall.com,play free game online game.onenall.com,play free game online game.onenall.com,play free game online

Download file or web page using PHP cURL

Download file or web page using PHP cURL

I am discussion how to use cURL function of php to download  a web page and save it .

A typical PHP cURL usage follows the following sequence of steps.

curl_init – Initializes the session and returns a cURL handle which can be passed to other cURL functions.

curl_opt – This is the main work horse of cURL library. This function is called multiple times and specifies what we want the cURL library to do.

curl_exec – Executes a cURL session.

curl_close – Closes the current cURL session.

Below are some examples which should make the working of cURL more clearer.

The below PHP code uses cURL to download Google’s RSS feed.

<?php
/**
* Initialize the cURL session
*/
$ch = curl_init();
/**
* Set the URL of the page or file to download.
*/
curl_setopt($ch, CURLOPT_URL,
'http://news.google.com/news?hl=en&topic=t&output=rss');
/**
* Create a new file
*/
$fp = fopen('rss.xml', 'w');
/**
* Ask cURL to write the contents to a file
*/
curl_setopt($ch, CURLOPT_FILE, $fp);
/**
* Execute the cURL session
*/
curl_exec ($ch);
/**
* Close cURL session and file
*/
curl_close ($ch);
fclose($fp);
?>

SO enjoy php cURL function to save remote file.

thanks

Submitting forms using PHP cURL

To submit forms using cURL, we need to follow the below steps:

Prepare the data to be posted
Connect to the remote URL
Post (submit) the data
Fetch response and display it to the user

Prepare data to be posted

<?php
$data = array();
$data['first_name'] = 'Jatinder';
$data['last_name'] = 'Thind';
$data['password'] = 'secret';
$data['email'] = 'mrrrr@playfreegamesonly.com';
?>
So now we need to make the post data encodeing we will use php encoding to do so.Below function will do the same.
<?php
$post_str = '';
foreach($data as $key=>$val) {
	$post_str .= $key.'='.urlencode($val).'&';
}
$post_str = substr($post_str, 0, -1);
?>

Connect to the remote URL

Below is the function to connect to the remote server using curl
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://example.com/form-handler.php' );
?>

Post (submit) the form data

First we instruct cURL to a regular HTTP POST.
<?php
curl_setopt($ch, CURLOPT_POST, TRUE);
?>

Next we tell cURL which data to send in the HTTP POST.

<?php
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str);
?>

Execute request and fetch the response

<?php
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>

So using above function we can submit form to remote server or site using curl
thanks