Do you need to post data to a website using PHP? It’s actually pretty easy to do. Here’s the code.
$urltopost = "http://somewebsite.com/script.php";
$datatopost = array (
"firstname" => "Mike",
"lastname" => "Lopez",
"email" => "my@email.com",
);
$ch = curl_init ($urltopost);
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $datatopost);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$returndata = curl_exec ($ch);
?>
And what happened? Here’s my quick explanation.
$urltopost
The url where you want to post your data to
$datatopost
The post data as an associative array. The keys are the post variables
$ch = curl_init ($urltopost);
Initializes cURL
curl_setopt ($ch, CURLOPT_POST, true);
Tells cURL that we want to send post data
curl_setopt ($ch, CURLOPT_POSTFIELDS, $datatopost);
Tells cURL what are post data is
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
Tells cURL to return the output of the post
$returndata = curl_exec ($ch);
Executes the cURL and saves theoutput in $returndata
There ya go! If you have questions, just ask in our forum.
Related Posts
Tags: PHP cURL, PHP Programming









Leave a Reply