{"id":84,"date":"2012-10-27T14:36:27","date_gmt":"2012-10-27T09:06:27","guid":{"rendered":"http:\/\/prasadk.com\/my-press\/?p=84"},"modified":"2012-10-27T14:36:27","modified_gmt":"2012-10-27T09:06:27","slug":"curl-php","status":"publish","type":"post","link":"https:\/\/www.prasadk.com\/my-press\/curl-php\/","title":{"rendered":"cURL \/ PHP"},"content":{"rendered":"<p><em><strong>cURL<\/strong><\/em> is a command line tool for transferring files with\u00a0URL\u00a0syntax, supporting\u00a0FTP, FTPS, HTTP, HTTPS, SCP, SFTP, TFTP,\u00a0TELNET, DICT, FILE and LDAP. cURL supports HTTPS certificates, HTTP POST, HTTP PUT,\u00a0FTP\u00a0uploading, HTTP form based upload, proxies, cookies, user+password authentication (Basic, Digest, NTLM, Negotiate, kerberos&#8230;), file transfer resume, proxy tunneling and other useful tricks.<\/p>\n<h3>Fetching a web page<\/h3>\n<pre>&lt;?php\r\n$ch = curl_init();\r\ncurl_setopt($ch, CURLOPT_URL, \"http:\/\/example.com\/\");\r\ncurl_setopt($ch, CURLOPT_HEADER, 0);\r\ncurl_exec($ch);\r\ncurl_close($ch);\r\n?&gt;<\/pre>\n<h3>Alternative for\u00a0<code>file_get_contents()<\/code><\/h3>\n<p>Instead of:<\/p>\n<pre>&lt;?php\r\n$file_contents = file_get_contents('http:\/\/example.com\/');\r\n\r\n\/\/ display file\r\necho $file_contents;\r\n?&gt;<\/pre>\n<p>Use this:<\/p>\n<pre>&lt;?php\r\n$ch = curl_init();\r\n$timeout = 5; \/\/ set to zero for no timeout\r\ncurl_setopt ($ch, CURLOPT_URL, 'http:\/\/example.com');\r\ncurl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);\r\ncurl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);\r\n$file_contents = curl_exec($ch);\r\ncurl_close($ch);\r\n\r\n\/\/ display file\r\necho $file_contents;\r\n?&gt;<\/pre>\n<p>Otherwise if you are getting some errors with the code above, use this:<\/p>\n<pre>&lt;?php\r\n$site_url = 'http:\/\/example.com';\r\n$ch = curl_init();\r\n$timeout = 5; \/\/ set to zero for no timeout\r\ncurl_setopt ($ch, CURLOPT_URL, $site_url);\r\ncurl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);\r\n\r\nob_start();\r\ncurl_exec($ch);\r\ncurl_close($ch);\r\n$file_contents = ob_get_contents();\r\nob_end_clean();\r\n\r\necho $file_contents;\r\n?&gt;<\/pre>\n<h3>Getting binary data<\/h3>\n<h4>Images<\/h4>\n<p>This script retrieves a remote image and assigns the binary data to the variable\u00a0<code>$image<\/code>, before outputting the image to the browser:<\/p>\n<pre>&lt;?php\r\n$image_url = \"http:\/\/example.com\/image.jpg\";\r\n$ch = curl_init();\r\n$timeout = 0;\r\ncurl_setopt ($ch, CURLOPT_URL, $image_url);\r\ncurl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);\r\n\r\n\/\/ Getting binary data\r\ncurl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);\r\ncurl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);\r\n\r\n$image = curl_exec($ch);\r\ncurl_close($ch);\r\n\r\n\/\/ output to browser\r\nheader(\"Content-type: image\/jpeg\");\r\nprint $image;\r\n?&gt;<\/pre>\n<h3>Alternative for\u00a0<code>file()<\/code><\/h3>\n<p>Instead of:<\/p>\n<pre>&lt;?php\r\n$lines = file('http:\/\/example.com\/');\r\n\r\n\/\/ display file line by line\r\nforeach($lines as $line_num =&gt; $line) {\r\n    echo \"Line # {$line_num}\u00a0: \".htmlspecialchars($line).\"&lt;br \/&gt;\\n\";\r\n}\r\n?&gt;<\/pre>\n<p>Use this:<\/p>\n<pre>&lt;?php\r\n$ch = curl_init();\r\n$timeout = 5; \/\/ set to zero for no timeout\r\ncurl_setopt ($ch, CURLOPT_URL, 'http:\/\/example.com');\r\ncurl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);\r\ncurl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);\r\n$file_contents = curl_exec($ch);\r\ncurl_close($ch);\r\n$lines = array();\r\n$lines = explode(\"\\n\", $file_contents);\r\n\r\n\/\/ display file line by line\r\nforeach($lines as $line_num =&gt; $line) {\r\n    echo \"Line # {$line_num}\u00a0: \".htmlspecialchars($line).\"&lt;br \/&gt;\\n\";\r\n}\r\n?&gt;<\/pre>\n<h3>Wrapping it all in an easy class<\/h3>\n<p>Use the following class to make reading\/saving remote files easy. This class will automatically delete the temp files downloaded at the end of your PHP script.<\/p>\n<pre>&lt;?php\r\n\r\nclass downloader {\r\n    var $tempFolder;\r\n    var $tempFiles = array();\r\n\r\n    function __destruct () {\r\n        foreach ($this-&gt;tempFiles as $file) {\r\n            unlink($file['temp']);\r\n        }\r\n    }\r\n\r\n    function __construct ($temp)\r\n    {\r\n        $this-&gt;tempFolder = $temp;\r\n    }\r\n\r\n    function get ($url) {\r\n        array_unshift($this-&gt;tempFiles, array(\r\n            'extension'=&gt; array_pop(explode('.', $url)),\r\n            'original'=&gt; basename($url),\r\n            'temp'=&gt; $this-&gt;tempFolder . md5(microtime()),\r\n        ));\r\n        $ch = curl_init($url);\r\n        $fp = fopen($this-&gt;tempFiles[0]['temp'], 'w');\r\n        curl_setopt($ch, CURLOPT_FILE, $fp);\r\n        curl_setopt($ch, CURLOPT_HEADER, 0);\r\n        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);\r\n        curl_exec($ch);\r\n        curl_close($ch);\r\n        fclose($fp);\r\n        return $this-&gt;tempFiles[0]['temp'];\r\n    }\r\n\r\n    function read ($index = 0) {\r\n        return file_get_contents($this-&gt;tempFiles[$index]['temp']);\r\n    }\r\n\r\n    function readArray ($index = 0)\r\n    {\r\n        return file($this-&gt;tempFiles[$index]['temp']);\r\n    }\r\n\r\n    function listFiles () {\r\n        return $this-&gt;tempFiles;\r\n    }\r\n\r\n    function save ($path, $index = 0) {\r\n        copy($this-&gt;tempFiles[$index]['temp'], (is_dir($path)\u00a0? $path . $this-&gt;tempFiles[$index]['original']\u00a0: $path));\r\n    }\r\n}\r\n\r\n$d = new downloader('\/home\/&lt;username&gt;\/&lt;temp folder&gt;');\r\n\r\n?&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>cURL is a command line tool for transferring files with\u00a0URL\u00a0syntax, supporting\u00a0FTP, FTPS, HTTP, HTTPS, SCP, SFTP, TFTP,\u00a0TELNET, DICT, FILE and LDAP. cURL supports HTTPS certificates, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0},"categories":[7],"tags":[],"_links":{"self":[{"href":"https:\/\/www.prasadk.com\/my-press\/wp-json\/wp\/v2\/posts\/84"}],"collection":[{"href":"https:\/\/www.prasadk.com\/my-press\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.prasadk.com\/my-press\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.prasadk.com\/my-press\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.prasadk.com\/my-press\/wp-json\/wp\/v2\/comments?post=84"}],"version-history":[{"count":2,"href":"https:\/\/www.prasadk.com\/my-press\/wp-json\/wp\/v2\/posts\/84\/revisions"}],"predecessor-version":[{"id":86,"href":"https:\/\/www.prasadk.com\/my-press\/wp-json\/wp\/v2\/posts\/84\/revisions\/86"}],"wp:attachment":[{"href":"https:\/\/www.prasadk.com\/my-press\/wp-json\/wp\/v2\/media?parent=84"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.prasadk.com\/my-press\/wp-json\/wp\/v2\/categories?post=84"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.prasadk.com\/my-press\/wp-json\/wp\/v2\/tags?post=84"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}