public function get($url_mixed, $data = array()) { if (is_array($url_mixed)) { $curl_multi = curl_multi_init(); $this->_multi_parent = true; $this->curls = array(); foreach ($url_mixed as $url) { $curl = new Curl(); $curl->_multi_child = true; $curl->setOpt(CURLOPT_URL, $this->_buildURL($url, $data), $curl->curl); $curl->setOpt(CURLOPT_HTTPGET, true); $this->_call($this->_before_send, $curl); $this->curls[] = $curl; $curlm_error_code = curl_multi_add_handle($curl_multi, $curl->curl); if (!($curlm_error_code === CURLM_OK)) { //throw new \ErrorException('cURL multi add handle error: ' .curl_multi_strerror($curlm_error_code)); } } foreach ($this->curls as $ch) { foreach ($this->_options as $key => $value) { $ch->setOpt($key, $value); } } do { $status = curl_multi_exec($curl_multi, $active); } while ($status === CURLM_CALL_MULTI_PERFORM || $active); foreach ($this->curls as $ch) { $this->exec($ch); } } else { $this->setopt(CURLOPT_URL, $this->_buildURL($url_mixed, $data)); $this->setopt(CURLOPT_HTTPGET, true); return $this->exec(); } }
public function get($url_mixed, $data = array()) { if (is_array($url_mixed)) { $curl_multi = curl_multi_init(); $this->multi_parent = true; $this->curls = array(); foreach ($url_mixed as $url) { $curl = new Curl(); $curl->multi_child = true; $curl->beforeSend($this->before_send_function); $curl->success($this->success_function); $curl->error($this->error_function); $curl->complete($this->complete_function); $curl->base_url = $url; $curl->url = $this->buildURL($url, $data); $curl->setOpt(CURLOPT_URL, $curl->url, $curl->curl); $curl->setOpt(CURLOPT_CUSTOMREQUEST, 'GET'); $curl->setOpt(CURLOPT_HTTPGET, true); $this->curls[] = $curl; $curlm_error_code = curl_multi_add_handle($curl_multi, $curl->curl); if (!($curlm_error_code === CURLM_OK)) { throw new \ErrorException('cURL multi add handle error: ' . curl_multi_strerror($curlm_error_code)); } } foreach ($this->curls as $ch) { foreach ($this->options as $key => $value) { $ch->setOpt($key, $value); } } do { curl_multi_select($curl_multi); $status = curl_multi_exec($curl_multi, $active); } while ($status === CURLM_CALL_MULTI_PERFORM || $active); while (!($info_array = curl_multi_info_read($curl_multi)) === false) { if (!($info_array['msg'] === CURLMSG_DONE)) { continue; } foreach ($this->curls as $ch) { if ($ch->curl === $info_array['handle']) { $ch->curl_error_code = $info_array['result']; break; } } } foreach ($this->curls as $ch) { $this->exec($ch); } } else { $this->base_url = $url_mixed; $this->url = $this->buildURL($url_mixed, $data); $this->setOpt(CURLOPT_URL, $this->url); $this->setOpt(CURLOPT_CUSTOMREQUEST, 'GET'); $this->setOpt(CURLOPT_HTTPGET, true); return $this->exec(); } }
public function process_user($username, $password, $facility_code) { $curl = new Curl(); $response = array(); //Get Supplier $supplier = $this->get_supplier($facility_code); if ($supplier == "kemsa") { //Use nascop url $url = $this->default_url; $post = array("email" => $username, "password" => $password); $url = $this->default_url . 'sync/user'; $curl->post($url, $post); } else { //Use escm url $curl->setBasicAuthentication($username, $password); $curl->setOpt(CURLOPT_RETURNTRANSFER, TRUE); $url = $this->default_url . 'user/' . $username; $curl->get($url); } //Handle Response if ($curl->error) { $response['error'] = TRUE; $response['content'] = array($curl->error_code); } else { $response['error'] = FALSE; $response['content'] = json_decode($curl->response, TRUE); } return json_encode($response); }
public function testError() { $this->curl->setOpt(CURLOPT_CONNECTTIMEOUT_MS, 2000); $this->curl->get('http://1.2.3.4/'); $this->assertTrue($this->curl->error === TRUE); $this->assertTrue($this->curl->curl_error === TRUE); $this->assertTrue($this->curl->curl_error_code === CURLE_OPERATION_TIMEOUTED); }
public static function create($apiKey, $endpoint = self::DEFAULT_ENDPOINT, $version = '1.0') { $curl = new Curl(); if (strpos($endpoint, 'https') === 0) { $curl->setOpt(CURLOPT_CAINFO, __DIR__ . '/resources/cacert.pem'); } $curl->setHeader('X-Auth-Key', $apiKey); $curl->setHeader('Content-Type', 'application/json'); $connector = new Connector($curl, $apiKey, $version, $endpoint); $parser = new Parser(); $errorHandler = new ErrorHandler(); return new IUcto($connector, $parser, $errorHandler); }
static function mapi_get($url_mixed, $data = array(), $debug = false) { //$debug=true; Yii::import('application.vendors.*'); require_once 'Tp/Curl.class.php'; $curl = new Curl(); if (YII_ENV == 'development') { $curl->setHeader('Apihandshake', 'yaochufa'); $curl->setHeader('Referer', 'http://m.yaochufa.com'); } else { $curl->setHeader('Apihandshake', 'yaochufaapi'); $curl->setHeader('Referer', 'http://m.yaochufa.com'); } $curl->setOpt(CURLOPT_TIMEOUT, 15); $data['system'] = Yii::app()->params['mmm_api']['system']; $curl->get($url_mixed, $data); if ($debug == true) { XUtils::dump($curl->request_headers); } return $curl->response; }
/** * Add Put * * @access public * @param $url * @param $data * * @return object */ public function addPut($url, $data = array()) { if (is_array($url)) { $data = $url; $url = $this->baseUrl; } $curl = new Curl(); $curl->setURL($url); $curl->setOpt(CURLOPT_CUSTOMREQUEST, 'PUT'); $put_data = $curl->buildPostData($data); $curl->setHeader('Content-Length', strlen($put_data)); $curl->setOpt(CURLOPT_POSTFIELDS, $put_data); $this->addHandle($curl); return $curl; }
<?php // Perform a post-redirect-get request (POST data and follow 303 redirections // using GET requests). $curl = new Curl(); $curl->setOpt(CURLOPT_FOLLOWLOCATION, true); $curl->post('https://www.example.com/login/', array('username' => 'myusername', 'password' => 'mypassword')); // POST data and follow 303 redirections by POSTing data again. Please note // that 303 redirections should not be handled this way. // https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4 $curl = new Curl(); $curl->setOpt(CURLOPT_FOLLOWLOCATION, true); $curl->post('https://www.example.com/login/', array('username' => 'myusername', 'password' => 'mypassword'), false); // A POST request performs a post-redirect-get by default. Other request // methods force an option which conflicts with the post-redirect-get behavior. // Due to technical limitations of PHP engines <5.5.11 and HHVM, it is not // possible to reset this option. It is therefore impossible to perform a // post-redirect-get request using a php-curl-class Curl object that has already // been used to perform other types of requests. Either use a new php-curl-class // Curl object or upgrade your PHP engine.
function curlNet2($method, $url, $param = array(), $ssl = FALSE) { require dirname(__FILE__) . '/ext/curl.php'; $curl = new Curl(); $exMethod = strtolower($method); if ($ssl == TRUE) { $curl->setOpt(CURLOPT_SSL_VERIFYPEER, false); } $curl->{$exMethod}($url, $param); if ($curl->error) { echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage; $curl->close(); } else { return $curl->response; $curl->close(); unset($curl); } }
/** * @expectedException PHPUnit_Framework_Error_Warning */ public function testRequiredOptionCurlOptReturnTransferEmitsWarning() { $curl = new Curl(); $curl->setOpt(CURLOPT_RETURNTRANSFER, false); }
/** * Add Search * * @access public * @param $url * @param $data * * @return object */ public function addSearch($url, $data = array()) { if (is_array($url)) { $data = $url; $url = $this->baseUrl; } $curl = new Curl(); $curl->setUrl($url); $curl->setOpt(CURLOPT_CUSTOMREQUEST, 'SEARCH'); $put_data = $curl->buildPostData($data); if (is_string($put_data)) { $curl->setHeader('Content-Length', strlen($put_data)); } $curl->setOpt(CURLOPT_POSTFIELDS, $put_data); $this->queueHandle($curl); return $curl; }
private function _connect($url) { $curl = new Curl(); $curl -> setOpt(CURLOPT_RETURNTRANSFER, TRUE); $curl -> setOpt(CURLOPT_SSL_VERIFYPEER, FALSE); $curl -> get($url); if ($curl -> error) { $curl -> error_code; $response = "Error: " . $curl -> error_code; } else { $response = $curl -> response; } return $response; }
public function testErrorCallback() { $success_called = FALSE; $error_called = FALSE; $complete_called = FALSE; $curl = new Curl(); $curl->setHeader('X-DEBUG-TEST', 'get'); $curl->setOpt(CURLOPT_SSL_VERIFYPEER, FALSE); $curl->setOpt(CURLOPT_SSL_VERIFYHOST, FALSE); $curl->setOpt(CURLOPT_CONNECTTIMEOUT_MS, 2000); $curl->success(function ($instance) use(&$success_called, &$error_called, &$complete_called) { PHPUnit_Framework_Assert::assertInstanceOf('Curl', $instance); PHPUnit_Framework_Assert::assertFalse($success_called); PHPUnit_Framework_Assert::assertFalse($error_called); PHPUnit_Framework_Assert::assertFalse($complete_called); $success_called = TRUE; }); $curl->error(function ($instance) use(&$success_called, &$error_called, &$complete_called, &$curl) { PHPUnit_Framework_Assert::assertInstanceOf('Curl', $instance); PHPUnit_Framework_Assert::assertFalse($success_called); PHPUnit_Framework_Assert::assertFalse($error_called); PHPUnit_Framework_Assert::assertFalse($complete_called); $error_called = TRUE; }); $curl->complete(function ($instance) use(&$success_called, &$error_called, &$complete_called) { PHPUnit_Framework_Assert::assertInstanceOf('Curl', $instance); PHPUnit_Framework_Assert::assertFalse($success_called); PHPUnit_Framework_Assert::assertTrue($error_called); PHPUnit_Framework_Assert::assertFalse($complete_called); $complete_called = TRUE; }); $curl->get(Test::ERROR_URL); $this->assertFalse($success_called); $this->assertTrue($error_called); $this->assertTrue($complete_called); }
<?php define('MAX_SIZE', 3 * 1024 * 1024); require "vendor/autoload.php"; use Respect\Validation\Validator as v; $url = $_GET['url']; $url_parts = parse_url($url); //error_log(print_r($url_parts,1)); // check valid url. if (!v::arr()->key('scheme', v::startsWith('http'))->key('host', v::domain())->key('path', v::string())->validate($url_parts)) { die_and_log($status = 400, 'invalid url', $url); } // head access $c = new Curl(); $c->setHeader('X-Forwarded-For', getRemoteIP()); $c->setOpt(CURLOPT_NOBODY, true); $c->setOpt(CURLOPT_RETURNTRANSFER, true); $c->error(function () { die_and_log($status = 500, "head request error"); }); $c->get($url); error_log($url); //error_log(print_r($c->response_headers,1)); // check head res $length = null; $content_type = null; foreach ($c->response_headers as $header) { if (preg_match("/\\AContent-Length/i", $header)) { $length = (int) explode(":", $header, 2)[1]; } else { if (preg_match("/\\AContent-Type/i", $header)) {
public function get_updates($type = 0) { if ($type != 0) { if ($this->session->userdata("update_timer") != "") { $to_time = strtotime(date('Y-m-d H:i:s')); $from_time = strtotime($this->session->userdata("update_timer")); if (round(abs($to_time - $from_time) / 60, 2) <= 10) { $this->session->set_userdata("update_test", false); echo 2; die; } } $this->session->set_userdata("update_timer", date('Y-m-d H:i:s')); } ini_set("max_execution_time", "1000000"); $current_month_start = date('Y-m-01'); $one_current_month_start = date('Y-m-d', strtotime($current_month_start . "-1 month")); $two_current_month_start = date('Y-m-d', strtotime($current_month_start . "-2 months")); $three_current_month_start = date('Y-m-d', strtotime($current_month_start . "-3 months")); $facility_code = $this->session->userdata('facility'); $api_userID = $this->session->userdata('api_id'); $facility_list = User_Facilities::getHydratedFacilityList($api_userID); $lists = json_decode($facility_list['facility'], TRUE); $facility = Facilities::getSupplier($facility_code); $supplier = $facility->supplier->name; $links = array(); $curl = new Curl(); if (strtoupper($supplier) == "KENYA PHARMA") { $url = $this->esm_url; foreach ($lists as $facility_id) { if ($type == 0) { $links[] = "facility/" . $facility_id . "/cdrr"; $links[] = "facility/" . $facility_id . "/maps"; } else { $links[] = "facility/" . $facility_id . "/cdrr/" . $current_month_start; $links[] = "facility/" . $facility_id . "/maps/" . $current_month_start; $links[] = "facility/" . $facility_id . "/cdrr/" . $one_current_month_start; $links[] = "facility/" . $facility_id . "/maps/" . $one_current_month_start; $links[] = "facility/" . $facility_id . "/cdrr/" . $two_current_month_start; $links[] = "facility/" . $facility_id . "/maps/" . $two_current_month_start; } } $username = $this->session->userdata('api_user'); $password = $this->session->userdata('api_pass'); $curl->setBasicAuthentication($username, $password); $curl->setOpt(CURLOPT_RETURNTRANSFER, TRUE); } else { $url = $this->nascop_url; if (!empty($lists)) { $lists = explode(",", $lists[0]); foreach ($lists as $facility_id) { if ($type == 0) { $links[] = "sync/facility/" . $facility_id . "/cdrr"; $links[] = "sync/facility/" . $facility_id . "/maps"; } else { $links[] = "sync/facility/" . $facility_id . "/cdrr/" . $current_month_start; $links[] = "sync/facility/" . $facility_id . "/maps/" . $current_month_start; $links[] = "sync/facility/" . $facility_id . "/cdrr/" . $one_current_month_start; $links[] = "sync/facility/" . $facility_id . "/maps/" . $one_current_month_start; $links[] = "sync/facility/" . $facility_id . "/cdrr/" . $two_current_month_start; $links[] = "sync/facility/" . $facility_id . "/maps/" . $two_current_month_start; } } } } //clear orders if its a full sync if ($type == 0) { $this->clear_orders(); } foreach ($links as $link) { $target_url = $url . $link; $curl->get($target_url); if ($curl->error) { $curl->error_code; echo "Error: " . $curl->error_code . "<br/>"; } else { $main_array = json_decode($curl->response, TRUE); $clean_data = array(); foreach ($main_array as $main) { if ($main['code'] == "D-CDRR" || $main['code'] == "F-CDRR_units" || $main['code'] == "F-CDRR_packs") { $type = "cdrr"; } else { $type = "maps"; } if ($type == 0) { if (is_array($main)) { if (!empty($main)) { $id = $this->extract_order($type, array($main), $main['id']); } } } else { if ($main['period_begin'] == $current_month_start || $main['period_begin'] == $one_current_month_start || $main['period_begin'] == $two_current_month_start) { if (is_array($main)) { if (!empty($main)) { $id = $this->extract_order($type, array($main), $main['id']); } } } } } } } $this->session->set_flashdata('order_message', "Sync Complete"); echo 1; }