Skip to content

Instantly share code, notes, and snippets.

@clawfire
Created February 22, 2012 18:25
Show Gist options
  • Save clawfire/1886491 to your computer and use it in GitHub Desktop.
Save clawfire/1886491 to your computer and use it in GitHub Desktop.
<?php
$doc = fopen('./debug.log', 'w+b');
fwrite($doc, "GENERATED AT ".time()."\n\n");
require_once('./curl.php');
/**
*
*/
function APICALL($methode,$url,$data,$encod=null){
global $doc;
fwrite($doc, "Appel de la fonction APICALL\n");
/*-------------------------------------------------------------*/
$methode = strtoupper($methode);
fwrite($doc, "methode $methode\n");
$url = urldecode($url);
fwrite($doc, "url $url\n");
$data = json_decode($data,true);
fwrite($doc, "data ".print_r($data,true)."\n");
/*-------------------------------------------------------------*/
$curl = new Curl;
$curl->headers['Accept'] = "application/json";
$curl->headers['Content-Type'] = "application/json";
$curl->options['returntransfer'] = true;
$curl->follow_redirects = false;
/*-------------------------------------------------------------*/
switch ($methode) {
case 'GET':
echo $curl->get($url,$data);
break;
case 'PUT':
$reponse = $curl->put($url,json_encode($data));
setHeader($reponse->headers);
echo $reponse->body;
break;
case 'DELETE':
echo $curl->delete($url,$data);
break;
case 'POST':
fwrite($doc, "envoi d'un POST CURL \n");
echo $curl->post($url,$data,$encod);
break;
default:
echo "...";
break;
}
}
/**
* Fonction de parametrage des headers
*
* @author Thibault Milan
* @version 1.0
* @param array $headers
*/
function setHeader($headers){
header("HTTP/".$headers['Http-Version']." ".$headers['Status']);
header("Content-Type: ".$headers['Content-Type']);
header("Content-Length: ".$headers['Content-Length']);
}
/**
* Fonction de test d'ip
*
* @description Teste si l'IP courante fait partie du tableau passé en entrée.
* @version 1.0
* @author Thibault Milan
* @param array $ip tableau d'adresse IP autorisées
* @return bool
*/
function testIP($ip){
for($i=0, $cnt=count($ip); $i<$cnt; $i++) {
$ipregex = preg_replace("/\./", "\.", $ip[$i]);
$ipregex = preg_replace("/\*/", ".*", $ipregex);
if(preg_match('/^'.$ipregex.'/', $_SERVER['REMOTE_ADDR']))
return true;
}
return false;
}
/*-------------------------------------------------------------*/
/*----------------------------------------------
Tableaux d'ip autorisées a faire les req.
---------------------------------------------*/
$ip[] = '10.234.234.151';
$ip[] = '127.0.0.1';
$ip[] = '172.20.1.194';
$ip[] = '172.20.1.195';
/*-------------------------------------------------------------*/
if (testIP($ip))APICALL($_GET['m'],$_GET['u'],$_GET['p'],(isset($_GET['e'])) ? $_GET['e'] : null);
else header("Location: http://rickrolled.fr/");
fclose($doc);
?>
<?php
/**
* A basic CURL wrapper
*
* See the README for documentation/examples or http://php.net/curl for more information about the libcurl extension for PHP
*
* @package curl
* @author Sean Huber <shuber@huberry.com>
**/
class Curl {
/**
* The file to read and write cookies to for requests
*
* @var string
**/
public $cookie_file;
/**
* Determines whether or not requests should follow redirects
*
* @var boolean
**/
public $follow_redirects = true;
/**
* An associative array of headers to send along with requests
*
* @var array
**/
public $headers = array();
/**
* An associative array of CURLOPT options to send along with requests
*
* @var array
**/
public $options = array();
/**
* The referer header to send along with requests
*
* @var string
**/
public $referer;
/**
* The user agent to send along with requests
*
* @var string
**/
public $user_agent;
/**
* Stores an error string for the last request if one occurred
*
* @var string
* @access protected
**/
protected $error = '';
/**
* Stores resource handle for the current CURL request
*
* @var resource
* @access protected
**/
protected $request;
/**
* Initializes a Curl object
*
* Sets the $cookie_file to "curl_cookie.txt" in the current directory
* Also sets the $user_agent to $_SERVER['HTTP_USER_AGENT'] if it exists, 'Curl/PHP '.PHP_VERSION.' (http://github.com/shuber/curl)' otherwise
**/
function __construct() {
$this->cookie_file = dirname(__FILE__).DIRECTORY_SEPARATOR.'curl_cookie.txt';
$this->user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Curl/PHP '.PHP_VERSION.' (http://github.com/shuber/curl)';
}
/**
* Makes an HTTP DELETE request to the specified $url with an optional array or string of $vars
*
* Returns a CurlResponse object if the request was successful, false otherwise
*
* @param string $url
* @param array|string $vars
* @return CurlResponse object
**/
function delete($url, $vars = array()) {
return $this->request('DELETE', $url, $vars);
}
/**
* Returns the error string of the current request if one occurred
*
* @return string
**/
function error() {
return $this->error;
}
/**
* Makes an HTTP GET request to the specified $url with an optional array or string of $vars
*
* Returns a CurlResponse object if the request was successful, false otherwise
*
* @param string $url
* @param array|string $vars
* @return CurlResponse
**/
function get($url, $vars = array()) {
if (!empty($vars)) {
$url .= (stripos($url, '?') !== false) ? '&' : '?';
$url .= (is_string($vars)) ? $vars : http_build_query($vars, '', '&');
}
return $this->request('GET', $url);
}
/**
* Makes an HTTP HEAD request to the specified $url with an optional array or string of $vars
*
* Returns a CurlResponse object if the request was successful, false otherwise
*
* @param string $url
* @param array|string $vars
* @return CurlResponse
**/
function head($url, $vars = array()) {
return $this->request('HEAD', $url, $vars);
}
/**
* Makes an HTTP POST request to the specified $url with an optional array or string of $vars
*
* @param string $url
* @param array|string $vars
* @param string $enctype
* @return CurlResponse|boolean
**/
function post($url, $vars = array(), $enctype = NULL) {
global $doc;
fwrite($doc, "Entrée dans la function CURL POST\nurl = $url\nencodage = $enctype\nvars = $vars\n");
return $this->request('POST', $url, $vars, $enctype);
}
/**
* Makes an HTTP PUT request to the specified $url with an optional array or string of $vars
*
* Returns a CurlResponse object if the request was successful, false otherwise
*
* @param string $url
* @param array|string $vars
* @return CurlResponse|boolean
**/
function put($url, $vars = array()) {
return $this->request('PUT', $url, $vars);
}
/**
* Makes an HTTP request of the specified $method to a $url with an optional array or string of $vars
*
* Returns a CurlResponse object if the request was successful, false otherwise
*
* @param string $method
* @param string $url
* @param array|string $vars
* @return CurlResponse|boolean
**/
function request($method, $url, $vars = array(), $enctype = NULL) {
global $doc;
fwrite($doc, "Entrée dans la function CURL request\nmethode = $method\nurl = $url\nencodage = $enctype\nvars = $vars\n");
$this->error = '';
$this->request = curl_init();
if (is_array($vars) && $enctype != 'multipart/form-data') $vars = http_build_query($vars, '', '&');
fwrite($doc, "Vars en chaine de caract. = $vars\n");
$this->set_request_method($method);
$this->set_request_options($url, $vars);
$this->set_request_headers();
$response = curl_exec($this->request);
fwrite($doc, "Execution appel CURL\n");
if ($response) {
$response = new CurlResponse($response);
fwrite($doc, "Appel REUSSIS\nReponse = $response\n");
} else {
$this->error = curl_errno($this->request).' - '.curl_error($this->request);
fwrite($doc, "ERREUR d'appel !!!\nErreur = $this->error\n");
}
curl_close($this->request);
return $response;
}
/**
* Formats and adds custom headers to the current request
*
* @return void
* @access protected
**/
protected function set_request_headers() {
$headers = array();
foreach ($this->headers as $key => $value) {
$headers[] = $key.': '.$value;
}
curl_setopt($this->request, CURLOPT_HTTPHEADER, $headers);
}
/**
* Set the associated CURL options for a request method
*
* @param string $method
* @return void
* @access protected
**/
protected function set_request_method($method) {
global $doc;
switch (strtoupper($method)) {
case 'HEAD':
curl_setopt($this->request, CURLOPT_NOBODY, true);
break;
case 'GET':
curl_setopt($this->request, CURLOPT_HTTPGET, true);
break;
case 'POST':
fwrite($doc, "SET CURL METHOD POST\n");
curl_setopt($this->request, CURLOPT_POST, true);
break;
default:
curl_setopt($this->request, CURLOPT_CUSTOMREQUEST, $method);
}
}
/**
* Sets the CURLOPT options for the current request
*
* @param string $url
* @param string $vars
* @return void
* @access protected
**/
protected function set_request_options($url, $vars) {
global $doc;
fwrite($doc, "Set request options functions\n");
curl_setopt($this->request, CURLOPT_URL, $url);
if (!empty($vars)){
fwrite($doc, "Set PostFields to $vars\n");
curl_setopt($this->request, CURLOPT_POSTFIELDS, $vars);
}
# Set some default CURL options
curl_setopt($this->request, CURLOPT_HEADER, true);
curl_setopt($this->request, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->request, CURLOPT_USERAGENT, $this->user_agent);
if ($this->cookie_file) {
curl_setopt($this->request, CURLOPT_COOKIEFILE, $this->cookie_file);
curl_setopt($this->request, CURLOPT_COOKIEJAR, $this->cookie_file);
}
if ($this->follow_redirects) curl_setopt($this->request, CURLOPT_FOLLOWLOCATION, true);
if ($this->referer) curl_setopt($this->request, CURLOPT_REFERER, $this->referer);
# Set any custom CURL options
foreach ($this->options as $option => $value) {
curl_setopt($this->request, constant('CURLOPT_'.str_replace('CURLOPT_', '', strtoupper($option))), $value);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment