Created
September 5, 2013 08:06
-
-
Save mk2/33c771e6835fb660e3ec to your computer and use it in GitHub Desktop.
Proxy PHP
This file contains 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 | |
/** | |
* Created by IntelliJ IDEA. | |
* User: okabayashi | |
* Date: 13/07/09 | |
* Time: 10:47 | |
*/ | |
class APIRequestProxy | |
{ | |
private $log; | |
function __construct() | |
{ | |
} | |
function request($url, $method, $contentType = null, array $data = null) | |
{ | |
$opts = null; | |
if ($method === 'GET') { | |
$url .= ($data) ? '?' . urldecode(http_build_query($data)) : ''; | |
$opts = array( | |
'http' => array( | |
'method' => $method, | |
)); | |
} else if ($method === 'POST') { | |
$opts = array( | |
'http' => array( | |
'method' => $method, | |
'header' => $contentType, | |
'content' => http_build_query($data) | |
) | |
); | |
} | |
$result = @file_get_contents($url, false, stream_context_create($opts)); | |
return array($result, $http_response_header); | |
} | |
} | |
if (basename(__FILE__) === basename($_SERVER['PHP_SELF'])) { | |
$requestProxy = new APIRequestProxy(); | |
$url = $_REQUEST['url']; | |
$method = $_REQUEST['method']; | |
$contentType = (@$_REQUEST['type']) ? : null; | |
$data = (@$_REQUEST['data']) ? : null; | |
list($response, $header) = $requestProxy->request($url, $method, $contentType, $data); | |
// 結果を返す | |
header('Content-Type: application/json; charset="UTF-8"'); | |
header('Expires: 0'); | |
header('Cache-Control: must-revalidate'); | |
$jsons = json_encode(array('response' => $response, 'header' => $header)); | |
echo $jsons; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
簡易的なプロキシースクリプト。あまり使いものにならないので、nodejsのcorsproxyを使うこと。