{"id":170,"date":"2012-01-12T09:24:44","date_gmt":"2012-01-12T09:24:44","guid":{"rendered":"http:\/\/test.bleuanus.nl\/index.php\/curl-download-content\/"},"modified":"2018-12-11T16:09:08","modified_gmt":"2018-12-11T15:09:08","slug":"curl-download-content","status":"publish","type":"post","link":"https:\/\/www.bleuanus.nl\/index.php\/2012\/01\/12\/curl-download-content\/","title":{"rendered":"Curl (download content)"},"content":{"rendered":"<p class=\"last-modified\">This post was most recently updated on December 11th, 2018<\/p><h1>Download a URL\u2019s Content Using PHP cURL<\/h1>\n<div class=\" ready\" id=\"promo\">\u00a0<\/div>\n<p>Downloading content at a specific URL is common practice on the internet, especially due to increased usage of web services and APIs offered by Amazon, Alexa, Digg, etc. PHP&#8217;s cURL library, which often comes with default shared hosting configurations, allows web developers to complete this task.<\/p>\n<h2>The Code<\/h2>\n<pre class=\"php\" style=\"display: none\">\/* gets the data from a URL *\/function get_data($url){\t$ch = curl_init();\t$timeout = 5;\tcurl_setopt($ch,CURLOPT_URL,$url);\tcurl_setopt($ch,CURLOPT_RETURNTRANSFER,1);\tcurl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);\t$data = curl_exec($ch);\tcurl_close($ch);\treturn $data;}<\/pre>\n<pre class=\"standardLighter twilightLighter fadeIn\"><a class=\"codeCopy\">copy<\/a><span class=\"co2\">\/* gets the data from a URL *\/<\/span><span class=\"kw1\">function<\/span> <span class=\"me1\">get_data<\/span><span class=\"br0\">(<\/span><span class=\"kw3\">$url<\/span><span class=\"br0\">)<\/span><span class=\"br0\">{<\/span>  <span class=\"kw3\">$ch<\/span> = <span class=\"me1\">curl_init<\/span><span class=\"br0\">(<\/span><span class=\"br0\">)<\/span>;  <span class=\"kw3\">$timeout<\/span> = <span class=\"nu0\">5<\/span>;  <span class=\"me1\">curl_setopt<\/span><span class=\"br0\">(<\/span><span class=\"kw3\">$ch<\/span>,<span class=\"kw4\">CURLOPT_URL<\/span>,<span class=\"kw3\">$url<\/span><span class=\"br0\">)<\/span>;  <span class=\"me1\">curl_setopt<\/span><span class=\"br0\">(<\/span><span class=\"kw3\">$ch<\/span>,<span class=\"kw4\">CURLOPT_RETURNTRANSFER<\/span>,<span class=\"nu0\">1<\/span><span class=\"br0\">)<\/span>;  <span class=\"me1\">curl_setopt<\/span><span class=\"br0\">(<\/span><span class=\"kw3\">$ch<\/span>,<span class=\"kw4\">CURLOPT_CONNECTTIMEOUT<\/span>,<span class=\"kw3\">$timeout<\/span><span class=\"br0\">)<\/span>;  <span class=\"kw3\">$data<\/span> = <span class=\"me1\">curl_exec<\/span><span class=\"br0\">(<\/span><span class=\"kw3\">$ch<\/span><span class=\"br0\">)<\/span>;  <span class=\"me1\">curl_close<\/span><span class=\"br0\">(<\/span><span class=\"kw3\">$ch<\/span><span class=\"br0\">)<\/span>;  <span class=\"kw1\">return<\/span> <span class=\"kw3\">$data<\/span>;<span class=\"br0\">}<\/span><\/pre>\n<h2>The Usage<\/h2>\n<pre class=\"php\" style=\"display: none\">$returned_content = get_data('http:\/\/davidwalsh.name');<\/pre>\n<pre class=\"standardLighter twilightLighter fadeIn\"><a class=\"codeCopy\">copy<\/a><span class=\"kw3\">$returned_content<\/span> = <span class=\"me1\">get_data<\/span><span class=\"br0\">(<\/span><span class=\"st0\">'http:\/\/davidwalsh.name'<\/span><span class=\"br0\">)<\/span>;<\/pre>\n<p>Alternatively, you can use the <a target=\"_blank\" rel=\"nofollow\" href=\"http:\/\/php.net\/file_get_contents\">file_get_contents<\/a> function remotely, but many hosts don&#8217;t allow this.<\/p>\n<p>\u00a0<\/p>\n<h1>Set the User Agent With PHP cURL<\/h1>\n<div class=\" ready\" id=\"promo\">\u00a0<\/div>\n<p>A few months back, I shared with you how to <a href=\"http:\/\/davidwalsh.name\/download-urls-content-php-curl\">download the contents of a URL<\/a> and <a href=\"http:\/\/davidwalsh.name\/execute-http-post-php-curl\">execute a HTTP POST transmission<\/a> using PHP cURL. For security purposes, some hosts require that a common user agent be present in the POST. If an unacceptable user agent is given, the POST is ignored. Luckily, cURL allows us to &#8220;spoof&#8221; the server using any user agent we choose:<\/p>\n<pre class=\"php\" style=\"display: none\">curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla\/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko\/20080311 Firefox\/2.0.0.13');<\/pre>\n<pre class=\"standardLighter twilightLighter fadeIn\"><a class=\"codeCopy\">copy<\/a><span class=\"me1\">curl_setopt<\/span><span class=\"br0\">(<\/span><span class=\"kw3\">$ch<\/span>,<span class=\"kw4\">CURLOPT_USERAGENT<\/span>,<span class=\"st0\">'Mozilla\/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko\/20080311 Firefox\/2.0.0.13'<\/span><span class=\"br0\">)<\/span>;<\/pre>\n<p>A little sneaky but it could get you out of a jam at some point!<br \/>\u00a0<\/p>\n<p><a href=\"http:\/\/res2.windows.microsoft.com\/resbox\/en\/Windows 7\/main\/22bbe6bd-a56e-4072-b48e-a722b2546374_0.wmv\">temp url<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post was most recently updated on December 11th, 2018Download a URL\u2019s Content Using PHP cURL \u00a0 Downloading content at a specific URL is common practice on the internet, especially due to increased usage of web services and APIs offered by Amazon, Alexa, Digg, etc. PHP&#8217;s cURL library, which often comes with default shared hosting [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-170","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.bleuanus.nl\/index.php\/wp-json\/wp\/v2\/posts\/170","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bleuanus.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bleuanus.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bleuanus.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bleuanus.nl\/index.php\/wp-json\/wp\/v2\/comments?post=170"}],"version-history":[{"count":1,"href":"https:\/\/www.bleuanus.nl\/index.php\/wp-json\/wp\/v2\/posts\/170\/revisions"}],"predecessor-version":[{"id":220,"href":"https:\/\/www.bleuanus.nl\/index.php\/wp-json\/wp\/v2\/posts\/170\/revisions\/220"}],"wp:attachment":[{"href":"https:\/\/www.bleuanus.nl\/index.php\/wp-json\/wp\/v2\/media?parent=170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bleuanus.nl\/index.php\/wp-json\/wp\/v2\/categories?post=170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bleuanus.nl\/index.php\/wp-json\/wp\/v2\/tags?post=170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}