{"id":1207,"date":"2013-06-20T12:06:45","date_gmt":"2013-06-20T12:06:45","guid":{"rendered":"http:\/\/garysieling.com\/blog\/?p=1207"},"modified":"2013-06-20T12:06:45","modified_gmt":"2013-06-20T12:06:45","slug":"proxying-http-requests-with-php","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/proxying-http-requests-with-php\/","title":{"rendered":"Proxying HTTP requests with PHP"},"content":{"rendered":"<p>The following code will proxy requests to an external API. <\/p>\n<p>This has several advantages: <\/p>\n<ul>\n<li>Control over an API key<\/li>\n<li>Set caching headers to prevent overuse of an API<\/li>\n<li>Prevent issues with cross-domain scripting errors<\/li>\n<li>Limit the scope of what APIs can be called through your proxy<\/li>\n<\/ul>\n<pre lang=\"php\">\n$query = urlencode($_GET['query']);\n$url = '';\n\n$url = \"http:\/\/blekko.com\/ws\/?q=%22\" . $query . \"%22+\/json&auth=<AUTH>\";\nset_time_limit(30);\n\n$s = fopen($url,\"rb\");\n\n$seconds_to_cache = 3600 * 24;\n$ts = gmdate(\"D, d M Y H:i:s\", time() + $seconds_to_cache) . \" GMT\";\nheader(\"Expires: $ts\");\nheader(\"Pragma: cache\");\nheader(\"Cache-Control: max-age=$seconds_to_cache\");\n\nforeach ($http_response_header as $h) {\n        header($h);\n}\n\nheader($h);\n\nwhile (!feof($s)) {\n        echo fread($s, 4096);\n}\n\nfclose($s);\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The following code will proxy requests to an external API. This has several advantages: Control over an API key Set caching headers to prevent overuse of an API Prevent issues with cross-domain scripting errors Limit the scope of what APIs can be called through your proxy $query = urlencode($_GET[&#8216;query&#8217;]); $url = &#8221;; $url = &#8220;http:\/\/blekko.com\/ws\/?q=%22&#8221; &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/proxying-http-requests-with-php\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Proxying HTTP requests with PHP&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[53,278,322,432,444,495],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/1207"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=1207"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/1207\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=1207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=1207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=1207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}