php curl post data – How To POST JSON Data with PHP cURL?

php curl post data – A POST request is usually made to send user collected data to a server. Finally, we’ve used the curl_exec function to execute the cURL request.

php curl post data – PHP + curl, HTTP POST sample code

In this article, you will learn how to post JSON data using cURL in PHP. PHP cURL command is usually used to transfer data to and from a server.

PHP: Curl Post Fields or Raw data

Using Curl to Post Fields

These 2 main source code examples display how to send and get all the data as individual fields.

send_post_fields.php

 'Users@8956',
    'password' => 'Pass@#$dsjk'
);

$query_string = http_build_query($params);

$my_crl = curl_init($url);

curl_setopt($my_crl, CURLOPT_POST, true);
curl_setopt($my_crl, CURLOPT_POSTFIELDS, $query_string);
curl_setopt($my_crl, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($my_crl);

curl_close($my_crl);

print_r($result);

don’t Miss : Curl GET And POST Method Calls – PHP Example

get_all_the_products_data.php


Using Curl to Post Raw Data

get_all_the_products_data.php

 'pakainfo_v1',
    'password' => 'paka#$%jfkdkjf'
);

$body = json_encode($params);

$my_crl = curl_init($url);

curl_setopt($my_crl, CURLOPT_POSTFIELDS, $body);
curl_setopt($my_crl, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($my_crl, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($my_crl);

curl_close($my_crl);

print_r($result);

get_all_the_products_data.php


php curl post data Example

A sample PHP Script to POST data using cURL with create a POST request with PHP cURL.

 'pakainfo',
    'password' => '987654321'
);

$payload = json_encode($data);

$my_curl = curl_init('https://api.domain-name.com/api/1.0/user/login');
curl_setopt($my_curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($my_curl, CURLINFO_HEADER_OUT, true);
curl_setopt($my_curl, CURLOPT_POST, true);
curl_setopt($my_curl, CURLOPT_POSTFIELDS, $payload);

curl_setopt($my_curl, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($payload))
);

$result = curl_exec($my_curl);
curl_close($my_curl);

?>

Execute a HTTP POST Using PHP CURL

set the url, number of POST vars, POST data

$url = 'http://domain-name.com/get-post.php';
$params = array(
	'title' => urlencode($_POST['title']),
	'author' => urlencode($_POST['author_name']),
	'rank' => urlencode($_POST['rank']),
	'description' => urlencode($_POST['description']),
	'domain' => urlencode($_POST['domain']),
	'email' => urlencode($_POST['email']),
	'mobile' => urlencode($_POST['mobile'])
);


foreach($params as $key=>$value) { $params_string .= $key.'='.$value.'&'; }
rtrim($params_string, '&');


$curl_ex = curl_init();

curl_setopt($curl_ex,CURLOPT_URL, $url);
curl_setopt($curl_ex,CURLOPT_POST, count($params));
curl_setopt($curl_ex,CURLOPT_POSTFIELDS, $params_string);

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

//close connection
curl_close($curl_ex);

I hope you get an idea about php curl post data.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment