Skip to content

Instantly share code, notes, and snippets.

@mk2
Created September 5, 2013 08:06
Show Gist options
  • Save mk2/33c771e6835fb660e3ec to your computer and use it in GitHub Desktop.
Save mk2/33c771e6835fb660e3ec to your computer and use it in GitHub Desktop.
Proxy PHP
<?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;
}
@mk2
Copy link
Author

mk2 commented Jun 30, 2014

簡易的なプロキシースクリプト。あまり使いものにならないので、nodejsのcorsproxyを使うこと。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment