Advertisement
Guest User

PHP REST PROXY

a guest
Mar 16th, 2020
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.02 KB | None | 0 0
  1. <?php
  2.     $url_source = "http://IP:PORT";
  3.     $url = str_replace($_SERVER['SCRIPT_NAME'], $url_source, $_SERVER['REQUEST_URI']);
  4.  
  5.     function HandleHeaderLine( $curl, $header_line ) {
  6.         header($header_line, true);
  7.         return strlen($header_line);
  8.     }
  9.  
  10.     $ch = curl_init();
  11.     curl_setopt($ch, CURLOPT_URL, $url);
  12.     curl_setopt($ch, CURLOPT_HEADERFUNCTION, "HandleHeaderLine");
  13.     $headers = array();
  14.     foreach (getallheaders() as $header => $value) {
  15.         $headers[] = $header.": ".$value;
  16.     }
  17.     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  18.     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $_SERVER['REQUEST_METHOD']);
  19.     if($_SERVER['REQUEST_METHOD'] == 'PUT') {
  20.         $fp = fopen('php://input', 'r');
  21.  
  22.         curl_setopt($ch, CURLOPT_PUT, true);
  23.         curl_setopt($ch, CURLOPT_INFILE, $fp);
  24.         curl_setopt($ch, CURLOPT_INFILESIZE, (int)$_SERVER['CONTENT_LENGTH']);
  25.     } else {
  26.         curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
  27.     }
  28.     curl_exec($ch);
  29.     curl_close($ch);
  30. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement