private function curlConnection($method = 'GET', $url, array $data = null, $timeout, $charset) { if (strtoupper($method) === 'POST') { $postFields = $data ? http_build_query($data, '', '&') : ""; $contentLength = "Content-length: " . strlen($postFields); $methodOptions = array(CURLOPT_POST => true, CURLOPT_POSTFIELDS => $postFields); } else { $contentLength = null; $methodOptions = array(CURLOPT_HTTPGET => true); } $options = array(CURLOPT_HTTPHEADER => array("Content-Type: application/x-www-form-urlencoded; charset=" . $charset, $contentLength), CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => false, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_CONNECTTIMEOUT => $timeout); $options = $options + $methodOptions; $curl = curl_init(); curl_setopt_array($curl, $options); $resp = curl_exec($curl); $info = curl_getinfo($curl); $error = curl_errno($curl); $errorMessage = curl_error($curl); curl_close($curl); $this->setStatus((int) $info['http_code']); $this->setResponse((string) $resp); if ($error) { throw new Exception("CURL can't connect: {$errorMessage}"); return false; } else { return true; } }
/** * 执行一个 HTTP 请求 * * @param string $url 执行请求的URL * @param mixed $params 表单参数 * 可以是array, 也可以是经过url编码之后的string * @param mixed $cookie cookie参数 * 可以是array, 也可以是经过拼接的string * @param string $method 请求方法 post / get * @param string $protocol http协议类型 http / https * @return array 结果数组 */ public static function makeRequest($url, $params, $cookie, $method = 'post', $protocol = 'http') { $query_string = self::makeQueryString1($params); $cookie_string = self::makeCookieString($cookie); $ch = curl_init(); if ('get' == $method) { curl_setopt($ch, CURLOPT_URL, "{$url}?{$query_string}"); } else { curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string); } curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); // disable 100-continue curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); if (!empty($cookie_string)) { curl_setopt($ch, CURLOPT_COOKIE, $cookie_string); } if ('https' == $protocol) { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); } $ret = curl_exec($ch); $err = curl_error($ch); if (false === $ret || !empty($err)) { $errno = curl_errno($ch); $info = curl_getinfo($ch); curl_close($ch); return array('result' => false, 'errno' => $errno, 'msg' => $err, 'info' => $info); } curl_close($ch); return array('result' => true, 'msg' => $ret); }
public function getDom($url, $post = false) { $f = fopen(CURL_LOG_FILE, 'a+'); // curl session log file if ($this->lastUrl) { $header[] = "Referer: {$this->lastUrl}"; } $curlOptions = array(CURLOPT_ENCODING => 'gzip,deflate', CURLOPT_AUTOREFERER => 1, CURLOPT_CONNECTTIMEOUT => 120, CURLOPT_TIMEOUT => 120, CURLOPT_URL => $url, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_FOLLOWLOCATION => true, CURLOPT_MAXREDIRS => 9, CURLOPT_RETURNTRANSFER => 1, CURLOPT_HEADER => 0, CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36", CURLOPT_COOKIEFILE => COOKIE_FILE, CURLOPT_COOKIEJAR => COOKIE_FILE, CURLOPT_STDERR => $f, CURLOPT_VERBOSE => true); if ($post) { // add post options $curlOptions[CURLOPT_POSTFIELDS] = $post; $curlOptions[CURLOPT_POST] = true; } $curl = curl_init(); curl_setopt_array($curl, $curlOptions); $data = curl_exec($curl); $this->lastUrl = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL); // get url we've been redirected to curl_close($curl); if ($this->dom) { $this->dom->clear(); $this->dom = false; } $dom = $this->dom = str_get_html($data); fwrite($f, "{$post}\n\n"); fwrite($f, "-----------------------------------------------------------\n\n"); fclose($f); return $dom; }
function send() { //check the fields to make sure that they are not NULL $this->isComplete(); $url = $this->host . $this->postPath; $postBody = json_encode($this->data); $sign = md5("POST" . $url . $postBody . $this->appMasterSecret); $url = $url . "?sign=" . $sign; $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); curl_setopt($ch, CURLOPT_TIMEOUT, 60); curl_setopt($ch, CURLOPT_POSTFIELDS, $postBody); $result = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $curlErrNo = curl_errno($ch); $curlErr = curl_error($ch); curl_close($ch); //print($result . "\r\n"); if ($httpCode == "0") { // Time out throw new Exception("Curl error number:" . $curlErrNo . " , Curl error details:" . $curlErr . "\r\n"); } else { if ($httpCode != "200") { // We did send the notifition out and got a non-200 response throw new Exception("Http code:" . $httpCode . " details:" . $result . "\r\n"); } else { return $result; } } }
/** * Make an api request * * @param string $resource * @param array $params * @param string $method */ public function call($resource, $params = array()) { $queryString = 'access_token=' . $this->getAccessToken(); if (!empty($params) && is_array($params)) { $queryString .= http_build_query($params); } $requestUrl = self::API_URL . $resource . '/?' . $queryString; $curl = curl_init(); $curl_options = array(CURLOPT_RETURNTRANSFER => 1, CURLOPT_URL => $requestUrl, CURLOPT_TIMEOUT => 30, CURLOPT_HTTPHEADER => array('Accept: application/json', 'appid: nike')); curl_setopt_array($curl, $curl_options); $response = curl_exec($curl); $curl_info = curl_getinfo($curl); //@todo test for curl error if ($response === FALSE) { throw new Exception(curl_error($curl), curl_errno($curl)); } curl_close($curl); //@todo test for any non 200 response if ($curl_info['http_code'] != 200) { throw new Exception("Response: Bad response - HTTP Code:" . $curl_info['http_code']); } $jsonArray = json_decode($response); if (!is_object($jsonArray)) { throw new Exception("Response: Response was not a valid response"); } return $jsonArray; }
function updateIndex($lang, $file) { $fileData = readFileData($file); $filename = $file->getPathName(); list($filename) = explode('.', $filename); $path = $filename . '.html'; $id = str_replace($lang . '/', '', $filename); $id = str_replace('/', '-', $id); $id = trim($id, '-'); $url = implode('/', array(ES_URL, ES_INDEX, $lang, $id)); $data = array('contents' => $fileData['contents'], 'title' => $fileData['title'], 'url' => $path); $data = json_encode($data); $size = strlen($data); $fh = fopen('php://memory', 'rw'); fwrite($fh, $data); rewind($fh); echo "Sending request:\n\tfile: {$file}\n\turl: {$url}\n"; $ch = curl_init($url); curl_setopt($ch, CURLOPT_PUT, true); curl_setopt($ch, CURLOPT_INFILE, $fh); curl_setopt($ch, CURLOPT_INFILESIZE, $size); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); $metadata = curl_getinfo($ch); if ($metadata['http_code'] > 400) { echo "[ERROR] Failed to complete request.\n"; var_dump($response); exit(2); } curl_close($ch); fclose($fh); echo "Sent {$file}\n"; }
function make_call($call_options) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $call_options['route']); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_USERPWD, $call_options['credentials']); curl_setopt($ch, CURLOPT_USERAGENT, $call_options['userAgent']); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: ' . $call_options['contentType'])); switch ($call_options['method']) { case CS_REST_PUT: curl_setopt($ch, CURLOPT_CUSTOMREQUEST, CS_REST_PUT); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length: ' . strlen($call_options['data']))); curl_setopt($ch, CURLOPT_POSTFIELDS, $call_options['data']); break; case CS_REST_POST: curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $call_options['data']); break; case CS_REST_DELETE: curl_setopt($ch, CURLOPT_CUSTOMREQUEST, CS_REST_DELETE); break; } $response = curl_exec($ch); if (!$response && $response !== '') { trigger_error('Error making request with curl_error: ' . curl_error($ch)); } $this->_log->log_message('API Call Info for ' . $call_options['method'] . ' ' . curl_getinfo($ch, CURLINFO_EFFECTIVE_URL) . ': ' . curl_getinfo($ch, CURLINFO_SIZE_UPLOAD) . ' bytes uploaded. ' . curl_getinfo($ch, CURLINFO_SIZE_DOWNLOAD) . ' bytes downloaded' . ' Total time (seconds): ' . curl_getinfo($ch, CURLINFO_TOTAL_TIME), get_class($this), CS_REST_LOG_VERBOSE); $result = array('code' => curl_getinfo($ch, CURLINFO_HTTP_CODE), 'response' => $response); curl_close($ch); return $result; }
function toastup($channel, $message) { $channel_uri = urldecode($channel); $toast_xml = '<?xml version="1.0" encoding="utf-8" ?> <wp:Notification xmlns:wp="WPNotification"> <wp:Toast> <wp:Text1>' . 信息: . '</wp:Text1> <wp:Text2>' . $message . '</wp:Text2> </wp:Toast> </wp:Notification>'; $headers = array('Content-Type: text/xml', "Content-Length: " . strlen($toast_xml), "X-WindowsPhone-Target: toast", "X-NotificationClass: 2"); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $channel_uri); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 2); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POSTFIELDS, "{$toast_xml}"); curl_setopt($ch, CURLOPT_VERBOSE, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); $response = curl_getinfo($ch); curl_close($ch); return $response['http_code']; }
public function query($domain, $postvars) { $this->log_proxy(' domain: ' . $domain); $this->log_proxy('POSTVARS: ' . $postvars); $ch = curl_init($domain); curl_setopt($ch, CURLOPT_POST, 0); curl_setopt($ch, CURLOPT_VERBOSE, 0); // curl_setopt( $ch, CURLOPT_USERAGENT , isset( $_SERVER[ 'User-Agent' ]) ? $_SERVER[ 'User-Agent' ] : '' ); curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars); // curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 ); curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_REFERER, $domain); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); curl_setopt($ch, CURLOPT_AUTOREFERER, 0); curl_setopt($ch, CURLOPT_COOKIEJAR, 'ses_' . session_id()); curl_setopt($ch, CURLOPT_COOKIEFILE, 'ses_' . session_id()); // curl_setopt( $ch, CURLOPT_COOKIE , $COOKIE ); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FAILONERROR, 1); $content = curl_exec($ch); $response = curl_getinfo($ch); curl_close($ch); unlink('ses_' . session_id()); return $content; }
private function getResponse($arPost) { if (!function_exists('curl_init')) { die('curl library not installed'); return false; } if ($this->isAuth()) { $arPost = array_merge($arPost, $this->auth); } $data = array(); foreach ($arPost as $k => $v) { $data[] = urlencode($k) . '=' . urlencode($v); } $data[] = 'language=' . $this->language; $data = implode('&', $data); $handler = curl_init(); curl_setopt($handler, CURLOPT_URL, $this->url); curl_setopt($handler, CURLOPT_HEADER, 0); curl_setopt($handler, CURLOPT_POST, true); curl_setopt($handler, CURLOPT_POSTFIELDS, $data); curl_setopt($handler, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($handler, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($handler, CURLOPT_USERAGENT, $this->agent); curl_setopt($handler, CURLOPT_RETURNTRANSFER, 1); $content = curl_exec($handler); //print_r($content); $arRequest = curl_getinfo($handler); //print_r($arRequest); curl_close($handler); if (strpos($content, '{') !== false) { $content = $this->objectToArray(json_decode($content)); } return $content; }
function http_request($url, $method = 'GET', $headers = null, $content = null, &$respheaders = null) { if (empty($method)) { $method = 'GET'; } /** * Make post query */ if (!empty($content) && is_array($content)) { $content = http_build_query($content); } $curl = curl_init(); /** * Set headers */ if (!empty($headers)) { if (!is_array($headers)) { $headers = explode("\n", $headers); } curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); } curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($curl, CURLOPT_POSTFIELDS, $content); //curl_setopt($curl, CURLINFO_HEADER_OUT, true); curl_setopt($curl, CURLOPT_VERBOSE, 1); curl_setopt($curl, CURLOPT_HEADER, 1); $data = curl_exec($curl); /** * get headers and body */
private function _postCurl($url, $params = array()) { //init cURL $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_HEADER, FALSE); curl_setopt($ch, CURLOPT_USERPWD, $this->user . ':' . $this->pass); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, TRUE); if (!empty($params)) { curl_setopt($ch, CURLOPT_POSTFIELDS, $params); } // run cURL $data = curl_exec($ch); $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); $this->data = $data; $this->status = $status; // result return 200, 302 for OK, 400, 500 for BAD if ($status == 200 || $status == 302) { return true; } else { return false; } }
/** * A proxy curl implementation to get the content of the url. * * @param string $url * * @return string * @throws CurlException */ public function get($url) { $ch = curl_init($url); if (!ini_get('open_basedir')) { curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); } if (self::$proxy) { curl_setopt($ch, CURLOPT_PROXY, self::$proxy); } curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent()); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 40); curl_setopt($ch, CURLOPT_REFERER, $this->getReferrer()); /*curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLINFO_HEADER_OUT, true);*/ sleep(mt_rand(10, 20)); $content = curl_exec($ch); $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); $this->connectedURL = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); if (404 === $code) { throw new CurlException('Content not found.'); } if ($content === false) { // there was a problem $error = curl_error($ch); throw new CurlException('Error retrieving "' . $url . '" (' . $error . ')'); } if (false !== strpos($content, 'Error 525')) { throw new CurlException('Error in a source site.'); } return $content; }
function exec_curl_request($handle) { $response = curl_exec($handle); if ($response === false) { $errno = curl_errno($handle); $error = curl_error($handle); error_log("Curl retornou um erro {$errno}: {$error}\n"); curl_close($handle); return false; } $http_code = intval(curl_getinfo($handle, CURLINFO_HTTP_CODE)); curl_close($handle); if ($http_code >= 500) { // do not wat to DDOS server if something goes wrong sleep(10); return false; } else { if ($http_code != 200) { $response = json_decode($response, true); error_log("Request has failed with error {$response['error_code']}: {$response['description']}\n"); if ($http_code == 401) { throw new Exception('Invalid access token provided'); } return false; } else { $response = json_decode($response, true); if (isset($response['description'])) { error_log("Request was successfull: {$response['description']}\n"); } $response = $response['result']; } } return $response; }
/** * @inheritdoc */ public function executeHttpRequest($url, $method = self::METHOD_GET, $data = []) { $ch = curl_init(); if ($method === self::METHOD_GET) { if ($data) { $query = $this->buildQuery($data); if (strpos($url, '?') !== false) { $url .= $query; } else { $url .= '?' . $query; } } } else { curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); $body = curl_exec($ch); $info = curl_getinfo($ch); $code = $info['http_code']; if ($code === 200) { return $body; } else { $error = curl_errno($ch); $msg = curl_error($ch); throw new HttpException($code, sprintf('%s %s', $error, $msg)); } }
function curlExec($url, $data = false, $method = 'GET') { //global $JWT; // global $_SITE; if (!function_Exists('curl_init')) { return false; } // $parseUrl = parse_url($url); // if(!isset($parseUrl['scheme'])) { // $url = $_SITE['CLIENT']['API'].$url; // } //$auth = $JWT->decode(SECURITY_TOKEN, SERVER_KEY); $auth = ""; $headers = array("Accept: application/json", "Content-type: application/json"); $headers[] = "Authorization: NLAuth " . implode(', ', $auth); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_VERBOSE, 1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); $output = curl_exec($curl); $info = curl_getinfo($curl); $status = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); return array('output' => $output, 'status' => $status, 'info' => $info); }
function postToIDS($url, $data) { $agent = $_SERVER["HTTP_USER_AGENT"]; $curlData = http_build_query($data); $curlData = str_replace(" ", '%20', $curlData); $curlData = str_replace("+", '%20', $curlData); $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); //curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); //curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($curl, CURLOPT_USERAGENT, $agent); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $curlData); $response = curl_exec($curl); echo '<!-- CURLINFO_EFFECTIVE_URL' . PHP_EOL; var_dump(curl_getinfo($curl, CURLINFO_EFFECTIVE_URL)); echo PHP_EOL . '/-->' . PHP_EOL; if ($response === false) { echo "curl error: " . curl_error($curl); } else { $http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE); } curl_close($curl); return $http_status; }
function lrss_is_site_available($url) { //check, if a valid url is provided if (!filter_var($url, FILTER_VALIDATE_URL)) { return FALSE; } if (!function_exists('curl_init') && function_exists('get_headers')) { $headers = get_headers($url, 1); if ($headers[0] == 'HTTP/1.1 200 OK') { return TRUE; } else { return FALSE; } } else { if (function_exists('curl_init') && !function_exists('get_headers')) { return FALSE; } } $handle = curl_init(urldecode($url)); curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 0.5); curl_setopt($handle, CURLOPT_TIMEOUT, 1); curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE); $response = curl_exec($handle); $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE); if ($httpCode >= 200 && $httpCode < 400) { return TRUE; } else { return FALSE; } curl_close($handle); }
/** * @param $url * @param $destination * @return bool|null */ public function fetch($url, $destination) { try { $ret = null; $url = $this->addhttp($url); $ch = curl_init($url . '/favicon.ico'); curl_setopt($ch, CURLOPT_TIMEOUT, 5); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); $contents = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($httpCode == 200) { $fp = fopen($destination, 'w+'); fwrite($fp, $contents); fclose($fp); $ret = true; if ($this->converter) { $ret = $this->converter->convert($destination); } } curl_close($ch); return $ret; } catch (\Exception $e) { // hmm ok, let the next Fetcher try its luck } return null; }
public function send($payload) { $headers = array('Content-Type: application/x-www-form-urlencoded'); $post_string = http_build_query($payload); $cacertPath = dirname(__FILE__) . '/cacert.pem'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->config->apiAddress); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); if (file_exists($cacertPath)) { $this->log('cacert.pem file found, verifying API endpoint'); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); curl_setopt($ch, CURLOPT_CAINFO, $cacertPath); } else { $this->log('cacert.pem file not found, the API endpoint will not be verified', true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); } curl_exec($ch); $curl_error = curl_error($ch); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($http_code !== 200) { throw new \Exception('cURL error code ' . $http_code . ': ' . $curl_error); } }
public static function httpRequest($req, $hash_config = NULL) { $config = Obj2xml::getConfig($hash_config); if ((int) $config['print_xml']) { echo $req; } $ch = curl_init(); curl_setopt($ch, CURLOPT_PROXY, $config['proxy']); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: text/xml')); curl_setopt($ch, CURLOPT_URL, $config['url']); curl_setopt($ch, CURLOPT_POSTFIELDS, $req); curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, true); curl_setopt($ch, CURLOPT_TIMEOUT, $config['timeout']); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSLVERSION, 3); $output = curl_exec($ch); $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); if (!$output) { throw new \Exception(curl_error($ch)); } else { curl_close($ch); if ((int) $config['print_xml']) { echo $output; } return $output; } }
/** * @param $method - API method, including version number * @param array $params - Query params * @param string $requestType - (get|post|put|delete) * @param string $format - (json|xml) * @param bool|true $isAuth * * @return mixed * @throws Exception * */ public function call($method, $params = array(), $requestType = 'get', $format = 'json', $isAuth = true) { if (!is_array($params)) { throw new Exception('Query params must be an array.'); } $type = strtoupper($requestType); if (!in_array($type, array('GET', 'POST', 'PUT', 'DELETE'))) { $type = 'GET'; } $params['format'] = $format; $options = array(CURLOPT_URL => $this->_url . $method, CURLOPT_CUSTOMREQUEST => $type, CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_HEADERFUNCTION => array($this, '_parseHeaders')); $ch = curl_init(); if ($type == 'GET') { $options[CURLOPT_URL] = $this->_url . $method . '?' . http_build_query($params); } else { $options[CURLOPT_POST] = true; $options[CURLOPT_POSTFIELDS] = http_build_query($params); } if ($isAuth) { $options[CURLOPT_HTTPHEADER] = $this->_getAuthHeader($method, $params); } curl_setopt_array($ch, $options); $response = curl_exec($ch); $error = curl_error($ch); $this->_httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($error) { throw new Exception($error); } return $response; }
public function request($type, $url, array $params = array(), array $properties = array()) { $params = array_filter($params); $ch = curl_init(); switch (strtolower($type)) { case 'post': curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $params); break; case 'get': $url = $this->buildQueryString($url, $params); break; case 'delete': $url = $this->buildQueryString($url, $params); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); break; case 'update': case 'put': curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); break; } curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_URL, $url); foreach ($properties as $propertyKey => $propertyValue) { curl_setopt($ch, $propertyKey, $propertyValue); } $response = curl_exec($ch); $info = curl_getinfo($ch); curl_close($ch); return new Response($info, $response); }
/** * Returns the output of a remote URL. Any [curl option](http://php.net/curl_setopt) * may be used. * * // Do a simple GET request * $data = Remote::get($url); * * // Do a POST request * $data = Remote::get($url, array( * CURLOPT_POST => true, * CURLOPT_POSTFIELDS => http_build_query($array), * )); * * @param string remote URL * @param array curl options * @return string * @throws Exception */ public static function remote($url, array $options = null) { // The transfer must always be returned $options[CURLOPT_RETURNTRANSFER] = true; // Open a new remote connection $remote = curl_init($url); // Set connection options if (!curl_setopt_array($remote, $options)) { throw new Exception('Failed to set CURL options, check CURL documentation: http://php.net/curl_setopt_array'); } // Get the response $response = curl_exec($remote); // Get the response information $code = curl_getinfo($remote, CURLINFO_HTTP_CODE); if ($code and ($code < 200 or $code > 299)) { $error = $response; } elseif ($response === false) { $error = curl_error($remote); } // Close the connection curl_close($remote); if (isset($error)) { throw new Exception(sprintf('Error fetching remote %s [ status %s ] %s', $url, $code, $error)); } return $response; }
/** * Get embed data from this media content provider. * @param $url * @return OEmbedData|null */ public function GetEmbedData($url) { $targetUrl = $this->contentProviderEndpoint . '?format=json&url=' . $url; // Request a oEmbed content $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $targetUrl); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $result = curl_exec($ch); // Verify response $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($status_code != 200) { return null; } // Parse json response $response = json_decode($result, true); if ($response == null) { return null; } $embedData = $this->ParseJsonResponse($response); return $embedData; }
/** * @return array * @throws Payone_Api_Exception_InvalidResponse */ protected function doRequest() { $response = array(); $urlArray = $this->generateUrlArray(); $urlHost = $urlArray['host']; $urlPath = isset($urlArray['path']) ? $urlArray['path'] : ''; $urlScheme = $urlArray['scheme']; $urlQuery = $urlArray['query']; $curl = curl_init($urlScheme . "://" . $urlHost . $urlPath); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $urlQuery); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($curl, CURLOPT_TIMEOUT, self::DEFAULT_TIMEOUT); $result = curl_exec($curl); $this->setRawResponse($result); if (curl_getinfo($curl, CURLINFO_HTTP_CODE) != 200) { throw new Payone_Api_Exception_InvalidResponse(); } elseif (curl_error($curl)) { $response[] = "errormessage=" . curl_errno($curl) . ": " . curl_error($curl); } else { $response = explode("\n", $result); } curl_close($curl); return $response; }
/** * Returns the output of a remote URL. Any [curl option](http://php.net/curl_setopt) * may be used. * * // Do a simple GET request * $data = Remote::get($url); * * // Do a POST request * $data = Remote::get($url, array( * CURLOPT_POST => TRUE, * CURLOPT_POSTFIELDS => http_build_query($array), * )); * * @param string remote URL * @param array curl options * @return string * @throws Kohana_Exception */ public static function remote($url, array $options = NULL) { // The transfer must always be returned $options[CURLOPT_RETURNTRANSFER] = TRUE; // Open a new remote connection $remote = curl_init($url); // Set connection options if (!curl_setopt_array($remote, $options)) { throw new Kohana_Exception('Failed to set CURL options, check CURL documentation: :url', array(':url' => 'http://php.net/curl_setopt_array')); } // Get the response $response = curl_exec($remote); // Get the response information $code = curl_getinfo($remote, CURLINFO_HTTP_CODE); if ($code and $code < 200 or $code > 299) { $error = $response; } elseif ($response === FALSE) { $error = curl_error($remote); } // Close the connection curl_close($remote); if (isset($error)) { throw new Kohana_OAuth_Exception('Error fetching remote :url [ status :code ] :error', array(':url' => $url, ':code' => $code, ':error' => $error)); } return $response; }
/** * 发送Http请求到推送服务 * @param String $server * @param Array $data */ private static function http($server, $data) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $server); curl_setopt($ch, CURLOPT_TIMEOUT, 3); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $response = curl_exec($ch); if (curl_errno($ch)) { throw new \Exception('curl_error ' . curl_error($ch)); } else { if (strtolower($response) == 'ok') { curl_close($ch); return true; } else { if (preg_match('/not\\s+sub/', $response)) { } else { $info = curl_getinfo($ch); throw new \Exception('response_error ' . $response . ' ' . json_encode($info)); } } } curl_close($ch); return false; }
public function exec() { $active = null; do { do { $status = curl_multi_exec($this->curlHandle, $active); } while ($status == CURLM_CALL_MULTI_PERFORM); if ($status != CURLM_OK) { break; } $response = array(); $respond = curl_multi_info_read($this->curlHandle); while ($respond) { $callback = $this->requestMap[(string) $respond['handle']]; $responses[$callback]['content'] = curl_multi_getcontent($respond['handle']); $responses[$callback]['httpcode'] = curl_getinfo($respond['handle'], CURLINFO_HTTP_CODE); curl_multi_remove_handle($this->curlHandle, $respond['handle']); curl_close($respond['handle']); $respond = curl_multi_info_read($this->curlHandle); } if ($active > 0) { curl_multi_select($this->curlHandle, 0.05); } } while ($active); return $responses; }
public static function send($url, $method = 'GET', $data = array()) { if ($method === 'GET') { $url .= count($data) > 0 ? '?' . http_build_query($data) : ''; } else { $data = json_encode($data); } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); switch ($method) { case "POST": curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); break; case "GET": curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); break; case "PUT": curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); default: break; } // curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json')); $content = curl_exec($ch); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); return array('http_code' => $http_code, 'content' => $content); }