{"id":128875,"date":"2022-10-13T17:51:07","date_gmt":"2022-10-13T17:51:07","guid":{"rendered":"https:\/\/phppot.com\/?p=19698"},"modified":"2022-10-13T17:51:07","modified_gmt":"2022-10-13T17:51:07","slug":"php-curl-post-json-send-request-data","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/10\/13\/php-curl-post-json-send-request-data\/","title":{"rendered":"PHP Curl POST JSON Send Request Data"},"content":{"rendered":"<div class=\"modified-on\" readability=\"7.1304347826087\"> by <a href=\"https:\/\/phppot.com\/about\/\">Vincy<\/a>. Last modified on October 13th, 2022.<\/div>\n<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>\n<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>\n<h2>Example: PHP cURL POST by Sending JSON Data<\/h2>\n<p>It prepares the JSON from an input array and bundles it to the PHP cURL post.<\/p>\n<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>\n<p class=\"code-heading\">curl-post-json.php<\/p>\n<pre class=\"prettyprint\"><code class=\"language-php\">&lt;?php\n\/\/ URL of the API that is to be invoked and data POSTed\n$url = 'https:\/\/example.com\/api-to-post'; \/\/ request data that is going to be sent as POST to API\n$data = array( \"animal\" =&gt; \"Lion\", \"type\" =&gt; \"Wild\", \"name\" =&gt; \"Simba\", \"zoo\" =&gt; array( \"address1\" =&gt; \"5333 Zoo\", \"city\" =&gt; \"Los Angeles\", \"state\" =&gt; \"CA\", \"country\" =&gt; \"USA\", \"zipcode\" =&gt; \"90027\" )\n); \/\/ encoding the request data as JSON which will be sent in POST\n$encodedData = json_encode($data); \/\/ initiate curl with the url to send request\n$curl = curl_init($url); \/\/ return CURL response\ncurl_setopt($curl, CURLOPT_RETURNTRANSFER, true); \/\/ Send request data using POST method\ncurl_setopt($curl, CURLOPT_CUSTOMREQUEST, \"POST\"); \/\/ Data conent-type is sent as JSON\ncurl_setopt($curl, CURLOPT_HTTPHEADER, array( 'Content-Type:application\/json'\n));\ncurl_setopt($curl, CURLOPT_POST, true); \/\/ Curl POST the JSON data to send the request\ncurl_setopt($curl, CURLOPT_POSTFIELDS, $encodedData); \/\/ execute the curl POST request and send data\n$result = curl_exec($curl);\ncurl_close($curl); \/\/ if required print the curl response\nprint $result;\n?&gt;\n<\/code><\/pre>\n<p><img decoding=\"async\" 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=\"auto, (max-width: 550px) 100vw, 550px\"><\/p>\n<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>\n<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>\n<h3>How to get the JSON data in the endpoint<\/h3>\n<p>This is to handle the JSON data posted via PHP cURL in the API endpoint.<\/p>\n<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 \u201ctrue\u201d to convert the request data into an array.<\/p>\n<p class=\"code-heading\">curl-request-data.php<\/p>\n<pre class=\"prettyprint\"><code class=\"language-php\">&lt;?php\n\/\/ use the following code snippet to receive\n\/\/ JSON POST data\n\/\/ json_decode converts the JSON string to JSON object\n$data = json_decode(file_get_contents('php:\/\/input'), true);\nprint_r($data);\necho $data;\n?&gt;\n<\/code><\/pre>\n<p>The json_encode function also allows setting the allowed nesting limit of the input JSON. The default limit is 512.<\/p>\n<p>If the posted JSON data is exceeding the nesting limit, then the API endpoint will be failed to get the post data.<\/p>\n<h2>Other modes of posting data to a cURL request<\/h2>\n<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>\n<p>This program sets the content type \u201capplication\/json\u201d in the CURLOPT_HTTPHEADER. There are other modes of posting data via PHP cURL.<\/p>\n<ol>\n<li>multipart\/form-data \u2013 to send an array of post data to the endpoint\/<\/li>\n<li>application\/x-www-form-urlencoded \u2013 to send a URL-encoded string of form data.<\/li>\n<\/ol>\n<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>\n<p> <!-- #comments --> <\/p>\n<div class=\"related-articles\">\n<h2>Popular Articles<\/h2>\n<\/p><\/div>\n<p> <a href=\"https:\/\/phppot.com\/php\/php-curl-post-json\/#top\" class=\"top\">\u2191 Back to Top<\/a> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>by Vincy. Last modified on October 13th, 2022. 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. The cURL is a way of remote accessing the API [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":128876,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[65],"tags":[],"class_list":["post-128875","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php-updates"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/128875","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=128875"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/128875\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media\/128876"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=128875"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=128875"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=128875"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}