How to POST Form Data using CURL - By PHP Expert


Posting Form Data with cURL
CURL is a very useful tool for a web programmer to be able to fully interact with another website or page. It stands for Client URL (Library), and contains many powerful features when used with php.

<?php
$ch = curl_init( );
curl_setopt( $ch, CURLOPT_URL, "http://www.google.com" );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
$response = curl_exec( $ch );
print $response;
?>

Breakup:
Line 1: Create a curl handle and set it to the variable $ch
Line 2: Use the curl_setopt( ) function to set the url the script is targeting to google.com
Line 3: Use the parameter CURLOPT_RETURNTRANSFER to make the curl handle receive the response in a string when executed.
Line 4: Execute the curl script and put the response into $response.
Line 5: Output what google.com says.

Now, to add a pratical application of this incredibly powerful library. Use an HTTP POST request.

<?php

$ch = curl_init( );
curl_setopt( $ch, CURLOPT_URL, "http://www.google.com" );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, "username=Third_Degree&password=youwish" );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
$response = curl_exec( $ch );
print $response;

?>


These two lines are the power of this script
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, "username=Third_Degree&password=youwish" );

The URL specified earlier will now be dealing with the post fields username and password just like you submitted the form in your browser. This technique has an endless list of applications, and hopefully you will now have better control of the posting technique of cURL in PHP.

No comments

Enter your email address:

Delivered by FeedBurner

OR

 Subscribe in a reader

 
Latest Blogs
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Tips for optimizing php code by PHP Expert Important SERVER Variables in PHP - By PHP Expert Improved Error Messages in PHP 5 - By PHP Expert New Object Oriented Features - By PHP Expert New Object Oriented Features - By PHP Expert New Object Oriented Features - By PHP Expert New Object Oriented Features - By PHP Expert New Object Oriented Features - By PHP Expert New Object Oriented Features - By PHP Expert New Object Oriented Features - By PHP Expert Object Overloading in PHP 5 Persistable Classes - By PHP Expert Dynamic Getter/Setter Methods - By PHP Expert New Functions in PHP 5 New Directives - By PHP Expert Exception Handling - By PHP Expert Password Encryption in PHP - By PHP Expert Output Buffering in PHP - By PHP Expert Page Excerpts Using CURL - By PHP Expert Quick and Easy Google Site Search - By PHP Expert Always Be Notified When Google Crawls Your Site - By PHP Expert How to POST Form Data using CURL - By PHP Expert Cryptography for web developers - By PHP Expert Dynamically Loading JavaScript Files - by PHP Expert What is Web 2.0 - By PHP Expert