{"id":4224,"date":"2013-03-11T14:24:10","date_gmt":"2013-03-11T14:24:10","guid":{"rendered":"http:\/\/www.codekites.com\/?p=4224"},"modified":"2023-05-01T19:46:28","modified_gmt":"2023-05-01T14:16:28","slug":"use-proxy-with-curl-php","status":"publish","type":"post","link":"https:\/\/www.binarytides.com\/use-proxy-with-curl-php\/","title":{"rendered":"How to use proxy with curl in Php"},"content":{"rendered":"<h3>Proxy with Curl<\/h3>\n<p>Curl is a very useful library for transferring data over various protocols like http, ftp, https etc. In other words curl can be used to programatically download a web page, or upload file to ftp etc. <\/p>\t\t<div class=\"display-ad-unit mobile-wide bsa\" style=\"background:#fff3f3; height:315px;\">\n\n<!-- BinaryTides_S2S_InContent_ROS_Pos1 -->\n<style>\n\t@media only screen and (min-width: 0px) and (min-height: 0px) {\n\t\tdiv[id^=\"bsa-zone_1611170977806-3_123456\"] {\n\t\t\tmin-width: 300px;\n\t\t\tmin-height: 250px;\n\t\t}\n\t}\n\t@media only screen and (min-width: 640px) and (min-height: 480px) {\n\t\tdiv[id^=\"bsa-zone_1611170977806-3_123456\"] {\n\t\t\tmin-width: 300px;\n\t\t\tmin-height: 250px;\n\t\t}\n\t}\n<\/style>\n<div id=\"bsa-zone_1611170977806-3_123456\"><\/div>\n\n\n<\/div>\n<!-- Time: 1.215934753418E-5, Pos: 233, Key: ad_unit_1 -->\n\n\n<p>It also supports using a proxy. This means that curl requests can go through a proxy server just like a web browser does when configured to use a proxy.<\/p>\n<p>Using a proxy with curl is very simple and straight forward. Lets take a look at a simple curl request in php that fetches a url.<\/p>\n<h3>Fetch url directly<\/h3>\n<p>The following program fetches the contents of a URL directly, without using any proxy.<\/p>\n<pre class=\"source-code\" >\/\/gets the data from a URL\r\nfunction get_url($url) \r\n{\r\n    $ch = curl_init();\r\n     \r\n    if($ch === false)\r\n    {\r\n        die(&#039;Failed to create curl object&#039;);\r\n    }\r\n     \r\n    $timeout = 5;\r\n    curl_setopt($ch, CURLOPT_URL, $url);\r\n    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);\r\n    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);\r\n    $data = curl_exec($ch);\r\n    curl_close($ch);\r\n    return $data;\r\n}\r\n \r\necho get_url(&quot;http:\/\/www.apple.com\/&quot;);<\/pre>\n<p>Now that same request can be made to go through a proxy. Now proxies are of 2 kinds, SOCKS and HTTP. Curl can use either type. Curl needs to be told the ip address and port number of the proxy server. To run the next piece of code you would need a working proxy server. <\/p>\n<p>I would recommend using TOR, which is the best proxy\/anonymity service out there and it is always in a working condition. You do not have to search for working proxies here and there instead. Just install tor and start it. <\/p>\n<p>If you got some other working proxy that too will work just fine.<\/p>\n<h3>Use socks proxy with curl<\/h3>\n<p>This code example demonstrates how to use a socks proxy with curl in php<\/p>\n<pre class=\"source-code\" >$url = &quot;http:\/\/www.ipmango.com\/api\/myip&quot;;\r\n$ch = curl_init();\r\n\t\r\nif($ch === false)\r\n{\r\n\tdie(&#039;Failed to create curl object&#039;);\r\n}\r\n\t\r\n$timeout = 5;\r\ncurl_setopt($ch, CURLOPT_URL, $url);\r\ncurl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);\r\ncurl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);\r\n\r\n\/\/proxy details\r\ncurl_setopt($ch, CURLOPT_PROXY, &#039;localhost:9050&#039;);\r\ncurl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);\r\n\r\n$data = curl_exec($ch);\r\ncurl_close($ch);\r\n\r\necho $data;<\/pre>\n<p>The above code fetches the remote url <a href=\"http:\/\/www.ipmango.com\/api\/myip\">http:\/\/www.ipmango.com\/api\/myip<\/a> which tells the public ip of the requesting machine. If a proper proxy is running on localhost at 9050 (tor in this case) then the returned ip should be a different ip than the real ip of the user. <\/p>\n<p>To see the difference, comment out the proxy details and compare the ip addresses returned.<\/p>\n\n<!-- SMARTADDR: No ad unit (ad_unit_2) was added at location: 3733. Content Length: 3414 -->\n<!-- SMARTADDR: No ad unit (ad_unit_3) was added at location: 3733. Content Length: 3507 -->\n<!-- SMARTADDR: No ad unit (ad_unit_4) was added at location: 3733. Content Length: 3600 -->\n<!-- SMARTADDR: No ad unit (ad_unit_5) was added at location: 3733. Content Length: 3693 -->","protected":false},"excerpt":{"rendered":"<p>Proxy with Curl Curl is a very useful library for transferring data over various protocols like http, ftp, https etc. In other words curl can be used to programatically download a web page, or upload file to ftp etc. It also supports using a proxy. This means that curl requests can go through a proxy&#8230; <span class=\"read-more\"><a href=\"https:\/\/www.binarytides.com\/use-proxy-with-curl-php\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":15958,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[322],"tags":[],"class_list":["post-4224","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to use proxy with curl in Php - BinaryTides<\/title>\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.binarytides.com\/use-proxy-with-curl-php\/\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Silver Moon\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.binarytides.com\/use-proxy-with-curl-php\/\",\"url\":\"https:\/\/www.binarytides.com\/use-proxy-with-curl-php\/\",\"name\":\"How to use proxy with curl in Php - BinaryTides\",\"isPartOf\":{\"@id\":\"https:\/\/www.binarytides.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.binarytides.com\/use-proxy-with-curl-php\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.binarytides.com\/use-proxy-with-curl-php\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.binarytides.com\/blog\/wp-content\/uploads\/2023\/09\/no-thumbnail.jpg\",\"datePublished\":\"2013-03-11T14:24:10+00:00\",\"dateModified\":\"2023-05-01T14:16:28+00:00\",\"author\":{\"@id\":\"https:\/\/www.binarytides.com\/#\/schema\/person\/ce24c6ddfa0368f9a08bcf46505884dd\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.binarytides.com\/use-proxy-with-curl-php\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.binarytides.com\/use-proxy-with-curl-php\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.binarytides.com\/use-proxy-with-curl-php\/#primaryimage\",\"url\":\"https:\/\/www.binarytides.com\/blog\/wp-content\/uploads\/2023\/09\/no-thumbnail.jpg\",\"contentUrl\":\"https:\/\/www.binarytides.com\/blog\/wp-content\/uploads\/2023\/09\/no-thumbnail.jpg\",\"width\":1920,\"height\":1080},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.binarytides.com\/use-proxy-with-curl-php\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.binarytides.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to use proxy with curl in Php\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.binarytides.com\/#website\",\"url\":\"https:\/\/www.binarytides.com\/\",\"name\":\"BinaryTides\",\"description\":\"News, Technology, Entertainment and more\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.binarytides.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.binarytides.com\/#\/schema\/person\/ce24c6ddfa0368f9a08bcf46505884dd\",\"name\":\"Silver Moon\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.binarytides.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/67ac3d58b656585dc0201e900a67f4197eb0c3ef2d1f83dd8f95a0b497cd97da?s=96&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/67ac3d58b656585dc0201e900a67f4197eb0c3ef2d1f83dd8f95a0b497cd97da?s=96&r=g\",\"caption\":\"Silver Moon\"},\"description\":\"A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at binarytides@gmail.com.\",\"url\":\"https:\/\/www.binarytides.com\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to use proxy with curl in Php - BinaryTides","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.binarytides.com\/use-proxy-with-curl-php\/","twitter_misc":{"Written by":"Silver Moon","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.binarytides.com\/use-proxy-with-curl-php\/","url":"https:\/\/www.binarytides.com\/use-proxy-with-curl-php\/","name":"How to use proxy with curl in Php - BinaryTides","isPartOf":{"@id":"https:\/\/www.binarytides.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.binarytides.com\/use-proxy-with-curl-php\/#primaryimage"},"image":{"@id":"https:\/\/www.binarytides.com\/use-proxy-with-curl-php\/#primaryimage"},"thumbnailUrl":"https:\/\/www.binarytides.com\/blog\/wp-content\/uploads\/2023\/09\/no-thumbnail.jpg","datePublished":"2013-03-11T14:24:10+00:00","dateModified":"2023-05-01T14:16:28+00:00","author":{"@id":"https:\/\/www.binarytides.com\/#\/schema\/person\/ce24c6ddfa0368f9a08bcf46505884dd"},"breadcrumb":{"@id":"https:\/\/www.binarytides.com\/use-proxy-with-curl-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.binarytides.com\/use-proxy-with-curl-php\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.binarytides.com\/use-proxy-with-curl-php\/#primaryimage","url":"https:\/\/www.binarytides.com\/blog\/wp-content\/uploads\/2023\/09\/no-thumbnail.jpg","contentUrl":"https:\/\/www.binarytides.com\/blog\/wp-content\/uploads\/2023\/09\/no-thumbnail.jpg","width":1920,"height":1080},{"@type":"BreadcrumbList","@id":"https:\/\/www.binarytides.com\/use-proxy-with-curl-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.binarytides.com\/"},{"@type":"ListItem","position":2,"name":"How to use proxy with curl in Php"}]},{"@type":"WebSite","@id":"https:\/\/www.binarytides.com\/#website","url":"https:\/\/www.binarytides.com\/","name":"BinaryTides","description":"News, Technology, Entertainment and more","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.binarytides.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.binarytides.com\/#\/schema\/person\/ce24c6ddfa0368f9a08bcf46505884dd","name":"Silver Moon","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.binarytides.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/67ac3d58b656585dc0201e900a67f4197eb0c3ef2d1f83dd8f95a0b497cd97da?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/67ac3d58b656585dc0201e900a67f4197eb0c3ef2d1f83dd8f95a0b497cd97da?s=96&r=g","caption":"Silver Moon"},"description":"A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at binarytides@gmail.com.","url":"https:\/\/www.binarytides.com\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.binarytides.com\/wp-json\/wp\/v2\/posts\/4224","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.binarytides.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.binarytides.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.binarytides.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.binarytides.com\/wp-json\/wp\/v2\/comments?post=4224"}],"version-history":[{"count":2,"href":"https:\/\/www.binarytides.com\/wp-json\/wp\/v2\/posts\/4224\/revisions"}],"predecessor-version":[{"id":14932,"href":"https:\/\/www.binarytides.com\/wp-json\/wp\/v2\/posts\/4224\/revisions\/14932"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.binarytides.com\/wp-json\/wp\/v2\/media\/15958"}],"wp:attachment":[{"href":"https:\/\/www.binarytides.com\/wp-json\/wp\/v2\/media?parent=4224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.binarytides.com\/wp-json\/wp\/v2\/categories?post=4224"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.binarytides.com\/wp-json\/wp\/v2\/tags?post=4224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}