Kısaca neler yapabilir?- "safe_mode" bakmaksızın FOLLOWLOCATION işlemini yerine getirebilir.
- Parametre alımını GET ve POST modüllerin array tipinde alabilir.
- cURL ile gelen header ile body değerlerini ayrıştırarak saklar.
- En çok kullanılan parametreleri hazır değişkenler ile ayarlayabilirsiniz.
- "curl_reset" fonksiyonuna bakmasızın bu işlemi gerçekleştirebilir.
<?php
/*
* Created by Anılcan ÇAKIR for Webools.
* This code can't change.
*
* Website: http://anilcancakir.com or http://anilcan.webools.com
* E-Mail: anilcan@webools.com
*
* Project: WO_cURL
* Version: 1.0.0
*
* File date: 12.3.2015
* File time: 23:24
*/
class WO_cURL {
/* Ana değişkenler */
protected $safe_mode = true;
protected $curl;
protected $clean = false;
public $result;
/* Değiştirilebilir cURL ana değerleri */
public $referer;
public $ssl = false;
public $return_transfer = true;
public $timeout = 30;
public $headers = array();
public $options = array();
public $follow_location = true;
/* Çıktı değişkenleri */
public $header_size;
public $body;
public $header;
public function __construct() {
if (!function_exists('curl_init')){
die('cURL kutuphanesi sunucunuzda aktif degil, lutfen sunucu saglayiciniz ile iletisime gecin.');
exit;
}
if(ini_get('open_basedir') != '' AND ini_get('safe_mode' == 'Off'))
{
$this->safe_mode = true;
}
$this->referer = $this->_getBaseURL();
$this->curl = curl_init();
}
public function __destruct() {
$this->close();
}
public function POST($url, array $post = array()) {
$this->_setOptions();
$post_var = '';
if(count($post) > 0) {
$post_var = $this->http_build_multi_query($post);
}
curl_setopt($this->curl, CURLOPT_URL, $url);
curl_setopt($this->curl, CURLOPT_POST, TRUE);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $post_var);
$this->result = curl_exec($this->curl);
$this->header_size = curl_getinfo($this->curl, CURLINFO_HEADER_SIZE);
$this->header = substr($this->result, 0, $this->header_size);
$this->body = substr($this->result, $this->header_size);
}
public function GET($url, array $query = array()) {
$this->_setOptions();
if(count($query) > 0) {
$url = $url . '?' . $this->http_build_multi_query($query);
}
curl_setopt($this->curl, CURLOPT_URL, $url);
if($this->follow_location) {
if($this->safe_mode) {
$code = 301;
curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, FALSE);
while($code <> 0)
{
$this->_setOptions();
curl_setopt($this->curl, CURLOPT_URL, $url);
$this->result = curl_exec($this->curl);
if(curl_errno($this->curl))
{
$code = 0;
}
else
{
$code = curl_getinfo($this->curl, CURLINFO_HTTP_CODE);
if($code == 301 || $code == 302)
{
preg_match('/Location:(.*?)\n/i', $this->result, $matches);
if(isset($matches[1]) AND !empty($matches[1]))
{
$url = trim($matches[1]);
}
else
{
$code = 0;
}
}
else
{
$code = 0;
}
}
}
}
else {
curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, TRUE);
$this->result = curl_exec($this->curl);
}
}
$this->header_size = curl_getinfo($this->curl, CURLINFO_HEADER_SIZE);
$this->header = substr($this->result, 0, $this->header_size);
$this->body = substr($this->result, $this->header_size);
$this->clean = false;
}
public function json() {
return json_decode($this->body);
}
public function close() {
curl_close($this->curl);
}
public function clean() {
$this->clean = true;
}
protected function _setOptions() {
if(function_exists('curl_reset')) {
curl_reset($this->curl);
}
else {
if(function_exists('curl_close')) {
curl_close($this->curl);
$this->curl = curl_init();
}
else {
$this->curl = curl_init();
}
}
if(!$this->clean) {
if(is_array($this->headers)) {
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $this->headers);
}
elseif(is_string($this->headers)) {
curl_setopt($this->curl, CURLOPT_HTTPHEADER, array($this->headers));
}
else {
curl_setopt($this->curl, CURLOPT_HTTPHEADER, array());
}
if(!empty($this->referer)) {
curl_setopt($this->curl, CURLOPT_REFERER, $this->referer);
}
if($this->ssl) {
curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, TRUE);
}
else {
curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, FALSE);
}
if($this->return_transfer) {
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, TRUE);
}
else {
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, FALSE);
}
if(is_int($this->timeout)) {
curl_setopt($this->curl, CURLOPT_TIMEOUT, $this->timeout);
curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, $this->timeout);
}
foreach($this->options as $key => $value) {
curl_setopt($this->curl, $key, $value);
}
curl_setopt($this->curl, CURLOPT_HEADER, TRUE);
curl_setopt($this->curl, CURLOPT_VERBOSE, TRUE);
}
}
protected function _getBaseURL(){
$s = &$_SERVER;
$ssl = (!empty($s['HTTPS']) && $s['HTTPS'] == 'on') ? true:false;
$sp = strtolower($s['SERVER_PROTOCOL']);
$protocol = substr($sp, 0, strpos($sp, '/')) . (($ssl) ? 's' : '');
$port = $s['SERVER_PORT'];
$port = ((!$ssl && $port=='80') || ($ssl && $port=='443')) ? '' : ':'.$port;
$host = isset($s['HTTP_X_FORWARDED_HOST']) ? $s['HTTP_X_FORWARDED_HOST'] : (isset($s['HTTP_HOST']) ? $s['HTTP_HOST'] : null);
$host = isset($host) ? $host : $s['SERVER_NAME'] . $port;
$uri = $protocol . '://' . $host . $s['REQUEST_URI'];
$segments = explode('?', $uri, 2);
$url = $segments[0];
return $url;
}
protected function http_build_multi_query($data, $key = null)
{
$query = array();
if (empty($data)) {
return $key . '=';
}
$is_array_assoc = (bool)count(array_filter(array_keys($data), 'is_string'));
foreach ($data as $k => $value) {
if (is_string($value) || is_numeric($value)) {
$brackets = $is_array_assoc ? '[' . $k . ']' : '[]';
$query[] = urlencode($key === null ? $k : $key . $brackets) . '=' . rawurlencode($value);
} elseif (is_array($value)) {
$nested = $key === null ? $k : $key . '[' . $k . ']';
$query[] = $this->http_build_multi_query($value, $nested);
}
}
return implode('&', $query);
}
}Örnek kullanım: $curl = new WO_cURL();
$curl->GET('http://api.webools.com/');
var_dump($curl->json());