Created
October 21, 2017 10:04
-
-
Save wahyudibo/f3f2da76f8cfe01238991da974cb956b to your computer and use it in GitHub Desktop.
Example of Curl and Multi Curl in PHP
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 | |
namespace App\Library\Curl; | |
class Curl { | |
private static $cookies, $proxy, $verbose, $mobile; | |
public static function setCookie($cookie_name){ | |
self::$cookies = (dirname(getcwd()) . '/storage/app/cookies/'. $cookie_name . '.txt'); | |
} | |
public static function getCookie(){ | |
return self::$cookies; | |
} | |
public static function setProxy($proxy){ | |
self::$proxy = $proxy; | |
} | |
public static function verbose(){ | |
self::$verbose = true; | |
} | |
public static function mobileView() | |
{ | |
self::$mobile = true; | |
} | |
public static function request($user_options, $verbose = false){ | |
//set the options | |
$default_options[CURLOPT_FOLLOWLOCATION] = 1; | |
$default_options[CURLOPT_RETURNTRANSFER] = 1; | |
$default_options[CURLOPT_SSL_VERIFYHOST] = 2; | |
$default_options[CURLOPT_SSL_VERIFYPEER] = 1; | |
$default_options[CURLOPT_CAINFO] = dirname(getcwd()) . '/storage/app/CA/cacert.pem'; | |
if (! self::$mobile) { | |
$default_options[CURLOPT_USERAGENT] = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36'; | |
} else { | |
$default_options[CURLOPT_USERAGENT] = 'Mozilla/5.0 (Linux; Android 4.4.4; Nexus 5 Build/KTU84P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.114 Mobile Safari/537.36'; | |
} | |
$default_options[CURLOPT_ENCODING] = 'gzip,deflate'; | |
$default_options[CURLOPT_HEADER] = 0; | |
$default_options[CURLOPT_COOKIEFILE] = self::$cookies; | |
$default_options[CURLOPT_COOKIEJAR] = self::$cookies; | |
if(self::$verbose){ | |
$default_options[CURLOPT_VERBOSE] = 1; | |
} | |
if(isset(self::$proxy)){ | |
$default_options[CURLOPT_SSL_VERIFYPEER] = 0; | |
$default_options[CURLOPT_PROXY] = self::$proxy; | |
} | |
$options = $user_options + $default_options; | |
//do curl | |
$ch = curl_init(); | |
curl_setopt_array($ch, $options); | |
$output = curl_exec($ch); | |
if($verbose){ | |
$info = curl_getinfo($ch); | |
$errno = curl_errno($ch); | |
$error = curl_error($ch); | |
$response = compact('info', 'errno', 'error', 'output'); | |
} else { | |
$response = $output; | |
} | |
curl_close($ch); | |
return $response; | |
} | |
public function multiRequest($data, $options = array()){ | |
// array of curl handles | |
$curly = array(); | |
// data to be returned | |
$result = array(); | |
// multi handle | |
$mh = curl_multi_init(); | |
// loop through $data and create curl handles | |
// then add them to the multi-handle | |
foreach ($data as $id => $d) { | |
$curly[$id] = curl_init(); | |
$url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d; | |
curl_setopt($curly[$id], CURLOPT_URL, $url); | |
curl_setopt($curly[$id], CURLOPT_HEADER, 0); | |
curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($curly[$id], CURLOPT_COOKIEFILE, self::$cookies); | |
curl_setopt($curly[$id], CURLOPT_COOKIEJAR, self::$cookies); | |
// post? | |
if (is_array($d)) { | |
if (!empty($d['post'])) { | |
curl_setopt($curly[$id], CURLOPT_POST, 1); | |
curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']); | |
} | |
} | |
// extra options? | |
if (!empty($options)) { | |
curl_setopt_array($curly[$id], $options[$id]); | |
} | |
curl_multi_add_handle($mh, $curly[$id]); | |
} | |
// execute the handles | |
$running = null; | |
do { | |
curl_multi_exec($mh, $running); | |
} while($running > 0); | |
// get content and remove handles | |
foreach($curly as $id => $c) { | |
$result[$id] = curl_multi_getcontent($c); | |
curl_multi_remove_handle($mh, $c); | |
} | |
// all done | |
curl_multi_close($mh); | |
return $result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment