Execute a HTTP POST Using PHP CURL

Posted by w3diy - 5 months ago
//extract data from the post
//set POST variables
$url = 'http://domain.com/get-post.php';
$all_data = array(
    'lname' => urlencode($_POST['pname']),
    'fname' => urlencode($_POST['pdesc']),
    'title' => urlencode($_POST['seo_title']),
    'company' => urlencode($_POST['information']),
    'price' => urlencode($_POST['price']),
    'code' => urlencode($_POST['code']),
    'rate' => urlencode($_POST['rate'])
);

//url-ify the data for the POST
foreach($all_data as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($all_data));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);