/ Published in: PHP
URL: http://bcmoney-mobiletv.com/blog/2009/05/01/the-server-side-proxy/
Source to get me started on a PHP proxy using CURL: http://developer.yahoo.com/javascript/howto-proxy.html
Nice tutorial on using PHP proxy for cross-server JSON without JSONp: http://benalman.com/projects/php-simple-proxy/
SalesForce.com's full-blown CORS and CRUD supporting REST toolkit proxy: https://github.com/developerforce/Force.com-JavaScript-REST-Toolkit/blob/master/proxy.php
Expand |
Embed | Plain Text
<?php /** * proxy.php * Acts as a server-side requestor for data on behalf of the client-side, in order to get around the "same-origin" problem * (NOTE: there could be a small security risk by doing a naiive REQUEST to pass the proxy URL without POST + SSL and more thorough validation. Only if an attacker knew the location of this script, would there be a chance they can use it as a proxy for attacks to other servers, or this server. For our purposes, it probably is negligible, but for more on how to solve potential issues, see: http://php.net/manual/en/function.fopen.php or: http://www.virtualforge.de/vmovie/xss_selling_platform_v1.0.php) */ $url = $_REQUEST['url']; //URL to grab (again, see NOTE on security above) if (empty($url)) { $url = "http://dd.weatheroffice.ec.gc.ca/citypage_weather/xml/NB/s0000687_e.xml"; } //make sure we always get some data (default to a Weather feed) /** * getAddress * @get the full url of the current page (protocol + host + request URI including parameters) * @return string */ function getAddress() { $protocol = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http'; /*** check for https ***/ return $protocol.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; /*** return the full address ***/ } //parse the passed in URL using parameterized query object (could add validation here, see: http://www.scriptol.com/how-to/parsing-url.php ) $parameters = $arr["query"]; $format = $param['f']; //examples: &f=xml | &f=json | &f=html (for more MIME-Types, see: http://en.wikipedia.org/wiki/Mime_type) $encoding = $param['e']; //examples: &e=utf-8 | &e=iso-8859-1 | &e=Shift-JIS (for more Character Encodings, see: http://en.wikipedia.org/wiki/Character_encoding) $e = (!empty($encoding)) ? $encoding : "utf-8"; //might want to limit allowed charset/encoding types // Set your return content type, based on the expected response type... switch ($format) { case "xml": case "xsl": case "kml": break; case "geojson": case "json": case "rdfjson": case "rdf/json": case "rdf+json": case "jron": break; case "georss": case "atom": break; case "rss": case "rss2": case "rss2.0": case "rss1": case "rss1.0": case "rss0.92": case "rss0.91": case "rss0.90": case "feed": case "rdf": break; case "owl": case "rdf+xml": case "rdfxml": break; case "swf": case "flash": case "flv": break; case "image": break; case "svg": break; case "audio": case "ogg": break; case "mp3": break; case "video": case "webm": break; case "mp4": break; case "xhtml": break; case "xslt": case "html": case "html5": break; default: header("Content-type: text/plain; charset={$e}"); //could be any other plaintext format (including: CSV, TSV, conf, ini, rtf, txt, dat, n3, turtle, JSONp etc...) break; } try { // Get remote content/data (NOTE: your hosting provider may not allow fopen, if not you can request they allow for your VPS...if still not, we can use file_get_contents or CURL lib instead) // some content/data was received, then read & return if ($handle) { echo $buffer; } } } catch (Exception $e) { } ?>
You need to login to post a comment.
