[Tut] PHP Curl POST JSON Send Request Data - Printable Version +- Sick Gaming (https://www.sickgaming.net) +-- Forum: Programming (https://www.sickgaming.net/forum-76.html) +--- Forum: PHP Development (https://www.sickgaming.net/forum-82.html) +--- Thread: [Tut] PHP Curl POST JSON Send Request Data (/thread-100081.html) |
[Tut] PHP Curl POST JSON Send Request Data - xSicKxBot - 10-14-2022 PHP Curl POST JSON Send Request Data <div style="margin: 5px 5% 10px 5%;"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2022/10/php-curl-post-json-send-request-data.jpg" width="550" height="337" title="" alt="" /></div><div><div class="modified-on" readability="7.1304347826087"> by <a href="https://phppot.com/about/">Vincy</a>. Last modified on October 13th, 2022.</div> <p>Most of the APIs are used to accept requests and send responses in JSON format. JSON is the de-facto data exchange format. It is important to learn how to send JSON request data with an API call.</p> <p>The cURL is a way of remote accessing the API endpoint over the network. The below code will save you time to achieve posting JSON data via PHP cURL.</p> <h2>Example: PHP cURL POST by Sending JSON Data</h2> <p>It prepares the JSON from an input array and bundles it to the PHP cURL post.</p> <p>It uses <a href="https://phppot.com/php/php-json-encode-and-decode/">PHP json_encode function</a> to get the encoded request parameters. Then, it uses the CURLOPT_POSTFIELDS option to bundle the JSON data to be posted.</p> <p class="code-heading">curl-post-json.php</p> <pre class="prettyprint"><code class="language-php"><?php // URL of the API that is to be invoked and data POSTed $url = 'https://example.com/api-to-post'; // request data that is going to be sent as POST to API $data = array( "animal" => "Lion", "type" => "Wild", "name" => "Simba", "zoo" => array( "address1" => "5333 Zoo", "city" => "Los Angeles", "state" => "CA", "country" => "USA", "zipcode" => "90027" ) ); // encoding the request data as JSON which will be sent in POST $encodedData = json_encode($data); // initiate curl with the url to send request $curl = curl_init($url); // return CURL response curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // Send request data using POST method curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST"); // Data conent-type is sent as JSON curl_setopt($curl, CURLOPT_HTTPHEADER, array( 'Content-Type:application/json' )); curl_setopt($curl, CURLOPT_POST, true); // Curl POST the JSON data to send the request curl_setopt($curl, CURLOPT_POSTFIELDS, $encodedData); // execute the curl POST request and send data $result = curl_exec($curl); curl_close($curl); // if required print the curl response print $result; ?> </code></pre> <p><img loading="lazy" class="alignnone size-large wp-image-19727" src="https://phppot.com/wp-content/uploads/2022/10/php-curl-post-json-550x337.jpg" alt="php curl post json" width="550" height="337" srcset="https://phppot.com/wp-content/uploads/2022/10/php-curl-post-json-550x337.jpg 550w, https://phppot.com/wp-content/uploads/2022/10/php-curl-post-json-300x184.jpg 300w, https://phppot.com/wp-content/uploads/2022/10/php-curl-post-json-768x471.jpg 768w, https://phppot.com/wp-content/uploads/2022/10/php-curl-post-json.jpg 960w" sizes="(max-width: 550px) 100vw, 550px"></p> <p>The above code is one part of the API request-response cycle. If the endpoint belongs to some third-party API, this code is enough to complete this example.</p> <p>But, if the API is in the intra-system (custom API created for the application itself), then, the posted data has to be handled.</p> <h3>How to get the JSON data in the endpoint</h3> <p>This is to handle the JSON data posted via PHP cURL in the API endpoint.</p> <p>It used <a href="https://phppot.com/php/php-object-to-array/">json_decode to convert the JSON string</a> posted into a JSON object. In this program, it sets “true” to convert the request data into an array.</p> <p class="code-heading">curl-request-data.php</p> <pre class="prettyprint"><code class="language-php"><?php // use the following code snippet to receive // JSON POST data // json_decode converts the JSON string to JSON object $data = json_decode(file_get_contents('php://input'), true); print_r($data); echo $data; ?> </code></pre> <p>The json_encode function also allows setting the allowed nesting limit of the input JSON. The default limit is 512.</p> <p>If the posted JSON data is exceeding the nesting limit, then the API endpoint will be failed to get the post data.</p> <h2>Other modes of posting data to a cURL request</h2> <p>In a previous tutorial, we have seen many examples of sending requests with <a href="https://phppot.com/php/php-curl-post/">PHP cURL POST</a>.</p> <p>This program sets the content type “application/json” in the CURLOPT_HTTPHEADER. There are other modes of posting data via PHP cURL.</p> <ol> <li>multipart/form-data – to send an array of post data to the endpoint/</li> <li>application/x-www-form-urlencoded – to send a URL-encoded string of form data.</li> </ol> <p>Note: PHP http_build_query() can output the URL encoded string of an array.<br /><a class="download" href="https://phppot.com/downloads/php/curl-post-json.zip">Download</a></p> <p> <!-- #comments --> </p> <div class="related-articles"> <h2>Popular Articles</h2> </p></div> <p> <a href="https://phppot.com/php/php-curl-post-json/#top" class="top">↑ Back to Top</a> </p> </div> https://www.sickgaming.net/blog/2022/10/13/php-curl-post-json-send-request-data/ |