{"id":13117,"date":"2016-06-13T16:15:36","date_gmt":"2016-06-13T13:15:36","guid":{"rendered":"http:\/\/www.webcodegeeks.com\/?p=13117"},"modified":"2018-01-09T10:51:48","modified_gmt":"2018-01-09T08:51:48","slug":"php-curl-getpost-example","status":"publish","type":"post","link":"https:\/\/www.webcodegeeks.com\/php\/php-curl-getpost-example\/","title":{"rendered":"PHP Curl Get\/Post Example"},"content":{"rendered":"<p>If you have worked with Linux, you will\u00a0have probably used cURL for downloading resources from the Internet. This powerful library can also be used in PHP scripts, and that is what we are going to see in this example.<\/p>\n<p>For this example, we will use:<\/p>\n<ul>\n<li>Ubuntu (14.04) as Operating System.<\/li>\n<li>Apache HTTP server (2.4.7).<\/li>\n<li>PHP (5.5.9).<\/li>\n<\/ul>\n<p>&nbsp;<br \/>\n&nbsp;<br \/>\n[ulp id=&#8217;8njY7i2QRy6sg8pg&#8217;]<\/p>\n<div class=\"tip\"><strong>Tip<\/strong><br \/>\nYou may skip environment preparation and jump directly to the <a href=\"#section_2\"><strong>beginning of the example<\/strong><\/a> below.<\/div>\n<h2>1. Preparing the environment<\/h2>\n<h3>1.1. Installation<\/h3>\n<p>Below, commands to install Apache, PHP and PHP cURL library are shown:<\/p>\n<pre class=\"brush:bash\">sudo apt-get update\r\nsudo apt-get install apache2 php5 libapache2-mod-php5 php5-curl\r\nsudo service apache2 restart<\/pre>\n<h3>1.2. PHP configuration<\/h3>\n<p>In <code>\/etc\/php\/apache2\/php.ini<\/code> file, we need to include the cURL extension, in order to use it:<\/p>\n<pre class=\"brush:bash\">extension=php_curl.so<\/pre>\n<p>Don&#8217;t forget to restart Apache after doing any change.<\/p>\n<h2 id=\"section_2\">2. GET requests<\/h2>\n<p>Let&#8217;s see how we can use cURL to create GET requests:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>curl_get.php<\/em><\/span><\/p>\n<pre class=\"brush:php; highlight:[24,25,41,43,47,48,49,50,53,69,72,75,80,82]\">&lt;?php\r\n\r\n\/**\r\n\u00a0* Checks if the given parameters are set. If one of the specified parameters\r\n\u00a0* is not set, die() is called.\r\n\u00a0*\r\n\u00a0* @param $parameters The parameters to check.\r\n\u00a0*\/\r\nfunction checkGETParametersOrDie($parameters) {\r\n\u00a0\u00a0 \u00a0foreach ($parameters as $parameter) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0isset($_GET[$parameter]) || die(\"Please, provide '$parameter' parameter.\");\r\n\u00a0\u00a0 \u00a0}\r\n}\r\n\r\n\/**\r\n\u00a0* Gets the GET parameters.\r\n\u00a0*\r\n\u00a0* @return GET parameter string.\r\n\u00a0*\/\r\nfunction stringifyParameters() {\r\n\u00a0\u00a0 \u00a0$parameters = '?';\r\n\r\n\u00a0\u00a0 \u00a0foreach ($_GET as $key =&gt; $value) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0$key = urlencode($key);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0$value = urlencode($value);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0$parameters .= \"$key=$value&amp;\";\r\n\u00a0\u00a0 \u00a0}\r\n\r\n\u00a0\u00a0 \u00a0rtrim($parameters, '&amp;');\r\n\r\n\u00a0\u00a0 \u00a0return $parameters;\r\n}\r\n\r\n\/**\r\n\u00a0* Creates the cURL request for the given URL.\r\n\u00a0*\r\n\u00a0* @param $url The URL to create the request to.\r\n\u00a0* @return The cURL request to the url; false if an error occurs.\r\n\u00a0*\/\r\nfunction createCurlRequest($url) {\r\n\u00a0\u00a0 \u00a0$curl = curl_init();\r\n\r\n\u00a0\u00a0 \u00a0if (!$curl) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0return false;\r\n\u00a0\u00a0 \u00a0}\r\n\r\n\u00a0\u00a0 \u00a0$configured = curl_setopt_array($curl, [\r\n\u00a0\u00a0 \u00a0    CURLOPT_URL =&gt; $url . stringifyParameters(),\r\n\u00a0\u00a0 \u00a0 \u00a0\u00a0 CURLOPT_FOLLOWLOCATION =&gt; true,\r\n\u00a0\u00a0 \u00a0\u00a0 \u00a0 CURLOPT_RETURNTRANSFER =&gt; true\r\n\u00a0\u00a0 \u00a0]);\r\n\r\n\u00a0\u00a0 \u00a0if (!$configured) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0return false;\r\n\u00a0\u00a0 \u00a0}\r\n\r\n\u00a0\u00a0 \u00a0return $curl;\r\n}\r\n\r\n\/\/ Flow starts here.\r\n\r\ncheckGETParametersOrDie(['url']);\r\n\r\n$url = $_GET['url'];\r\n\r\n$curl = createCurlRequest($url);\r\n\r\nif (!$curl) {\r\n\u00a0\u00a0 \u00a0die('An error occured: ' . curl_error($curl));\r\n}\r\n\r\n$result = curl_exec($curl);\r\n\r\nif (!$result) {\r\n\u00a0\u00a0 \u00a0die('An error occured: ' . curl_error($curl));\r\n}\r\n\r\necho '&lt;div&gt;The result of the cURL request:&lt;\/div&gt;';\r\necho '&lt;hr&gt;';\r\necho $result;\r\n\r\ncurl_close($curl); \/\/ Don't forget to close!<\/pre>\n<p>This is what we have done:<\/p>\n<ul>\n<li>First, we created a cURL session, with <code>curl_init()<\/code> function, as in line 41. If some error occurs, <code>false<\/code> will be returned, so we have to check it before continuing (line 43).<\/li>\n<li>Once we have created successfully the cURL session, we have to configure it, as we do with <code>curl_setopt_array()<\/code> function. In this case, we have configured it with the following options:\n<ul>\n<li>The URL itself. For GET requests, we just have to specify the URL with the parameter string, in <code>key=value<\/code> format, as you already know. Note how is the parameter string constructed in <code>stringifyParameters()<\/code> function: <strong>the values should be encoded to URL encoding<\/strong>, with <code>urlencode()<\/code> function (lines 24 and 25).<\/li>\n<li>The <code>CURLOPT_FOLLOWLOCATION<\/code> option to <code>true<\/code>. This is for following the <code>3XX<\/code> HTTP redirections. If we set to <code>false<\/code>, and the specified URL makes a redirection, we won&#8217;t reach the final URL.<\/li>\n<li>The <code>CURLOPT_RETURNTRANSFER<\/code> option to <code>true<\/code>. This allows to save the HTTP response into a variable. If set to <code>false<\/code>, the response will be printed directly.<\/li>\n<\/ul>\n<\/li>\n<li>We should always check that the cURL functions don&#8217;t return any errors, checking the return values of the functions. When <code>false<\/code> is returned, we can get the information of the last error for the given cURL session with <code>curl_error()<\/code> function, as in lines 69 and 75.<\/li>\n<li>If any error has occurred initializing and configuring the cURL session, we can proceed to execute it, just calling <code>curl_exec()<\/code> function, specifying for which session we are executing the request. In this case, as we configured the <code>CURLOPT_RETURNTRANSFER<\/code> option, we can save the response value into a variable.<\/li>\n<li>Finally, when we have ended the cURL session handling, <strong>we must close it<\/strong>, <code>curl_close()<\/code> function.<\/li>\n<\/ul>\n<p>We can test this script, entering, for example,\u00a0<code>localhost\/path\/to\/curl_get.php?url=webcodegeeks.com&amp;s=php<\/code> in the browser. We will see that the output generated by the script, is the same of that which we would have received making a search in the above search box, in this page, entering <code>php<\/code>.<\/p>\n<h2>3. POST requests<\/h2>\n<p>For POST requests, we have to configure the cURL options in a slightly different way:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>curl_post.php<\/em><\/span><\/p>\n<pre class=\"brush:php;highlight:[11,12,13]\">\/\/ ...\r\n\r\nfunction createCurlRequest($url) {\r\n\u00a0\u00a0 \u00a0$curl = curl_init();\r\n\r\n\u00a0\u00a0 \u00a0if (!$curl) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0return false;\r\n\u00a0\u00a0 \u00a0}\r\n\r\n\u00a0\u00a0 \u00a0$configured = curl_setopt_array($curl, [\r\n\u00a0\u00a0 \u00a0\u00a0 \u00a0 CURLOPT_URL =&gt; $url,\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0CURLOPT_POST =&gt; true,\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0 CURLOPT_POSTFIELDS =&gt; stringifyParameters(),\r\n\u00a0\u00a0 \u00a0 \u00a0\u00a0 CURLOPT_FOLLOWLOCATION =&gt; true,\r\n\u00a0\u00a0 \u00a0\u00a0 \u00a0 CURLOPT_RETURNTRANSFER =&gt; true\r\n\u00a0\u00a0 \u00a0]);\r\n\r\n\u00a0\u00a0 \u00a0if (!$configured) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0return false;\r\n\u00a0\u00a0 \u00a0}\r\n\r\n\u00a0\u00a0 \u00a0return $curl;\r\n}\r\n\r\n\/\/ ...<\/pre>\n<p>Note that, when setting the URL, we just set the URL itself without the parameters, that&#8217;s for GET requests. Those parameters are specified in <code>CURLOPT_POSTFIELDS<\/code> option. And, apart from that, we are specifying that the request will be made for POST method, with <code>CURLOPT_POST<\/code> option.<\/p>\n<p>The only difference between cURL POST and GET requests is the configuration of the session. The rest of the script, can be the same for both.<\/p>\n<h2>4. Encapsulating operations in a cURL class<\/h2>\n<p>When we are going to use cURL in a project, is recommendable to have encapsulated all the code instead of using every time the functions we have used above, because we would be repeating much code, and the possibilities of introducing errors would increase. So, we are going to create a class that encapsulates this logic.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>curl_class.php<\/em><\/span><\/p>\n<pre class=\"brush:php;highlight:[38,62]\">&lt;?php\r\n\r\n\/**\r\n\u00a0* Methods for cURL request handling.\r\n\u00a0*\/\r\nclass CURL {\r\n\r\n\u00a0\u00a0\u00a0 \/**\r\n\u00a0\u00a0\u00a0\u00a0 * The cURL request object.\r\n\u00a0\u00a0\u00a0\u00a0 *\/\r\n\u00a0\u00a0\u00a0 private $request;\r\n\r\n\u00a0\u00a0\u00a0 \/**\r\n\u00a0\u00a0\u00a0\u00a0 * CURL class constructor.\r\n\u00a0\u00a0\u00a0\u00a0 *\r\n\u00a0\u00a0\u00a0\u00a0 * @throws Exception if an error occurs initializating.\r\n\u00a0\u00a0\u00a0\u00a0 *\/\r\n\u00a0\u00a0\u00a0 public function __construct() {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $this-&gt;request = curl_init();\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $this-&gt;throwExceptionIfError($this-&gt;request);\r\n\u00a0\u00a0\u00a0 }\r\n\r\n\u00a0\u00a0\u00a0 \/**\r\n\u00a0\u00a0\u00a0\u00a0 * Configure the cURL request.\r\n\u00a0\u00a0\u00a0\u00a0 *\r\n\u00a0\u00a0\u00a0\u00a0 * @param $url The target url.\r\n\u00a0\u00a0\u00a0\u00a0 * @param $urlParameters The array of parameters, with 'key' =&gt; 'value' format.\r\n\u00a0\u00a0\u00a0\u00a0 * @param $method 'GET' or 'POST'; 'GET' by default.\r\n\u00a0\u00a0\u00a0\u00a0 * @param $moreOptions Any other options to add to the cURL request. By default,\r\n\u00a0\u00a0\u00a0\u00a0 *\u00a0\u00a0\u00a0 'CURLOPT_FOLLOWLOCATION' (follow 3XX redirects) and 'CURLOPT_RETURNTRANSFER'\r\n\u00a0\u00a0\u00a0\u00a0 *\u00a0\u00a0\u00a0 (return HTTP response as a value, instead of outputting directly) are set.\r\n\u00a0\u00a0\u00a0\u00a0 * @throws Exception if an error occurs configurating.\r\n\u00a0\u00a0\u00a0\u00a0 *\/\r\n\u00a0\u00a0\u00a0 public function configure($url, $urlParameters = [], $method = 'GET',\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $moreOptions = [CURLOPT_FOLLOWLOCATION =&gt; true, CURLOPT_RETURNTRANSFER =&gt; true]) {\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 curl_reset($this-&gt;request);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 switch ($method) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case 'GET':\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $options = [CURLOPT_URL =&gt; $url . $this-&gt;stringifyParameters($urlParameters)];\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case 'POST':\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $options = [\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 CURLOPT_URL =&gt; $url,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 CURLOPT_POST =&gt; true,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 CURLOPT_POSTFIELDS =&gt; $this-&gt;stringifyParameters($urlParameters) \r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ];\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 default:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 throw new Exception('Method must be \"GET\" or \"POST\".');\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $options = $options + $moreOptions; \r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 foreach ($options as $option =&gt; $value) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $configured = curl_setopt($this-&gt;request, $option, $value);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $this-&gt;throwExceptionIfError($configured);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 }\r\n\r\n\u00a0\u00a0\u00a0 \/**\r\n\u00a0\u00a0\u00a0\u00a0 * Executes the cURL request for the options configured.\r\n\u00a0\u00a0\u00a0\u00a0 *\r\n\u00a0\u00a0\u00a0\u00a0 * @return The return value of curl_exec(). If CURLOPT_RETURNTRANSFER was configured,\r\n\u00a0\u00a0\u00a0\u00a0 *\u00a0\u00a0\u00a0\u00a0 the return value will be the HTTP response. In other case, a true value (or false\r\n\u00a0\u00a0\u00a0\u00a0 *\u00a0\u00a0\u00a0\u00a0 if some error has occured).\r\n\u00a0\u00a0\u00a0\u00a0 * @throws Exception if an error occurs executing.\r\n\u00a0\u00a0\u00a0\u00a0 *\/\r\n\u00a0\u00a0\u00a0 public function execute() {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $result = curl_exec($this-&gt;request);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $this-&gt;throwExceptionIfError($result);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return $result;\r\n\u00a0\u00a0\u00a0 }\r\n\r\n\u00a0\u00a0\u00a0 \/**\r\n\u00a0\u00a0\u00a0\u00a0 * Closes cURL session.\r\n\u00a0\u00a0\u00a0\u00a0 *\/\r\n\u00a0\u00a0\u00a0 public function close() {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 curl_close($this-&gt;request);\r\n\u00a0\u00a0\u00a0 }\r\n\r\n\u00a0\u00a0\u00a0 \/**\r\n\u00a0\u00a0\u00a0\u00a0 * Checks if a curl_* function returns success or failuer, throwing an exception\r\n\u00a0\u00a0\u00a0\u00a0 * with the cURL error message if it has failed.\r\n\u00a0\u00a0\u00a0\u00a0 *\r\n\u00a0\u00a0\u00a0\u00a0 * @param $success IF the curl function has succeeded or not.\r\n\u00a0\u00a0\u00a0\u00a0 * @throws Exception if the curl function has failed.\r\n\u00a0\u00a0\u00a0\u00a0 *\/\r\n\u00a0\u00a0\u00a0 protected function throwExceptionIfError($success) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if (!$success) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 throw new Exception(curl_error($this-&gt;request));\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 }\r\n\r\n\u00a0\u00a0\u00a0 \/**\r\n\u00a0\u00a0\u00a0\u00a0 * Creates a string of GET parameters.\r\n\u00a0\u00a0\u00a0\u00a0 *\r\n\u00a0\u00a0\u00a0\u00a0 * @param $parameters Parameter array.\r\n\u00a0\u00a0\u00a0\u00a0 * @return Parameters in string format: '?key1=value1&amp;key2=value2'\r\n\u00a0\u00a0\u00a0\u00a0 *\/\r\n\u00a0\u00a0\u00a0 protected function stringifyParameters($parameters) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $parameterString = '?';\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 foreach ($parameters as $key =&gt; $value) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $key = urlencode($key);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $value = urlencode($value);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $parameterString .= \"$key=$value&amp;\";\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 rtrim($parameterString, '&amp;');\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return $parameterString;\r\n\u00a0\u00a0\u00a0 }\r\n}<\/pre>\n<p>This class doesn&#8217;t do anything we haven&#8217;t seen before, except the way the options are configured (in line 60), with\u00a0<code>curl_setopt()<\/code> function, to set the options with no needing to know which options are.<\/p>\n<p>In order to use this class, we would only have to do something similar to this:<\/p>\n<pre class=\"brush:php\">&lt;?php\r\n\r\nrequire_once('curl_class.php');\r\n\r\ntry {\r\n\u00a0\u00a0\u00a0 $curl = new CURL();\r\n\r\n\u00a0\u00a0\u00a0 $curl-&gt;configure('webcodegeeks.com');\r\n\u00a0\u00a0\u00a0 $response = $curl-&gt;execute();\r\n\r\n    $curl-&gt;close();\r\n} catch (Exception $exception) {\r\n\u00a0\u00a0\u00a0 die('An exception has been thrown: ' . $exception-&gt;getMessage());\r\n}<\/pre>\n<p>Which is much more clean, readable and easy to maintain. And also more flexible, since it allows to configure any cURL option (and there is a large list of available options).<\/p>\n<p><strong>Note<\/strong>: cURL requests can be reused, increasing the performance. For example:<\/p>\n<pre class=\"brush:php\">&lt;?php\r\n\r\nrequire_once('curl_class.php');\r\n\r\ntry {\r\n\u00a0\u00a0\u00a0 $curl = new CURL();\r\n\r\n\u00a0\u00a0\u00a0 $curl-&gt;configure('webcodegeeks.com');\r\n\u00a0\u00a0\u00a0 $response1 = $curl-&gt;execute();\r\n\r\n    $curl-&gt;configure('webcodegeeks.com', ['s' =&gt; 'php'], 'GET');\r\n    $response2 = $curl-&gt;execute();\r\n\r\n    $curl-&gt;close();\r\n} catch (Exception $exception) {\r\n\u00a0\u00a0\u00a0 die('An exception has been thrown: ' . $exception-&gt;getMessage());\r\n}<\/pre>\n<p>The above script will work with any problem, and being more optimal than closing and opening a new cURL session for each request. In this case, the difference would probably be negligible, but for several request can suppose a significant change.<\/p>\n<p>To reuse the cURL sessions, we should clean and remove the options of this before adding new ones, as done in <code>curl_class.php<\/code> in line 38, with curl_reset <code>curl_reset()<\/code> function.<\/p>\n<h2>5. Summary<\/h2>\n<p>This example has shown how we can use the cURL library in PHP scripts, for making both GET and POST requests, which have to be handled in a slightly different way. Apart from that, we have developed a class for handling cURL in a much more ordered, clean and flexible way, allowing to reuse already opened sessions, in order to improve performance.<\/p>\n<h2>6. Download the source code<\/h2>\n<p>This was a cURL example for PHP.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>\nYou can download the full source code of this example here: <a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/06\/PHPCurlGetPostExample.zip\"><strong>PHPCurlGetPostExample<\/strong><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>If you have worked with Linux, you will\u00a0have probably used cURL for downloading resources from the Internet. This powerful library can also be used in PHP scripts, and that is what we are going to see in this example. For this example, we will use: Ubuntu (14.04) as Operating System. Apache HTTP server (2.4.7). PHP &hellip;<\/p>\n","protected":false},"author":160,"featured_media":930,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-13117","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>PHP Curl Get\/Post Example - Web Code Geeks - 2026<\/title>\n<meta name=\"description\" content=\"If you have worked with Linux, you will\u00a0have probably used cURL for downloading resources from the Internet. This powerful library can also be used in PHP\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.webcodegeeks.com\/php\/php-curl-getpost-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP Curl Get\/Post Example - Web Code Geeks - 2026\" \/>\n<meta property=\"og:description\" content=\"If you have worked with Linux, you will\u00a0have probably used cURL for downloading resources from the Internet. This powerful library can also be used in PHP\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.webcodegeeks.com\/php\/php-curl-getpost-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webcodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2016-06-13T13:15:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-01-09T08:51:48+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Toni\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Toni\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-curl-getpost-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-curl-getpost-example\/\"},\"author\":{\"name\":\"Toni\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/54a7be647b0b871cff41cbf3d2a95966\"},\"headline\":\"PHP Curl Get\/Post Example\",\"datePublished\":\"2016-06-13T13:15:36+00:00\",\"dateModified\":\"2018-01-09T08:51:48+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-curl-getpost-example\/\"},\"wordCount\":847,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-curl-getpost-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\",\"articleSection\":[\"PHP\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.webcodegeeks.com\/php\/php-curl-getpost-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-curl-getpost-example\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/php\/php-curl-getpost-example\/\",\"name\":\"PHP Curl Get\/Post Example - Web Code Geeks - 2026\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-curl-getpost-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-curl-getpost-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\",\"datePublished\":\"2016-06-13T13:15:36+00:00\",\"dateModified\":\"2018-01-09T08:51:48+00:00\",\"description\":\"If you have worked with Linux, you will\u00a0have probably used cURL for downloading resources from the Internet. This powerful library can also be used in PHP\",\"breadcrumb\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-curl-getpost-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.webcodegeeks.com\/php\/php-curl-getpost-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-curl-getpost-example\/#primaryimage\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-curl-getpost-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.webcodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP\",\"item\":\"https:\/\/www.webcodegeeks.com\/category\/php\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"PHP Curl Get\/Post Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"name\":\"Web Code Geeks\",\"description\":\"Web Developers Resource Center\",\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.webcodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webcodegeeks\",\"https:\/\/x.com\/webcodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/54a7be647b0b871cff41cbf3d2a95966\",\"name\":\"Toni\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/21c1661474c78b4757355b8beef9ab1d14f490ee3a3e67392f4e618d36643d4c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/21c1661474c78b4757355b8beef9ab1d14f490ee3a3e67392f4e618d36643d4c?s=96&d=mm&r=g\",\"caption\":\"Toni\"},\"url\":\"https:\/\/www.webcodegeeks.com\/author\/julen-pardo\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PHP Curl Get\/Post Example - Web Code Geeks - 2026","description":"If you have worked with Linux, you will\u00a0have probably used cURL for downloading resources from the Internet. This powerful library can also be used in PHP","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.webcodegeeks.com\/php\/php-curl-getpost-example\/","og_locale":"en_US","og_type":"article","og_title":"PHP Curl Get\/Post Example - Web Code Geeks - 2026","og_description":"If you have worked with Linux, you will\u00a0have probably used cURL for downloading resources from the Internet. This powerful library can also be used in PHP","og_url":"https:\/\/www.webcodegeeks.com\/php\/php-curl-getpost-example\/","og_site_name":"Web Code Geeks","article_publisher":"https:\/\/www.facebook.com\/webcodegeeks","article_published_time":"2016-06-13T13:15:36+00:00","article_modified_time":"2018-01-09T08:51:48+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","type":"image\/jpeg"}],"author":"Toni","twitter_card":"summary_large_image","twitter_creator":"@webcodegeeks","twitter_site":"@webcodegeeks","twitter_misc":{"Written by":"Toni","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.webcodegeeks.com\/php\/php-curl-getpost-example\/#article","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-curl-getpost-example\/"},"author":{"name":"Toni","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/54a7be647b0b871cff41cbf3d2a95966"},"headline":"PHP Curl Get\/Post Example","datePublished":"2016-06-13T13:15:36+00:00","dateModified":"2018-01-09T08:51:48+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-curl-getpost-example\/"},"wordCount":847,"commentCount":0,"publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-curl-getpost-example\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","articleSection":["PHP"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.webcodegeeks.com\/php\/php-curl-getpost-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.webcodegeeks.com\/php\/php-curl-getpost-example\/","url":"https:\/\/www.webcodegeeks.com\/php\/php-curl-getpost-example\/","name":"PHP Curl Get\/Post Example - Web Code Geeks - 2026","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-curl-getpost-example\/#primaryimage"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-curl-getpost-example\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","datePublished":"2016-06-13T13:15:36+00:00","dateModified":"2018-01-09T08:51:48+00:00","description":"If you have worked with Linux, you will\u00a0have probably used cURL for downloading resources from the Internet. This powerful library can also be used in PHP","breadcrumb":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-curl-getpost-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.webcodegeeks.com\/php\/php-curl-getpost-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/php\/php-curl-getpost-example\/#primaryimage","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.webcodegeeks.com\/php\/php-curl-getpost-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.webcodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"PHP","item":"https:\/\/www.webcodegeeks.com\/category\/php\/"},{"@type":"ListItem","position":3,"name":"PHP Curl Get\/Post Example"}]},{"@type":"WebSite","@id":"https:\/\/www.webcodegeeks.com\/#website","url":"https:\/\/www.webcodegeeks.com\/","name":"Web Code Geeks","description":"Web Developers Resource Center","publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.webcodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.webcodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.webcodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webcodegeeks","https:\/\/x.com\/webcodegeeks"]},{"@type":"Person","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/54a7be647b0b871cff41cbf3d2a95966","name":"Toni","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/21c1661474c78b4757355b8beef9ab1d14f490ee3a3e67392f4e618d36643d4c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/21c1661474c78b4757355b8beef9ab1d14f490ee3a3e67392f4e618d36643d4c?s=96&d=mm&r=g","caption":"Toni"},"url":"https:\/\/www.webcodegeeks.com\/author\/julen-pardo\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/13117","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/users\/160"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/comments?post=13117"}],"version-history":[{"count":0,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/13117\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media\/930"}],"wp:attachment":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media?parent=13117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/categories?post=13117"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/tags?post=13117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}