Last active
November 25, 2016 07:25
-
-
Save anjan011/90bebb829bf98c072e457863bd760afc to your computer and use it in GitHub Desktop.
Class: CurlFetch - Simple class to fetch content from URL via GET or POST
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 | |
/** | |
* Class CurlFetch | |
* | |
* A simple class to fetch content from an URL via GET or POST | |
*/ | |
class CurlFetch { | |
/** | |
* Code and message for | |
*/ | |
const CODE_SUCCESS = 1; | |
const MSG_SUCCESS = 'Content fetched'; | |
/** | |
* Common error codes | |
*/ | |
const ERR_CODE_EMPTY_URL = -1; | |
const ERR_CODE_CURL_INIT_FAILED = -2; | |
const ERR_CODE_CURL_EXEC_FAILED = -3; | |
/** | |
* Descriptive error messages for related codes | |
*/ | |
const ERR_MSG_EMPTY_URL = 'URL to fetch is not provided or empty'; | |
const ERR_MSG_CURL_INIT_FAILED = 'CURL initialization failed'; | |
const ERR_MSG_CURL_EXEC_FAILED = 'CURL execution failed'; | |
/** | |
* CurlFetch constructor. | |
* | |
* @throws Exception | |
*/ | |
function __construct() { | |
/** | |
* Lets check if curl module is installed and enabled! | |
*/ | |
if ( !function_exists( 'curl_init' ) ) { | |
throw new Exception( 'CURL module needs to be installed' ); | |
} | |
} | |
/** | |
* Return an array with 3 elements. 'code','message' and 'data'. | |
* | |
* array['code'] int 'code' is to denote the status of operation, usualy | |
* an integer value where 0 means nothing changed, | |
* negative value means errors and positive value means | |
* success. | |
* array['message'] string Short text to provide a hint about the status of | |
* operation. | |
* array['data'] mixed contains any possible data that function wish to return | |
* | |
* @param int $code The code | |
* @param string $message Message string | |
* @param null $data Data to transmit | |
* | |
* @return array | |
*/ | |
private function __returnArray( $code = 0, $message = '', $data = NULL ) { | |
return array( | |
'code' => (int) $code, | |
'message' => $message, | |
'data' => $data, | |
); | |
} | |
/** | |
* A sample user agent string for a mobile device (Chrome on Android) | |
* | |
* @return string | |
*/ | |
public function getUserAgentStringForMobile() { | |
return "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36"; | |
} | |
/** | |
* A sample user agent string for desktop browser (Chrome on Ubuntu) | |
* | |
* @return string | |
*/ | |
public function getUserAgentStringForDesktop() { | |
return "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.100 Safari/537.36"; | |
} | |
/** | |
* Fetch url content via GET method | |
* | |
* @param string $url The url | |
* @param array $curlOptions Additional CURL options | |
* | |
* @return array | |
*/ | |
function viaGet( $url = '', $curlOptions = array() ) { | |
$url = trim( $url ); | |
// check for non empty url! | |
if ( !$url ) { | |
return $this->__returnArray( | |
self::ERR_CODE_EMPTY_URL, | |
self::ERR_MSG_EMPTY_URL | |
); | |
} | |
$ch = curl_init(); | |
if ( $ch ) { | |
// init success! Now, set options | |
$options = array(); | |
if ( is_array( $curlOptions ) && !empty($curlOptions) ) { | |
foreach ( $curlOptions as $k => $v ) { | |
$options[ $k ] = $v; | |
} | |
} | |
// these options enforced by the function | |
$options[ CURLOPT_URL ] = $url; | |
$options[ CURLOPT_RETURNTRANSFER ] = TRUE; | |
$options[ CURLOPT_POST ] = FALSE; | |
curl_setopt_array( $ch, $options ); | |
$res = curl_exec( $ch ); | |
if ( $res === FALSE ) { | |
# request failed! | |
$returnData = array( | |
'targetUrl' => $url, | |
'curlError' => array( | |
'code' => curl_errno( $ch ), | |
'message' => curl_error( $ch ), | |
), | |
); | |
curl_close( $ch ); | |
return $this->__returnArray( | |
self::ERR_CODE_CURL_EXEC_FAILED, | |
self::ERR_MSG_CURL_EXEC_FAILED, | |
$returnData | |
); | |
} | |
else { | |
# request successful! Response is in $res | |
curl_close( $ch ); | |
return $this->__returnArray( | |
self::CODE_SUCCESS, | |
self::MSG_SUCCESS, | |
array( | |
'targetUrl' => $url, | |
'curlResponse' => array( | |
'length' => strlen( $res ), | |
'content' => $res, | |
), | |
) | |
); | |
} | |
} | |
else { | |
// curl init failed | |
return $this->__returnArray( | |
self::ERR_CODE_CURL_INIT_FAILED, | |
self::ERR_MSG_CURL_INIT_FAILED | |
); | |
} | |
} | |
/** | |
* Fetch url content via POST method | |
* | |
* @param string $url The url | |
* @param array $postData Post data to send | |
* @param array $curlOptions Additional CURL options | |
* | |
* @return array | |
*/ | |
function viaPost( $url = '', $postData = array(), $curlOptions = array() ) { | |
$url = trim( $url ); | |
// check for non empty url! | |
if ( !$url ) { | |
return $this->__returnArray( | |
self::ERR_CODE_EMPTY_URL, | |
self::ERR_MSG_EMPTY_URL | |
); | |
} | |
$ch = curl_init(); | |
if ( $ch ) { | |
// init success! Now, set options | |
$options = array(); | |
if ( is_array( $curlOptions ) && !empty($curlOptions) ) { | |
foreach ( $curlOptions as $k => $v ) { | |
$options[ $k ] = $v; | |
} | |
} | |
// these options enforced by the function | |
$options[ CURLOPT_URL ] = $url; | |
$options[ CURLOPT_RETURNTRANSFER ] = TRUE; | |
$options[ CURLOPT_POST ] = TRUE; | |
$options[ CURLOPT_POSTFIELDS ] = $postData; | |
curl_setopt_array( $ch, $options ); | |
$res = curl_exec( $ch ); | |
if ( $res === FALSE ) { | |
# request failed! | |
$returnData = array( | |
'targetUrl' => $url, | |
'postData' => $postData, | |
'curlError' => array( | |
'code' => curl_errno( $ch ), | |
'message' => curl_error( $ch ), | |
), | |
); | |
curl_close( $ch ); | |
return $this->__returnArray( | |
self::ERR_CODE_CURL_EXEC_FAILED, | |
self::ERR_MSG_CURL_EXEC_FAILED, | |
$returnData | |
); | |
} | |
else { | |
# request successful! Response is in $res | |
curl_close( $ch ); | |
return $this->__returnArray( | |
self::CODE_SUCCESS, | |
self::MSG_SUCCESS, | |
array( | |
'targetUrl' => $url, | |
'postData' => $postData, | |
'curlResponse' => array( | |
'length' => strlen( $res ), | |
'content' => $res, | |
), | |
) | |
); | |
} | |
} | |
else { | |
// curl init failed | |
return $this->__returnArray( | |
self::ERR_CODE_CURL_INIT_FAILED, | |
self::ERR_MSG_CURL_INIT_FAILED | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment