本文整理汇总了PHP中wp_safe_remote_request函数的典型用法代码示例。如果您正苦于以下问题:PHP wp_safe_remote_request函数的具体用法?PHP wp_safe_remote_request怎么用?PHP wp_safe_remote_request使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了wp_safe_remote_request函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __construct
function __construct($url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false)
{
$this->url = $url;
$this->timeout = $timeout;
$this->redirects = $redirects;
$this->headers = $headers;
$this->useragent = $useragent;
$this->method = SIMPLEPIE_FILE_SOURCE_REMOTE;
if (preg_match('/^http(s)?:\\/\\//i', $url)) {
$args = array('timeout' => $this->timeout, 'redirection' => $this->redirects);
if (!empty($this->headers)) {
$args['headers'] = $this->headers;
}
if (SIMPLEPIE_USERAGENT != $this->useragent) {
//Use default WP user agent unless custom has been specified
$args['user-agent'] = $this->useragent;
}
$res = wp_safe_remote_request($url, $args);
if (is_wp_error($res)) {
$this->error = 'WP HTTP Error: ' . $res->get_error_message();
$this->success = false;
} else {
$this->headers = wp_remote_retrieve_headers($res);
$this->body = wp_remote_retrieve_body($res);
$this->status_code = wp_remote_retrieve_response_code($res);
}
} else {
$this->error = '';
$this->success = false;
}
}
示例2: __construct
public function __construct($sURL, $iTimeout = 10, $iRedirects = 5, $aHeaders = null, $sUserAgent = null, $bForceFsockOpen = false)
{
$this->timeout = $iTimeout;
$this->redirects = $iRedirects;
$this->headers = $sUserAgent;
$this->useragent = $sUserAgent;
$this->url = $sURL;
// If the scheme is not http or https.
if (!preg_match('/^http(s)?:\\/\\//i', $sURL)) {
$this->error = '';
$this->success = false;
return;
}
// Arguments
$aArgs = array('timeout' => $this->timeout, 'redirection' => $this->redirects, true, 'sslverify' => false);
if (!empty($this->headers)) {
$aArgs['headers'] = $this->headers;
}
if (SIMPLEPIE_USERAGENT != $this->useragent) {
$aArgs['user-agent'] = $this->useragent;
}
// Request
$res = function_exists('wp_safe_remote_request') ? wp_safe_remote_request($sURL, $aArgs) : wp_remote_get($sURL, $aArgs);
if (is_wp_error($res)) {
$this->error = 'WP HTTP Error: ' . $res->get_error_message();
$this->success = false;
return;
}
$this->headers = wp_remote_retrieve_headers($res);
$this->body = wp_remote_retrieve_body($res);
$this->status_code = wp_remote_retrieve_response_code($res);
}
示例3: oauth2callback
/**
* Internal callback, after OAuth
*/
public function oauth2callback()
{
if (empty($_GET['code'])) {
$error = array('provider' => self::PROVIDER_NAME, 'code' => 'oauth2callback_error', 'message' => 'No authorization code received', 'raw' => $_GET);
$this->errorCallback($error);
return;
}
$code = $_GET['code'];
$url = self::TOKEN_ENDPOINT;
$args = array('method' => 'POST', 'headers' => array('Authorization' => 'Basic ' . base64_encode($this->strategy['client_id'] . ':' . $this->strategy['client_secret'])), 'body' => array('code' => $code, 'redirect_uri' => $this->strategy['redirect_uri'], 'grant_type' => 'authorization_code'));
$response = wp_safe_remote_request($url, $args);
if (is_wp_error($response)) {
$error = array('provider' => self::PROVIDER_NAME, 'code' => 'remote_request_error', 'raw' => $response->get_error_message());
$this->errorCallback($error);
return;
}
$responseHeaders = wp_remote_retrieve_body($response);
$results = json_decode($response['body']);
if (!empty($results) && !empty($results->access_token) && !empty($results->xoauth_yahoo_guid)) {
$user_info = $this->userinfo($results->access_token, $results->xoauth_yahoo_guid);
$email = $this->user_email($user_info);
if (!$email) {
$error = array('provider' => self::PROVIDER_NAME, 'code' => 'no_email_error', 'message' => 'No email in the user profile', 'raw' => array('response' => $response, 'headers' => $responseHeaders));
$this->errorCallback($error);
return;
}
$this->auth = array('uid' => $user_info->profile->guid, 'info' => array('nickname' => $user_info->profile->nickname, 'first_name' => $user_info->profile->givenName, 'last_name' => $user_info->profile->familyName, 'location' => $user_info->profile->location, 'image' => $user_info->profile->image->imageUrl, 'profileUrl' => $user_info->profile->profileUrl, 'email' => $email), 'credentials' => array('token' => $results->access_token, 'expires' => date('c', time() + $results->expires_in)), 'raw' => $user_info);
$this->callback();
} else {
$error = array('provider' => self::PROVIDER_NAME, 'code' => 'access_token_error', 'message' => 'Failed when attempting to obtain access token', 'raw' => array('response' => $response, 'headers' => $responseHeaders));
$this->errorCallback($error);
}
}
示例4: request
protected function request($url, $fields = '', $args = array())
{
$defaults = array('follow' => true, 'method' => 'GET', 'ssl_verify' => false, 'body' => '', 'httpversion' => '1.1', 'timeout' => 60);
$args = array_merge($defaults, $args);
$args['body'] = $fields;
$response = wp_safe_remote_request($url, $args);
if (is_wp_error($response)) {
throw new PHP_Merchant_Exception(PHPME_HTTP_REQUEST_FAILED, $response->get_error_message());
}
return $response['body'];
}
示例5: yp_remote_request
function yp_remote_request($url)
{
if (empty($url)) {
return false;
}
$args = array('headers' => array('Accept-Encoding' => ''), 'timeout' => 30, 'user-agent' => 'Yellow Pencil Updater');
$request = wp_safe_remote_request($url, $args);
if ($request['response']['code'] == 200) {
return $request['body'];
}
return false;
}
示例6: get_page
/**
* GET URL
*
* @param string $url
* @param string $username
* @param string $password
* @param bool $head
* @return array
*/
public function get_page($url, $username = '', $password = '', $head = false)
{
// Increase the timeout
add_filter('http_request_timeout', array($this, 'bump_request_timeout'));
$headers = array();
$args = array();
if (true === $head) {
$args['method'] = 'HEAD';
}
if (!empty($username) && !empty($password)) {
$headers['Authorization'] = 'Basic ' . base64_encode("{$username}:{$password}");
}
$args['headers'] = $headers;
return wp_safe_remote_request($url, $args);
}
示例7: http_request
/**
* Performs HTTP Request
*
* @since 1.0
* @see wp_safe_remote_request()
* @param string $method HTTP method to use for request
* @throws Exception for Blank/invalid endpoint or HTTP method, WP HTTP API error
*/
private function http_request($method)
{
// Check for blank endpoint or method
if (!$this->endpoint || !$method) {
throw new Exception(__('Endpoint and / or HTTP Method is blank.', WC_Twilio_SMS::TEXT_DOMAIN));
}
// Check that method is a valid http method
if (!in_array($method, array('GET', 'POST', 'PUT', 'DELETE'))) {
throw new Exception(__('Requested HTTP Method is invalid.', WC_Twilio_SMS::TEXT_DOMAIN));
}
// set the method
$this->wp_remote_http_args['method'] = $method;
// perform HTTP request with endpoint / args
$this->response = wp_safe_remote_request(esc_url_raw($this->endpoint), $this->wp_remote_http_args);
// WP HTTP API error like network timeout, etc
if (is_wp_error($this->response)) {
throw new Exception($this->response->get_error_message());
}
// Check for proper response / body
if (!isset($this->response['response'])) {
throw new Exception(__('Empty Response', WC_Twilio_SMS::TEXT_DOMAIN));
}
if (!isset($this->response['body'])) {
throw new Exception(__('Empty Body', WC_Twilio_SMS::TEXT_DOMAIN));
}
}
示例8: remote_request
/**
* Helper function to query the marketplace API via wp_remote_request.
*
* @param string The url to access.
* @return object The results of the wp_remote_request request.
*
* @access private
* @since 1.0
*/
protected function remote_request($url)
{
if (empty($url)) {
return false;
}
$args = array('headers' => array('Accept-Encoding' => '', 'Authorization' => sprintf('Bearer %s', $this->personal_token)), 'timeout' => 30, 'user-agent' => 'Toolkit/1.7.3');
$request = wp_safe_remote_request($url, $args);
if (is_wp_error($request)) {
echo $request->get_error_message();
return false;
}
$data = json_decode($request['body']);
if ($request['response']['code'] == 200) {
return $data;
} else {
$this->set_error('http_code', $request['response']['code']);
}
if (isset($data->error)) {
$this->set_error('api_error', $data->error);
}
return false;
}
示例9: httpRequest
/**
* Simple server-side HTTP request with file_get_contents
* Provides basic HTTP calls.
* See serverGet() and serverPost() for wrapper functions of httpRequest()
*
* Notes:
* Reluctant to use any more advanced transport like cURL for the time being to not
* having to set cURL as being a requirement.
* Strategy is to provide own HTTP transport handler if requiring more advanced support.
*
* @param string $url Full URL to load
* @param array $options Stream context options (http://php.net/stream-context-create)
* @param string $responseHeaders Response headers after HTTP call. Useful for error debugging.
* @return string Content resulted from request, without headers
*/
public static function httpRequest($url, $options = null, &$responseHeaders = null)
{
$wp_http_args = array('method' => !empty($options['http']['method']) ? $options['http']['method'] : 'GET', 'timeout' => MINUTE_IN_SECONDS, 'redirection' => 0, 'httpversion' => '1.0', 'sslverify' => true, 'blocking' => true, 'body' => !empty($options['http']['content']) ? $options['http']['content'] : '', 'headers' => !empty($options['http']['header']) ? $options['http']['header'] : array(), 'cookies' => array());
// perform request
$response = wp_safe_remote_request($url, $wp_http_args);
// immediately get response headers
$responseHeaders = wp_remote_retrieve_body($response);
if (is_wp_error($response)) {
// network timeout, etc
$content = $response->get_error_message();
} else {
$content = wp_remote_retrieve_body($response);
}
return $content;
}
示例10: delete
/**
* Sends a DELETE request for the given article and bundles.
*
* @param string $url
* @return mixed
* @since 0.2.0
*/
public function delete($url)
{
// Build the delete request args
$args = array('headers' => array('Authorization' => $this->sign($url, 'DELETE')), 'method' => 'DELETE');
// Allow filtering and merge with the default args
$args = apply_filters('apple_news_delete_args', wp_parse_args($args, $this->default_args));
// Perform the delete
$response = wp_safe_remote_request(esc_url_raw($url), $args);
// NULL is a valid response for DELETE
if (is_null($response)) {
return null;
}
// Parse and return the response
return $this->parse_response($response);
}
示例11: request
public static function request($url, $params = null, $args = array())
{
if (!empty($params)) {
$url = add_query_arg($params, $url);
}
$defaults = array('method' => 'GET');
$args = wp_parse_args($args, $defaults);
if (strpos($url, 'http') !== 0) {
$url = Sputnik::API_BASE . $url;
}
$args['timeout'] = 25;
$args['headers']['user-agent'] = 'WP eCommerce Marketplace: ' . WPSC_VERSION;
$args['headers']['X-WP-Domain'] = self::domain();
$request = wp_safe_remote_request(esc_url_raw($url), $args);
if (is_wp_error($request)) {
throw new Exception($request->get_error_message());
}
if ($request['response']['code'] != 200) {
throw new Exception($request['body'], $request['response']['code']);
}
$result = json_decode($request['body']);
if ($result === null) {
throw new Exception($request['body'], $request['response']['code']);
}
$request['body'] = $result;
return $request;
}
示例12: http
protected function http($url, $method, $postfields = NULL)
{
$args = array('method' => $method, 'user-agent' => 'WP eCommerce Marketplace: ' . WPSC_VERSION);
switch ($method) {
case 'POST':
if (!empty($postfields)) {
$args['body'] = $postfields;
}
break;
}
$args['headers'] = array('X-WP-Domain' => Sputnik_API::domain());
$response = wp_safe_remote_request($url, $args);
if (is_wp_error($response)) {
throw new Exception($response->get_error_message());
}
if ($response['response']['code'] != 200) {
throw new Exception($response['body']);
}
return $response['body'];
}
示例13: request
private function request($url, $method, $parameters)
{
$request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters);
$request->sign_request($this->sha1_method, $this->consumer, $this->token);
$args = array('sslverify' => false, 'method' => $method, 'timeout' => 15);
switch ($method) {
case 'GET':
$url = $request->to_url();
$response = wp_safe_remote_get($url, $args);
break;
default:
$url = $request->get_normalized_http_url();
$args = wp_parse_args(array('body' => $request->to_postdata()), $args);
$response = wp_safe_remote_request($url, $args);
break;
}
$this->response_code = wp_remote_retrieve_response_code($response);
$this->response_headers = wp_remote_retrieve_headers($response);
/* Check if we have a valid response */
if (200 == $this->response_code) {
if ($response_body = wp_remote_retrieve_body($response)) {
/* Decode the response body */
$result = json_decode(trim($response_body), true);
/* Check if we failed to decode the response, or the response contains errors */
if (is_null($result) || isset($result['errors'])) {
$this->response_errors = isset($result['errors']) ? $result['errors'] : null;
return false;
}
return $result;
}
}
return false;
}
示例14: http
/**
* Make an HTTP request
*
* @internal Adapted for WP_Http
* @return API results
*/
function http($url, $method, $postfields = NULL)
{
$this->http_info = null;
// this is never used
$options = array('method' => $method, 'timeout' => $this->timeout, 'user-agent' => $this->useragent, 'sslverify' => $this->ssl_verifypeer);
switch ($method) {
case 'POST':
if (!empty($postfields)) {
$options['body'] = $postfields;
}
break;
case 'DELETE':
if (!empty($postfields)) {
$url = "{$url}?{$postfields}";
}
}
$response = wp_safe_remote_request($url, $options);
if (is_wp_error($response)) {
$this->http_code = null;
$this->http_header = array();
return false;
}
$this->http_code = $response['response']['code'];
$this->http_header = $response['headers'];
return $response['body'];
}
示例15: do_remote_request
/**
* Simple wrapper for wp_remote_request() so child classes can override this
* and provide their own transport mechanism if needed, e.g. a custom
* cURL implementation
*
* @since 2.2.0
* @param string $request_uri
* @param string $request_args
* @return array|WP_Error
*/
protected function do_remote_request($request_uri, $request_args)
{
return wp_safe_remote_request($request_uri, $request_args);
}