Created
October 8, 2014 07:33
-
-
Save krizpoon/9430c0384f6a307cadd3 to your computer and use it in GitHub Desktop.
Getting content from URL through a PHP proxy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function val($array, $key) | |
{ | |
if (!$array) return null; | |
if (!isset($array[$key])) return null; | |
return $array[$key]; | |
} | |
// For proxy purpose, $excludeHeaders = array('Date', 'Server', 'X-Powered-By', 'Transfer-Encoding') | |
function TSParseCurlResponse($res, $excludeHeaders = null) | |
{ | |
try | |
{ | |
list($headers, $body) = explode("\r\n\r\n", $res, 2); | |
$headerDict = array(); | |
$headerLines = explode("\r\n", $headers); | |
$statusText = null; | |
for ($i=0; $i<count($headerLines); $i++) | |
{ | |
$header = $headerLines[$i]; | |
if ($i == 0) | |
{ | |
$statusText = $header; | |
} | |
else | |
{ | |
if (!$header) continue; | |
$split = explode(':', $header, 2); | |
if (count($split) != 2) continue; | |
$headerName = trim($split[0]); | |
$headerValue = trim($split[1]); | |
// check if header should be ignored | |
if ($excludeHeaders && in_array($headerName, $excludeHeaders)) continue; | |
// add to result | |
$headerDict[$headerName] = $headerValue; | |
} | |
} | |
if ($statusText) | |
{ | |
$splits = explode(' ', $statusText, 3); | |
if (count($splits) == 3) | |
{ | |
$statusCode = $splits[1]; | |
$statusText = $splits[2]; | |
} | |
} | |
} | |
catch (Exception $ex) | |
{ | |
$json['error'] = $ex->getMessage(); | |
} | |
$ret = array(); | |
$ret['responseText'] = $body; | |
$ret['statusText'] = $statusText; | |
$ret['statusCode'] = $statusCode; | |
$ret['headers'] = $headerDict; | |
return $ret; | |
} | |
/** | |
* TSCurl executes a curl command | |
* | |
* @param $url URL | |
* @param $opt.method HTTP method | |
* @param $opt.cookie Cookie header | |
* @param $opt.contentType Content type header | |
* @param $opt.referer Referer header | |
* @param $opt.params Keyed array to build the query string | |
* @param $opt.payload Raw content for post method | |
* @param $opt.parseResponse if true, the returned response is passed to TSParseCurlResponse | |
* @param $opt.excludeHeaders if $opt.parseResponse is true, excludeHeaders will be passed to TSParseCurlResponse | |
* | |
* @returns HTTP resonse (with raw response header lines) | |
*/ | |
function TSCurl($url, $opt = null) | |
{ | |
$ch = curl_init($url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); | |
curl_setopt($ch, CURLOPT_HEADER, TRUE); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); | |
// set referer | |
$referer = val($opt, 'referer'); | |
if ($referer) curl_setopt($ch, CURLOPT_REFERER, $referer); | |
// set cookie | |
$cookie = val($opt, 'cookie'); | |
curl_setopt ($ch, CURLOPT_COOKIE, $cookie); | |
// get payload or build payload from params | |
$payload = val($opt, 'payload'); | |
if (!$payload) | |
{ | |
$params = val($opt, 'params'); | |
if ($params) | |
{ | |
$payload = http_build_query($params); | |
} | |
} | |
// get method | |
$method = val($opt, 'method'); | |
if ($method == 'post') | |
{ | |
$contentType = val($opt, 'contentType'); | |
if (!$contentType) $contentType = 'application/x-www-form-urlencoded'; | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: $contentType; charset=utf-8")); | |
if ($payload) curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); | |
} | |
else | |
{ | |
if ($payload) | |
{ | |
$connector = strpos($url, '?') === FALSE? '?': '&'; | |
$url = $url . $connector . $payload; | |
curl_setopt($ch, CURLOPT_URL, $url); | |
} | |
} | |
$ret = curl_exec($ch); | |
if ($ret === false) | |
{ | |
$err = curl_error($ch); | |
curl_close($ch); | |
throw new Exception($err); | |
} | |
else | |
{ | |
curl_close($ch); | |
$parse = !!val($opt, 'parse'); | |
if ($parse) | |
{ | |
$excludeHeaders = val($opt, 'excludeHeaders'); | |
$ret = TSParseCurlResponse($ret, $excludeHeaders); | |
} | |
return $ret; | |
} | |
} | |
function TSOutputCurlResponse($res) | |
{ | |
if (gettype($res) == 'string') | |
{ | |
$res = TSParseCurlResponse($res, array('Date', 'Server', 'X-Powered-By', 'Transfer-Encoding')); | |
} | |
$headerDict = $res['headers']; | |
$body = $res['responseText']; | |
$statusText = $ret['statusText']; | |
$statusCode = $ret['statusCode']; | |
header("$statusCode $statusText"); | |
if ($headerDict) foreach ($headerDict as $name => $value) | |
{ | |
header("$name: $value"); | |
} | |
echo $body; | |
} | |
function TSUrlRelay($url, $referer = null) | |
{ | |
if (!$referer) | |
{ | |
$pos = strrpos($url, '/'); | |
if ($pos > 0) $referer = substr($url, 0, $pos + 1); | |
} | |
$res = TSCurl($url, array('referer' => $referer, 'params' => $_REQUEST)); | |
TSOutputCurlResponse($res); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment