Last active
July 12, 2016 00:59
-
-
Save max-dark/4c08575bcff9d4c6ea926cd8c9813c01 to your computer and use it in GitHub Desktop.
cURLWrapper is helper class that allows to send GET/POST requests, upload files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* \cURLWrapper is helper class that allows to send GET/POST requests, upload files | |
* | |
* @package cURLWrapper | |
* @author Maxim 'Cra3y' | |
* @license LGPL | |
* @link https://gist.github.com/max-dark/4c08575bcff9d4c6ea926cd8c9813c01 | |
* | |
* Date: 12.07.16 | |
*/ | |
/** | |
* Class \cURLWrapper | |
* | |
* cURLWrapper is helper class that allows to send GET/POST requests, upload files | |
* | |
* @depends PHP >= 5.4, cURL, Fileinfo | |
*/ | |
class cURLWrapper { | |
const CRLF = "\r\n"; | |
const CRLFx2 = self::CRLF . self::CRLF; | |
/** @var string $boundary */ | |
private $boundary; | |
/** @var \finfo $finfo file info tool */ | |
private $finfo; | |
/** @var array $last_info */ | |
private $last_info = [ ]; | |
/** @var resource $curl_handle */ | |
private $curl_handle = null; | |
/** | |
* cURLWrapper constructor. | |
*/ | |
public function __construct() { | |
$this->finfo = new finfo( FILEINFO_MIME_TYPE ); | |
} | |
/** | |
* Send 'multipart/form-data' request | |
* | |
* @param string $url | |
* @param string[] $request array of inputs values | |
* @param array $files [Optional] list of files to upload | |
* | |
* @return string|bool | |
*/ | |
function send_multipart( $url, $request, $files = [ ] ) { | |
$this->boundary = '------------------------' . uniqid(); | |
$files = array_map( 'realpath', array_filter( $files ) ); | |
$data = $this->format_multipart( $request, $files ); | |
$content_type = 'multipart/form-data; boundary=' . $this->boundary; | |
return $this->send_post( $url, $data, $content_type ); | |
} | |
/** | |
* Encode request to 'multipart/form-data' | |
* | |
* @param string[] $request array of inputs values | |
* @param string[] $files [Optional] list of files to upload | |
* | |
* @return string multipart/form-data | |
*/ | |
function format_multipart( $request, $files = [ ] ) { | |
$parts = [ ]; | |
$delimiter = '--' . $this->boundary . self::CRLF; | |
foreach ( $request as $name => $value ) { | |
$parts[] = $delimiter . $this->format_form_part( $name, $value ); | |
} | |
foreach ( $files as $name => $value ) { | |
$parts[] = $delimiter . $this->format_file_part( $name, $value ); | |
} | |
$parts[] = '--' . $this->boundary . '--' . self::CRLF; | |
return implode( '', $parts ); | |
} | |
/** | |
* Encode field to 'multipart/form-data' | |
* | |
* @param string $name name of field | |
* @param string $value value of field | |
* | |
* @return string | |
*/ | |
function format_form_part( $name, $value ) { | |
$body = 'Content-Disposition: form-data; name="' . $name . '"'; | |
$body .= self::CRLFx2 . $value . self::CRLF; | |
return $body; | |
} | |
/** | |
* Encode file to 'multipart/form-data' | |
* | |
* @param string $name name of field | |
* @param string $value filename | |
* | |
* @return string | |
*/ | |
function format_file_part( $name, $value ) { | |
$file = basename( $value ); | |
$mime = $this->finfo->file( $value ); | |
$body = 'Content-Disposition: form-data; name="' . $name . '"; '; | |
$body .= 'filename="' . $file . '"' . self::CRLF; | |
$body .= 'Content-Type: ' . $mime . self::CRLFx2; | |
$body .= file_get_contents( $value ) . self::CRLF; | |
return $body; | |
} | |
/** | |
* Send post request | |
* | |
* @param string $url | |
* @param string $data | |
* @param string $content_type | |
* @param array $options [Optional] | |
* | |
* @return bool|string | |
*/ | |
public function send_post( $url, $data, $content_type, $options = [ ] ) { | |
$headers = [ | |
'Content-Length: ' . strlen( $data ), | |
'Content-Type: ' . $content_type, | |
]; | |
$post_options = [ | |
CURLOPT_HTTPHEADER => $headers, | |
CURLOPT_POST => true, | |
CURLOPT_POSTFIELDS => $data, | |
]; | |
return $this->send_request( $url, $options + $post_options ); | |
} | |
/** | |
* Send request to $url | |
* | |
* @param string $url | |
* @param array $options [Optional] | |
* | |
* @return string|bool Result of request, false if fail | |
*/ | |
function send_request( $url, $options = [ ] ) { | |
$defaults = [ | |
CURLOPT_URL => $url, | |
CURLOPT_HEADER => false, | |
CURLOPT_RETURNTRANSFER => true, | |
]; | |
$options = $options + $defaults; | |
$this->curl_handle = curl_init(); | |
curl_setopt_array( $this->curl_handle, $options ); | |
$res = curl_exec( $this->curl_handle ); | |
$this->last_info = curl_getinfo( $this->curl_handle ); | |
curl_close( $this->curl_handle ); | |
return $res; | |
} | |
/** | |
* Send 'application/x-www-form-urlencoded' post request | |
* | |
* @param string $url | |
* @param string[] $request array of inputs values | |
* @param array $options [Optional] | |
* | |
* @return string|bool | |
*/ | |
function send_post_urlencoded( $url, $request, $options = [ ] ) { | |
$data = http_build_query( $request ); | |
$content_type = 'application/x-www-form-urlencoded'; | |
return $this->send_post( $url, $data, $content_type, $options ); | |
} | |
/** | |
* Info about last request | |
* | |
* @return array | |
*/ | |
public function getLastInfo() { | |
return $this->last_info; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment