Tuesday, April 3, 2012

How to make POST call in PHP using CURL

Ever wanted to make a POST or GET call to a URL without creating a form and submitting it? Well here is how you can do it using Curl, very simple.



<?php

// Submit those variables to the server
$postVals = urlencode("username")."=".urlencode('TomCruise');
$postVals.= "&";
$postVals.= urlencode("password")."=".urlencode('GhostProtocol');

// Send request to the required URL
$url = "http://www.b4blinky.com/index.php";

$curl = curl_init();
curl_setopt( $curl, CURLOPT_URL, $url );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER,1);
curl_setopt( $curl, CURLOPT_POSTFIELDS,  $postVals);
curl_setopt( $curl, CURLOPT_POST, 1);
$result = curl_exec( $curl );
curl_close( $curl );

?>

No comments:

Post a Comment