{"id":1906,"date":"2018-10-28T13:02:11","date_gmt":"2018-10-28T13:02:11","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=1906"},"modified":"2025-12-04T19:41:12","modified_gmt":"2025-12-04T19:41:12","slug":"curl-get-request","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/php\/curl\/curl-get-request","title":{"rendered":"CURL GET Request"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Example of using CURL to GET a response<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>  $Url = \"mydomain.org\/somefile.php?MyParameter1=abc&amp;MyParameter2=2\";\n  $ch = curl_init();\n  curl_setopt($ch, CURLOPT_URL, $Url);\n  curl_setopt($ch, CURLOPT_HTTPGET, true);\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  $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  \/\/If your output is json then you can decode it with this:\n  $DecodedJson = json_decode($Response);\n  echo \"Response: \";\n  print_r($Response);\n  echo \"&lt;br>DecodedJson: \";\n  print_r($DecodedJson);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">GET with query parameters loaded as an array<\/h4>\n\n\n\n<p>They are still added to the URL, but this method lets you load them as an array<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$QueryParameters = &#91;\n    'param1' => 'value1',\n    'param2' => 'value2'\n];\n\n  $Url = \"mydomain.org\/somefile.php\";\n  $Url .= '?' . http_build_query($QueryParameters);      \/\/Append query parameters to URL\n  $ch = curl_init();\n  curl_setopt($ch, CURLOPT_URL, $Url);\n  curl_setopt($ch, CURLOPT_HTTPGET, true);\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  $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  \/\/If your output is json then you can decode it with this:\n  $DecodedJson = json_decode($Response);\n  echo \"Response: \";\n  print_r($Response);\n  echo \"&lt;br>DecodedJson: \";\n  print_r($DecodedJson);<\/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\"));\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Example of using CURL to GET a response GET with query parameters loaded as an array They are still added to the URL, but this method lets you load them as an array Add header fields<\/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-1906","post","type-post","status-publish","format-standard","hentry","category-curl"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/1906","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=1906"}],"version-history":[{"count":16,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/1906\/revisions"}],"predecessor-version":[{"id":5251,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/1906\/revisions\/5251"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=1906"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=1906"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=1906"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}