/** * Make http request */ function request($url, $method, $postfields = NULL, $auth_header = NULL, $content_type = NULL, $multipart = false) { $this->http_info = array(); $ci = curl_init(); /* Curl settings */ curl_setopt($ci, CURLOPT_USERAGENT, $this->curl_useragent); curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->curl_connect_time_out); curl_setopt($ci, CURLOPT_TIMEOUT, $this->curl_time_out); curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:')); curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->curl_ssl_verifypeer); curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader')); curl_setopt($ci, CURLOPT_HEADER, FALSE); curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE); curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE); if ($multipart) { curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:', $auth_header)); } elseif ($content_type) { curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:', "Content-Type: {$content_type}")); } if ($this->curl_proxy) { curl_setopt($ci, CURLOPT_PROXY, $this->curl_proxy); } switch ($method) { case 'POST': curl_setopt($ci, CURLOPT_POST, TRUE); if (!empty($postfields)) { curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields); } if (!empty($auth_header) && $this->curl_auth_header && !$multipart) { curl_setopt($ci, CURLOPT_HTTPHEADER, array('Content-Type: application/atom+xml', $auth_header)); } break; case 'DELETE': curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE'); if (!empty($postfields)) { $url = "{$url}?{$postfields}"; } } curl_setopt($ci, CURLOPT_URL, $url); $response = curl_exec($ci); $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE); $this->http_info = array_merge($this->http_info, curl_getinfo($ci)); curl_close($ci); //- Hybrid_Error::deleteApiError(); if ($this->http_code != 200) { Hybrid_Error::setApiError($this->http_code . '. ' . preg_replace('/\\s+/', ' ', $response)); } if (defined('WORDPRESS_SOCIAL_LOGIN_DEBUG_API_CALLS')) { do_action('wsl_log_provider_api_call', 'OAuth1', $url, $method, $postfields, $this->http_code, $this->http_info, $response); } //- return $response; }
/** * General data send/request method. * * @param str $method * The data communication method. * @param str $url * The Linkedin API endpoint to connect with. * @param str $data * [OPTIONAL] The data to send to LinkedIn. * @param arr $parameters * [OPTIONAL] Addition OAuth parameters to send to LinkedIn. * * @return arr * Array containing: * * array( * 'info' => Connection information, * 'linkedin' => LinkedIn response, * 'oauth' => The OAuth request string that was sent to LinkedIn * ) */ function fetch($method, $url, $data = NULL, $parameters = array()) { // check for cURL if (!extension_loaded('curl')) { // cURL not present throw new LinkedInException('LinkedIn->fetch(): PHP cURL extension does not appear to be loaded/present.'); } try { // generate OAuth values $oauth_consumer = new OAuthConsumer($this->getApplicationKey(), $this->getApplicationSecret(), $this->getCallbackUrl()); $oauth_token = $this->getToken(); $oauth_token = !is_null($oauth_token) ? new OAuthToken($oauth_token['oauth_token'], $oauth_token['oauth_token_secret']) : NULL; $defaults = array('oauth_version' => self::_API_OAUTH_VERSION); $parameters = array_merge($defaults, $parameters); // generate OAuth request $oauth_req = OAuthRequest::from_consumer_and_token($oauth_consumer, $oauth_token, $method, $url, $parameters); $oauth_req->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $oauth_consumer, $oauth_token); // start cURL, checking for a successful initiation if (!($handle = curl_init())) { // cURL failed to start throw new LinkedInException('LinkedIn->fetch(): cURL did not initialize properly.'); } // set cURL options, based on parameters passed curl_setopt($handle, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($handle, CURLOPT_URL, $url); curl_setopt($handle, CURLOPT_VERBOSE, FALSE); if (isset(Hybrid_Auth::$config["proxy"])) { curl_setopt($handle, CURLOPT_PROXY, Hybrid_Auth::$config["proxy"]); } // configure the header we are sending to LinkedIn - http://developer.linkedin.com/docs/DOC-1203 $header = array($oauth_req->to_header(self::_API_OAUTH_REALM)); if (is_null($data)) { // not sending data, identify the content type $header[] = 'Content-Type: text/plain; charset=UTF-8'; switch ($this->getResponseFormat()) { case self::_RESPONSE_JSON: $header[] = 'x-li-format: json'; break; case self::_RESPONSE_JSONP: $header[] = 'x-li-format: jsonp'; break; } } else { $header[] = 'Content-Type: text/xml; charset=UTF-8'; curl_setopt($handle, CURLOPT_POSTFIELDS, $data); } curl_setopt($handle, CURLOPT_HTTPHEADER, $header); // set the last url, headers $this->last_request_url = $url; $this->last_request_headers = $header; // gather the response $return_data['linkedin'] = curl_exec($handle); $return_data['info'] = curl_getinfo($handle); $return_data['oauth']['header'] = $oauth_req->to_header(self::_API_OAUTH_REALM); $return_data['oauth']['string'] = $oauth_req->base_string; //- $http_code = curl_getinfo($handle, CURLINFO_HTTP_CODE); Hybrid_Error::deleteApiError(); if ($http_code != 200) { Hybrid_Error::setApiError($http_code . '. ' . preg_replace('/\\s+/', ' ', $return_data['linkedin'])); } if (defined('WORDPRESS_SOCIAL_LOGIN_DEBUG_API_CALLS')) { do_action('wsl_log_provider_api_call', 'OAuth1.LinkedIn', $url, $method, $data, $http_code, $this->http_info, $return_data['linkedin']); } //- // check for throttling if (self::isThrottled($return_data['linkedin'])) { throw new LinkedInException('LinkedIn->fetch(): throttling limit for this user/application has been reached for LinkedIn resource - ' . $url); } //TODO - add check for NO response (http_code = 0) from cURL // close cURL connection curl_close($handle); // no exceptions thrown, return the data return $return_data; } catch (OAuthException $e) { // oauth exception raised throw new LinkedInException('OAuth exception caught: ' . $e->getMessage()); } }
protected function request_curl($url, $method = 'GET', $params = array(), $update_claimed_id) { $params = http_build_query($params, '', '&'); $curl = curl_init($url . ($method == 'GET' && $params ? '?' . $params : '')); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/xrds+xml, */*')); if (!empty($this->proxy)) { curl_setopt($curl, CURLOPT_PROXY, $this->proxy['host']); if (!empty($this->proxy['port'])) { curl_setopt($curl, CURLOPT_PROXYPORT, $this->proxy['port']); } if (!empty($this->proxy['user'])) { curl_setopt($curl, CURLOPT_PROXYUSERPWD, $this->proxy['user'] . ':' . $this->proxy['pass']); } } if ($this->verify_peer !== null) { curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $this->verify_peer); if ($this->capath) { curl_setopt($curl, CURLOPT_CAPATH, $this->capath); } if ($this->cainfo) { curl_setopt($curl, CURLOPT_CAINFO, $this->cainfo); } } if ($method == 'POST') { curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $params); } elseif ($method == 'HEAD') { curl_setopt($curl, CURLOPT_HEADER, true); curl_setopt($curl, CURLOPT_NOBODY, true); } else { curl_setopt($curl, CURLOPT_HEADER, true); curl_setopt($curl, CURLOPT_HTTPGET, true); } $response = curl_exec($curl); if ($method == 'HEAD' && curl_getinfo($curl, CURLINFO_HTTP_CODE) == 405) { curl_setopt($curl, CURLOPT_HTTPGET, true); $response = curl_exec($curl); $response = substr($response, 0, strpos($response, "\r\n\r\n")); } //- $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); Hybrid_Error::deleteApiError(); if ($http_code != 200) { Hybrid_Error::setApiError($http_code . '. ' . preg_replace('/\\s+/', ' ', $response)); } if (defined('WORDPRESS_SOCIAL_LOGIN_DEBUG_API_CALLS')) { do_action('wsl_log_provider_api_call', 'OpenID', $url . ($method == 'GET' && $params ? '?' . $params : ''), $method, $params, curl_getinfo($curl), curl_getinfo($curl), $response); } //- if ($method == 'HEAD' || $method == 'GET') { $header_response = $response; # If it's a GET request, we want to only parse the header part. if ($method == 'GET') { $header_response = substr($response, 0, strpos($response, "\r\n\r\n")); } $headers = array(); foreach (explode("\n", $header_response) as $header) { $pos = strpos($header, ':'); if ($pos !== false) { $name = strtolower(trim(substr($header, 0, $pos))); $headers[$name] = trim(substr($header, $pos + 1)); } } if ($update_claimed_id) { # Updating claimed_id in case of redirections. $effective_url = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL); if ($effective_url != $url) { $this->identity = $this->claimed_id = $effective_url; } } if ($method == 'HEAD') { return $headers; } else { $this->headers = $headers; } } if (curl_errno($curl)) { throw new ErrorException(curl_error($curl), curl_errno($curl)); } return $response; }
function request($url, $params = false, $type = "GET") { if ($type == "GET") { $url = $url . (strpos($url, '?') ? '&' : '?') . http_build_query($params, '', '&'); } $this->http_info = array(); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, $this->curl_time_out); curl_setopt($ch, CURLOPT_USERAGENT, $this->curl_useragent); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->curl_connect_time_out); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->curl_ssl_verifypeer); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $this->curl_ssl_verifyhost); curl_setopt($ch, CURLOPT_HTTPHEADER, $this->curl_header); if ($this->curl_proxy) { curl_setopt($ch, CURLOPT_PROXY, $this->curl_proxy); } if ($type == "POST") { curl_setopt($ch, CURLOPT_POST, 1); if ($params) { curl_setopt($ch, CURLOPT_POSTFIELDS, $params); } } $response = curl_exec($ch); $this->http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); $this->http_info = array_merge($this->http_info, curl_getinfo($ch)); curl_close($ch); //- Hybrid_Error::deleteApiError(); if ($this->http_code != 200) { Hybrid_Error::setApiError($this->http_code . '. ' . preg_replace('/\\s+/', ' ', $response)); } if (defined('WORDPRESS_SOCIAL_LOGIN_DEBUG_API_CALLS')) { do_action('wsl_log_provider_api_call', 'OAuth2', $url, $type, $params, $this->http_code, $this->http_info, $response); } //- return $response; }
/** * Makes an HTTP request. This method can be overridden by subclasses if * developers want to do fancier things or use something other than curl to * make the request. * * @param string $url The URL to make the request to * @param array $params The parameters to use for the POST body * @param CurlHandler $ch Initialized curl handle * * @return string The response text */ protected function makeRequest($url, $params, $ch = null) { if (!$ch) { $ch = curl_init(); } $opts = self::$CURL_OPTS; if ($this->getFileUploadSupport()) { $opts[CURLOPT_POSTFIELDS] = $params; } else { $opts[CURLOPT_POSTFIELDS] = http_build_query($params, null, '&'); } $opts[CURLOPT_URL] = $url; // disable the 'Expect: 100-continue' behaviour. This causes CURL to wait // for 2 seconds if the server does not support this header. if (isset($opts[CURLOPT_HTTPHEADER])) { $existing_headers = $opts[CURLOPT_HTTPHEADER]; $existing_headers[] = 'Expect:'; $opts[CURLOPT_HTTPHEADER] = $existing_headers; } else { $opts[CURLOPT_HTTPHEADER] = array('Expect:'); } curl_setopt_array($ch, $opts); $result = curl_exec($ch); $errno = curl_errno($ch); // CURLE_SSL_CACERT || CURLE_SSL_CACERT_BADFILE if ($errno == 60 || $errno == 77) { self::errorLog('Invalid or no certificate authority found, ' . 'using bundled information'); curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . DIRECTORY_SEPARATOR . 'fb_ca_chain_bundle.crt'); $result = curl_exec($ch); } // With dual stacked DNS responses, it's possible for a server to // have IPv6 enabled but not have IPv6 connectivity. If this is // the case, curl will try IPv4 first and if that fails, then it will // fall back to IPv6 and the error EHOSTUNREACH is returned by the // operating system. if ($result === false && empty($opts[CURLOPT_IPRESOLVE])) { $matches = array(); $regex = '/Failed to connect to ([^:].*): Network is unreachable/'; if (preg_match($regex, curl_error($ch), $matches)) { if (strlen(@inet_pton($matches[1])) === 16) { self::errorLog('Invalid IPv6 configuration on server, ' . 'Please disable or get native IPv6 on your server.'); self::$CURL_OPTS[CURLOPT_IPRESOLVE] = CURL_IPRESOLVE_V4; curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); $result = curl_exec($ch); } } } //- $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); Hybrid_Error::deleteApiError(); if ($http_code != 200) { Hybrid_Error::setApiError($http_code . '. ' . preg_replace('/\\s+/', ' ', $result)); } if (defined('WORDPRESS_SOCIAL_LOGIN_DEBUG_API_CALLS')) { do_action('wsl_log_provider_api_call', 'OAuth2.Facebook', $opts[CURLOPT_URL], null, $opts[CURLOPT_POSTFIELDS], $http_code, curl_getinfo($ch), $result); } //- if ($result === false) { $e = new FacebookApiException(array('error_code' => curl_errno($ch), 'error' => array('message' => curl_error($ch), 'type' => 'CurlException'))); curl_close($ch); throw $e; } curl_close($ch); return $result; }