Skip to content

Instantly share code, notes, and snippets.

@daohoangson
Last active September 20, 2018 07:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daohoangson/b353e582b9e6fd97a3c5b12f98e916c3 to your computer and use it in GitHub Desktop.
Save daohoangson/b353e582b9e6fd97a3c5b12f98e916c3 to your computer and use it in GitHub Desktop.
Simple PHP proxy script
<?php
$url = 'https://httpbin.org/anything?';
function proxyDoHeader($curl, $headerLine)
{
$trimmed = trim($headerLine);
if (strlen($trimmed) > 0 && !preg_match('/^(Date|Server|Transfer)/', $trimmed)) {
header($trimmed);
}
return strlen($headerLine);
}
foreach ($_GET as $key => $value) {
$url .= sprintf('%s=%s&', $key, urlencode($value));
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'proxyDoHeader');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$headers = array();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
curl_setopt($ch, CURLOPT_POST, 1);
if (!empty($_SERVER['CONTENT_TYPE']) &&
$_SERVER['CONTENT_TYPE'] === 'application/json'
) {
$headers[] = 'Content-Type: ' . $_SERVER['CONTENT_TYPE'];
$json = file_get_contents('php://input');
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
} else {
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
$postFields = '';
foreach ($_POST as $key => $value) {
$postFields .= sprintf('%s=%s&', $key, urlencode($value));
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
}
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$contents = curl_exec($ch);
curl_close($ch);
echo($contents);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment