Sunday, April 28, 2024
HomePHPPHP Curl POST JSON Ship Request Information

PHP Curl POST JSON Ship Request Information


by Vincy. Final modified on October thirteenth, 2022.

A lot of the APIs are used to simply accept requests and ship responses in JSON format. JSON is the de-facto knowledge alternate format. You will need to discover ways to ship JSON request knowledge with an API name.

The cURL is a means of distant accessing the API endpoint over the community. The beneath code will prevent time to attain posting JSON knowledge by way of PHP cURL.

Instance: PHP cURL POST by Sending JSON Information

It prepares the JSON from an enter array and bundles it to the PHP cURL publish.

It makes use of PHP json_encode operate to get the encoded request parameters. Then, it makes use of the CURLOPT_POSTFIELDS choice to bundle the JSON knowledge to be posted.

curl-post-json.php

<?php
// URL of the API that's to be invoked and knowledge POSTed
$url="https://instance.com/api-to-post";

// request knowledge that's going to be despatched as POST to API
$knowledge = array(
    "animal" => "Lion",
    "kind" => "Wild",
    "title" => "Simba",
    "zoo" => array(
        "address1" => "5333 Zoo",
        "metropolis" => "Los Angeles",
        "state" => "CA",
        "nation" => "USA",
        "zipcode" => "90027"
    )
);

// encoding the request knowledge as JSON which will probably be despatched in POST
$encodedData = json_encode($knowledge);

// provoke curl with the url to ship request
$curl = curl_init($url);

// return CURL response
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

// Ship request knowledge utilizing POST methodology
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");

// Information conent-type is shipped as JSON
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
    'Content material-Kind:software/json'
));
curl_setopt($curl, CURLOPT_POST, true);

// Curl POST the JSON knowledge to ship the request
curl_setopt($curl, CURLOPT_POSTFIELDS, $encodedData);

// execute the curl POST request and ship knowledge
$consequence = curl_exec($curl);
curl_close($curl);

// if required print the curl response
print $consequence;
?>

php curl post json

The above code is one a part of the API request-response cycle. If the endpoint belongs to some third-party API, this code is sufficient to full this instance.

However, if the API is within the intra-system (customized API created for the appliance itself), then, the posted knowledge needs to be dealt with.

Find out how to get the JSON knowledge within the endpoint

That is to deal with the JSON knowledge posted by way of PHP cURL within the API endpoint.

It used json_decode to transform the JSON string posted right into a JSON object. On this program, it units “true” to transform the request knowledge into an array.

curl-request-data.php

<?php
// use the next code snippet to obtain
// JSON POST knowledge
// json_decode converts the JSON string to JSON object
$knowledge = json_decode(file_get_contents('php://enter'), true);
print_r($knowledge);
echo $knowledge;
?>

The json_encode operate additionally permits setting the allowed nesting restrict of the enter JSON. The default restrict is 512.

If the posted JSON knowledge is exceeding the nesting restrict, then the API endpoint will probably be did not get the publish knowledge.

Different modes of posting knowledge to a cURL request

In a earlier tutorial, we now have seen many examples of sending requests with PHP cURL POST.

This program units the content material kind “software/json” within the CURLOPT_HTTPHEADER. There are different modes of posting knowledge by way of PHP cURL.

  1. multipart/form-data – to ship an array of publish knowledge to the endpoint/
  2. software/x-www-form-urlencoded – to ship a URL-encoded string of type knowledge.

Notice: PHP http_build_query() can output the URL encoded string of an array.
Obtain

↑ Again to High

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments