Submit the PHP Form data without using CURL

Your PHP Server (HOSTING) doesn’t enabled CURL, then we can submit the data using PHP Function’s like fsockopen() and fputs()

Please see the below Sample Code:


//create array of data to be posted
$post_data['last_name'] = trim($_POST['name']);
$post_data['email'] = trim($_POST['email']);
$post_data['company'] = trim($_POST['company_name']);

//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
$post_items[] = $key . '=' . $value;

//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);

//we also need to add a question mark at the beginning of the string
$post_string = '?' . $post_string;

//we are going to need the length of the data string
$data_length = strlen($post_string);

//let's open the connection
$connection = fsockopen('', 80);

//sending the data
fputs($connection, "POST  /SOURCE_URL.php  HTTP/1.1\r\n");
fputs($connection, "Host: \r\n");
"Content-Type: application/x-www-form-urlencoded\r\n");
fputs($connection, "Content-Length: $data_length\r\n");
fputs($connection, "Connection: close\r\n\r\n");
fputs($connection, $post_string);

//closing the connection


Written by 

3x Salesforce MVP | Platform Champion | 18x Salesforce Certified | MuleSoft Certified | 8x Trailhead Ranger | TechForce Services | Sydney | Australia

Leave a Reply

Your email address will not be published.