{"id":2206,"date":"2019-09-15T08:45:29","date_gmt":"2019-09-15T07:45:29","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=2206"},"modified":"2025-12-04T19:40:38","modified_gmt":"2025-12-04T19:40:38","slug":"curl-post-request","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/php\/curl\/curl-post-request","title":{"rendered":"Curl POST Request"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">POST Multiple POST Values<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>  \/\/Setup the POST fields\n  $PostData = array(\n    'my_field1' => $my_field1,\n    'my_field1' => $my_field1\n  );\n  $PostData = http_build_query($PostData);\n  \n  $Url = 'https:\/\/urltosendto.com';\n  \n  $Response = '';\n  if(function_exists('curl_init') &amp;&amp; function_exists('curl_setopt') &amp;&amp; function_exists('curl_exec'))\n  {\n    $ch =  curl_init();\n    curl_setopt($ch, CURLOPT_URL, $Url);\n    curl_setopt($ch, CURLOPT_POST, True);\n    curl_setopt($ch, CURLOPT_POSTFIELDS, $PostData);\n    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);\n    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);        \/\/Connect timeout in secs\n    curl_setopt($ch, CURLOPT_TIMEOUT, 10);               \/\/Maximum time the request is allowed to take in secs\n    $Response = curl_exec($ch);\n    $HttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);\n\n    if ($HttpCode !== 200)\n      die(\"Curl request failed, HTTP status code: $HttpCode\");\n \n   \/\/echo($Response);\n    if (strpos($Response, 'SUCCESS') !== False)\n    {\n      \/\/It worked\n      \/\/echo\"&#91;Success]\";\n    }\n  }<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">POST a json based request<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>  $PostData = array(\n    'SomeField1' => $MyVariable,\n    'SomeField1' => 1\n  );\n  $PostData = json_encode($PostData);\n\n  $Url = \"https:\/\/myurl.com\";\n  $ch = curl_init();\n  curl_setopt($ch, CURLOPT_URL, $Url);\n  curl_setopt($ch, CURLOPT_POST, true);\n  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application\/json'));   \/\/&lt;&lt;&lt;You can add header fields in here. e.g.: curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application\/json', \"Token:$MyAcccessToken\"));\n  curl_setopt($ch, CURLOPT_POSTFIELDS, $PostData);\n  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); \t\t\/\/Follow any page redirects etc (optional)\n  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);\t\t\t\t\/\/&lt;&lt;&lt;Include this for the output of curl to go to the curl_exec() output below instead of the browser screen\n  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);\t\t\t  \/\/Connect timeout in secs\n  curl_setopt($ch, CURLOPT_TIMEOUT, 10);\t\t\t\t      \/\/Maximum time the request is allowed to take in secs\n  $CurlResult = curl_exec($ch);\n  $HttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);\n\n  if ($HttpCode !== 200)\n    die(\"Curl request failed, HTTP status code: $HttpCode\");\n\n  $DecodedJson = json_decode($CurlResult, True);\n  \n  echo \"CurlResult: \";\n  print_r($CurlResult);\n  echo \"&lt;br>DecodedJson: \";\n  print_r($DecodedJson);\n  \n  if (!is_null($DecodedJson))\n  {\n    \/\/Check for succcess\n    if ( (isset($DecodedJson&#91;'errorCode'])) &amp;&amp; ($DecodedJson&#91;'errorCode'] == 0) )\n    {\n    }\n  }<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">POST a json block with authorisation<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>  $PostData = array(\n    'SomeField1' => $MyVariable,\n    'SomeField1' => 1\n  );\n  $PostData = json_encode($PostData);\n  \n  $Url = 'https:\/\/urltosendto.com';\n  $Username = 'myusername';\n  $Password = 'mypassword';\n  \n  if(function_exists('curl_init') &amp;&amp; function_exists('curl_setopt') &amp;&amp; function_exists('curl_exec'))\n  {\n    $ch =  curl_init();\n    curl_setopt($ch, CURLOPT_URL, $Url);\n    curl_setopt($ch, CURLOPT_POST, 1);\n    curl_setopt($ch, CURLOPT_POSTFIELDS, $PostData);\n    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);\n    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);\n    curl_setopt($ch, CURLOPT_TIMEOUT, 10);\n    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Basic ' . base64_encode(\"$Username:$Password\"), 'Content-Type: application\/json'));\n    $Response = curl_exec($ch);\n    $HttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);\n  }\n\n  if ($HttpCode !== 200)\n    die(\"Curl request failed, HTTP status code: $HttpCode\");\n\n  \/\/response_code\":\"SUCCESS\"\n  $Response = json_decode($Response, TRUE);\n  $response_code = $Response&#91;'response_code'];\n  \n  if ($response_code == \"SUCCESS\")\n  {\n    \n  }<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Add header fields<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application\/json'));\n  \/\/For multiple entires use this instead:\n  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application\/json', \"Token:$MyAcccessToken\"));<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">POST with a file<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>  $FilePath = \"\/var\/www\/vhosts\/mydomain.com\/httpdocs\/wp-content\/uploads\/my_logo.png\";\n\n  $Url = 'https:\/\/urltosendto.com';\n\n  $PostData = array(\n    'SomeField1' => $MyVariable,\n    'SomeField1' => 1\n  );\n\n  \/\/Add the file to upload to PostData\n  $UploadFileName = basename($FilePath);                \/\/Get just the file name from the path\n  $MimeContentType = mime_content_type($FilePath);      \/\/Get the MIME type of the file\n  $PostData&#91;'file'] = new \\CURLFile($FilePath, $MimeContentType, $UploadFileName);         \/\/Add the file to the PostData     (Remove the leading '\\' if not using within a namespace  \n\n  \/\/Do not use http_build_query() when doing 'multipart\/form-data'\n\n  $Response = '';\n  if(function_exists('curl_init') &amp;&amp; function_exists('curl_setopt') &amp;&amp; function_exists('curl_exec'))\n  {\n    $ch =  curl_init();\n    curl_setopt($ch, CURLOPT_URL, $Url);\n    curl_setopt($ch, CURLOPT_POST, True);\n    curl_setopt($ch, CURLOPT_ENCODING, \"\");\n    curl_setopt($ch, CURLOPT_MAXREDIRS, 10);\n    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, \"POST\");\n    curl_setopt($ch, CURLOPT_POSTFIELDS, $PostData);\n    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\n    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);        \/\/Connect timeout in secs\n    curl_setopt($ch, CURLOPT_TIMEOUT, 30);               \/\/Maximum time the request is allowed to take in secs\n    curl_setopt($ch, CURLOPT_HTTPHEADER, array(\"Authorization: Bearer $PinataJwt\", \"Content-Type: multipart\/form-data\"));\n    $Response = curl_exec($ch);\n    $HttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);\n\n    $DecodedJson = json_decode($Response, True);\n    echo \"CurlResult: \";\n    print_r($Response);\n    echo \"&lt;br>DecodedJson: \";\n    print_r($DecodedJson);\n\n    if ($HttpCode !== 200)\n      die(\"Curl request failed, HTTP status code: $HttpCode\");<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>POST Multiple POST Values POST a json based request POST a json block with authorisation Add header fields POST with a file<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[157],"tags":[],"class_list":["post-2206","post","type-post","status-publish","format-standard","hentry","category-curl"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/2206","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=2206"}],"version-history":[{"count":17,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/2206\/revisions"}],"predecessor-version":[{"id":5249,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/2206\/revisions\/5249"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=2206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=2206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=2206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}