function jws_fetchUrl($url) { //Can we use cURL? if (is_callable('curl_init')) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 20); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $feedData = curl_exec($ch); curl_close($ch); //If not then use file_get_contents } elseif (ini_get('allow_url_fopen') == 1 || ini_get('allow_url_fopen') === TRUE) { $feedData = @file_get_contents($url); //Or else use the WP HTTP API } else { if (!class_exists('WP_Http')) { include_once ABSPATH . WPINC . '/class-http.php'; } $request = new WP_Http(); $result = $request->request($url); $feedData = $result['body']; } /* echo $feedData; exit;*/ return $feedData; }
/** * @param string $url * @param array $params * @param string $method * @param bool $has_return_value * * @throws RuntimeException * * @return null|string */ private function call_remote_api($url, $params, $method, $has_return_value = true) { $context = $this->filter_request_params($params, $method); $response = $this->http->request($url, $context); if ($has_return_value && (bool) $response === false || is_wp_error($response) || isset($response['response']['code']) && $response['response']['code'] > 400) { throw new RuntimeException("Cannot communicate with the remote service params: " . serialize($context) . ' response: ' . serialize($response)); } return $response; }
/** * Makes a http request to the url this points at and checks if the requests * returns the correct validation result. * * @return bool */ public function is_valid() { $url_glue = false === strpos($this->url, '?') ? '?' : '&'; $url = trailingslashit($this->url) . ($this->language_code ? '/' . $this->language_code . '/' : '') . $url_glue . '____icl_validate_domain=1'; $response = $this->http->request($url, 'timeout=15'); return !is_wp_error($response) && $response['response']['code'] == '200' && ($response['body'] === '<!--' . untrailingslashit($this->wp_api->get_home_url()) . '-->' || $response['body'] === '<!--' . untrailingslashit($this->wp_api->get_site_url()) . '-->'); }
/** * Gets content from GitHub Gist * * @param int $id GitHub Gist ID * @param int $ttl How long to cache (in seconds) * @param string $bump Bump value to force cache expirey. * @param string $file Name of file */ function embed_github_gist($id, $ttl = null, $bump = null, $file = null) { $gist = ''; if (!class_exists('WP_Http')) { require_once ABSPATH . WPINC . '/class-http.php'; } $key = embed_github_gist_build_cache_key($id, $bump); if (embed_github_gist_bypass_cache() or false === ($gist = get_transient($key))) { $http = new WP_Http(); if (embed_github_gist_prefer_inline_html() and function_exists('json_decode')) { $result = $http->request('https://gist.github.com/' . $id . '.json'); $json = json_decode($result['body']); $gist = $json->div; } else { if (!$file) { $file = 'file'; } $result = $http->request('https://gist.github.com/raw/' . $id . '/' . $file); $gist = '<script src="https://gist.github.com/' . $id . '.js?file=' . $file . '%5B345%5D" type="text/javascript"></script>'; $gist .= '<noscript><div class="embed-github-gist-source"><code><pre>'; $gist .= htmlentities($result['body']); $gist .= '</pre></code></div></noscript>'; } unset($result, $http); if (!embed_github_gist_bypass_cache()) { if (!$ttl) { $ttl = EMBED_GISTHUB_DEFAULT_TTL; } set_transient($key, $gist, $ttl); } } return $gist; }
function do_request($url, $oauth_header, $body_params = '') { $request = new WP_Http(); $params = array(); if ($body_params) { foreach ($body_params as $key => $value) { $body_params[$key] = $value; } $params['body'] = $body_params; } $params['method'] = 'POST'; $params['headers'] = array('Authorization' => $oauth_header); $result = $request->request($url, $params); if (!is_wp_error($result)) { $this->response_code = $result['response']['code']; if ($result['response']['code'] == '200') { return $result['body']; } else { switch ($result['response']['code']) { case 403: $this->duplicate_tweet = true; break; } $error_message_found = preg_match('#<error>(.*)</error>#i', $result['body'], $matches); if ($error_message_found) { $this->error_message = $matches[1]; } } } return false; }
/** * Send an authenticated request to the Urban Airship API. The request is * authenticated with the key and secret. * * @param string $method REST method for request * @param mixed $body Body of request, optional * @param string $uri URI for this request * @param string $contentType Content type for the request, optional * @param int $version version # for API, optional, default is 3 * @param mixed $request Request object for this operation (PushRequest, etc) * optional * @return \Httpful\associative|string * @throws AirshipException */ public function request($method, $body, $uri, $contentType = null, $version = 3, $request = null) { // As a result of replacing Httpful/Request with WP HTTP, // We need to map WP_HTTP response object to Httpful/Request properties as a shim, // Action is not necessary but looks a bit cleaner add_action('http_api_debug', array($this, 'set_mock_response_object'), 10, 5); $headers = array('Authorization' => 'Basic ' . base64_encode("{$this->key}:{$this->secret}"), "Accept" => sprintf(self::VERSION_STRING, $version)); if (!is_null($contentType)) { $headers["Content-type"] = $contentType; } $request = new \WP_Http(); /** * Logger is disabled in production, so this won't do nothing unless WP_DEBUG is enabled * * @var [type] */ $logger = UALog::getLogger(); $logger->debug("Making request", array("method" => $method, "uri" => $uri, "headers" => $headers, "body" => $body)); // Make a request (fires http_api_debug action that sets object property $mock_response) $response = $request->request($uri, array('method' => $method, 'body' => $body, 'headers' => $headers)); // Check the response for wp_error (that's what WP HTTP throws when there was an issue with request) if (is_wp_error($response)) { return $response; } // Check for "successful" WP HTTP request and see if UA returns any non-2xx response code if (300 <= $response['response']['code']) { throw AirshipException::fromResponse($this->mock_response); } $logger->debug("Received response", array("status" => $this->mock_response->code, "headers" => $this->mock_response->raw_headers, "body" => $this->mock_response->raw_body)); // Return mock response object for any components of UA library that make requests return $this->mock_response; }
function cff_fetchUrl($url) { //Can we use cURL? if (is_callable('curl_init')) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 20); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate,sdch'); $feedData = curl_exec($ch); curl_close($ch); //If not then use file_get_contents } elseif (ini_get('allow_url_fopen') == 1 || ini_get('allow_url_fopen') === TRUE) { $feedData = @file_get_contents($url); //Or else use the WP HTTP API } else { $request = new WP_Http(); $response = $request->request($urls, array('timeout' => 60, 'sslverify' => false)); if (is_wp_error($response)) { //Don't display an error, just use the Server config Error Reference message echo ''; } else { $feedData = wp_remote_retrieve_body($response); } } return $feedData; }
/** * Method responsible for making Rest API calls @ Return $response from rest api. * * @return $response */ public function rest_api_call() { try { if (!class_exists('WP_Http')) { include_once ABSPATH . WPINC . '/class-http.php'; } $request = new WP_Http(); $postfields = $this->getPostfields(); $header = $this->getHeaders(); $url = $this->getApiUrl(); $options = array('headers' => $header, 'method' => $this->method, 'timeout' => ConfigSDK::TIMEOUT); if (!is_null($postfields)) { $options['body'] = $postfields; } //SSL PROBLEMS $options['sslverify'] = false; $result = $request->request($url, $options); if (!is_wp_error($result)) { $json = $result['body']; $response = json_decode($json, true); $response_status = $result['response']['code']; $this->setLastStatusCode($response_status); } else { $this->setLastStatusCode(0); } if ($this->authorization->is_preauthorized_request) { $this->authorization->setAccessParams($response); } return $response; } catch (Exception $e) { throw new Exception('Failed to send Data in rest_api_call: ' . $e); } }
function kpg_read_file($f, $method = 'GET') { // try this using Wp_Http if (!class_exists('WP_Http')) { include_once ABSPATH . WPINC . '/class-http.php'; } $request = new WP_Http(); $parms = array(); $parms['timeout'] = 10; // bump timeout a little we are timing out in google $parms['method'] = $method; $result = $request->request($f, $parms); // see if there is anything there if (empty($result)) { return ''; } if (is_array($result)) { $ansa = $result['body']; return $ansa; } if (is_object($result)) { $ansa = 'ERR: ' . $result->get_error_message(); return $ansa; // return $ansa when debugging //return ''; } return ''; }
/** * Load IPN IP List * @since 1.1 * @version 1.0 */ public function load_ipn_ips() { $request = new WP_Http(); $data = $request->request('http://www.zombaio.com/ip_list.txt'); $data = explode('|', $data['body']); return $data; }
function genesis_purchase_themes_admin() { ?> <div class="wrap purchase-themes"> <?php screen_icon('themes'); ?> <h2><?php echo 'Genesis '; _e('- Purchase Themes', 'genesis'); ?> </h2> <?php $store = get_transient('genesis-remote-store'); if (!$store) { $request = new WP_Http(); $store = $request->request('http://www.studiopress.com/store.php'); set_transient('genesis-remote-store', $store, 60 * 60 * 12); // store for 12 hours } echo $store['body']; ?> </div> <?php }
/** * @param string $path * @param string $method * @param array $data * @param string $contentType * * @return array * @throws CommunicationError * @throws Exception\InvalidResponseError * @throws Exception\LaunchKeyEngineError * @throws Exception\NoPairedDevicesError * @throws Exception\NoSuchUserError * @throws Exception\RateLimitExceededError * @throws ExpiredAuthRequestError * @throws InvalidCredentialsError * @throws InvalidRequestError */ private function sendRequest($path, $method, array $data = array(), array $parameters = array(), $contentType = 'application/x-www-form-urlencoded') { $headers = array('Accept' => 'application/json', 'Connection' => 'close'); if (!empty($data)) { $headers['Content-Type'] = $contentType; if ($contentType === 'application/x-www-form-urlencoded') { $body = http_build_query($data); } elseif ($contentType === 'application/json') { $body = json_encode($data); $parameters['signature'] = $this->cryptService->sign($body); } } else { $body = null; } if (!empty($parameters)) { $path .= '?' . http_build_query($parameters); } $this->debugLog("Sending request", array('path' => $path, 'method' => $method, 'headers' => $headers, 'body' => $body)); $response = $this->http->request($this->getUrl($path), array('method' => $method, 'timeout' => $this->requestTimeout, 'redirection' => 0, 'httpversion' => '1.1', 'sslverify' => $this->sslVerify, 'body' => $body, 'headers' => $headers)); if ($response instanceof \WP_Error) { $msg = implode(' => ', $response->get_error_messages()); throw new CommunicationError($msg); } else { $this->debugLog("Response received", array($response)); $data = $this->jsonDecodeData($response['body']); if (!in_array($response['response']['code'], array(200, 201))) { $this->throwExceptionForErrorResponse($data); } } return $data; }
function ml_send_notification($alert, $sound = true, $badge = NULL, $custom_properties = NULL, $tags = NULL, $remote_identifier = NULL) { global $ml_api_key, $ml_secret_key; //push notification only when api key is set if (($ml_api_key == NULL || strlen($ml_api_key) < 5) && ($ml_secret_key == NULL || strlen($ml_secret_key) < 5)) { return false; } $notification = array('alert' => $alert); if ($sound) { $notification['sound'] = $sound; } if ($badge) { $notification['badge'] = $badge; } if ($custom_properties) { $notification['custom_properties'] = $custom_properties; } if ($tags) { $notification['tags'] = $tags; } $parameters = array('api_key' => $ml_api_key, 'secret_key' => $ml_secret_key, 'notification' => $notification); //postID if ($remote_identifier) { $parameters['remote_identifier'] = "{$remote_identifier}"; } $request = new WP_Http(); $result = $request->request(MOBILOUD_PUSH_API_PUBLISH_URL, array('method' => 'POST', 'timeout' => 10, 'body' => $parameters)); return false; }
function socialdiscuss_http_post($link, $body) { if (!$link) { return array(500, 'Invalid Link'); } require_once ABSPATH . WPINC . '/class-snoopy.php'; $snoop = new Snoopy(); if ($snoop->submit($link, $body)) { if (strpos($snoop->response_code, '200')) { $response = $snoop->results; return array(200, $response); } } if (!class_exists('WP_Http')) { include_once ABSPATH . WPINC . '/class-http.php'; } if (!class_exists('WP_Http')) { return array(500, $snoop->response_code); } $request = new WP_Http(); $response_full = $request->request($link, array('method' => 'POST', 'body' => $body, 'headers' => $headers)); if (isset($response_full->errors)) { return array(500, 'Unknown Error'); } $response_code = $response_full['response']['code']; if ($response_code === 200) { $response = $response_full['body']; return array($response_code, $response); } $response_msg = $response_full['response']['message']; return array($response_code, $response_msg); }
function prli_get_main_message($message = '', $expiration = 1800) { global $prli_update; // Set the default message if (empty($message)) { $message = __("Get started by <a href=\"?page=pretty-link&action=new\">" . "adding a URL</a> that you want to turn into a pretty link.<br/>" . "Come back to see how many times it was clicked.", 'pretty-link'); } $messages = get_site_transient('_prli_messages'); // if the messages array has expired go back to the mothership if (!$messages) { $remote_controller = $prli_update->pro_is_installed_and_authorized() ? 'prlipro' : 'prli'; $message_mothership = "http://prettylinkpro.com/index.php?controller={$remote_controller}&action=json_messages"; if (!class_exists('WP_Http')) { include_once ABSPATH . WPINC . '/class-http.php'; } $http = new WP_Http(); $response = $http->request($message_mothership); if (isset($response) and is_array($response) and isset($response['body']) and !empty($response['body'])) { $messages = json_decode($response['body']); } else { $messages = array($message); } set_site_transient("_prli_messages", $messages, $expiration); } if (empty($messages) or !$messages or !is_array($messages)) { return $message; } else { return $messages[array_rand($messages)]; } }
/** * rpc_call function. * * @access public * @param string $method. (default: '') * @param array $params. (default: array()) * @return void */ function rpc_call($method = '', $params = array()) { $request = new WP_Http(); if (function_exists('xmlrpc_encode_request')) { $params = xmlrpc_encode_request($method, $params); } return $request->request($this->getServiceUrl(), array('method' => 'POST', 'body' => $params)); }
function wp2android_display_charts() { global $ml_api_key, $ml_secret_key; $parameters = array('api_key' => $ml_api_key, 'secret_key' => $ml_secret_key); //SUBSCRIPTIONS $request = new WP_Http(); $ml_host = "https://api.wp2android.wps.edu.vn"; $url_subscriptions = "{$ml_host}/product/stats/devices/count"; $result_subscriptions = $request->request($url_subscriptions, array('method' => 'POST', 'timeout' => 10, 'body' => $parameters)); if ($result_subscriptions) { $json_subscriptions = $result_subscriptions['body']; } //MODELS $request = new WP_Http(); $url_models = "{$ml_host}/product/stats/devices/models"; $result_models = $request->request($url_models, array('method' => 'POST', 'timeout' => 10, 'body' => $parameters)); if ($result_models) { $json_models = $result_models['body']; } //SESSIONS $request = new WP_Http(); $url_sessions = "{$ml_host}/product/stats/events/sessions"; $result_sessions = $request->request($url_sessions, array('method' => 'POST', 'timeout' => 10, 'body' => $parameters)); if ($result_sessions) { $json_sessions = $result_sessions['body']; } ?> <script type="text/javascript" src="<?php echo $ml_host; ?> /assets/highcharts.js"></script> <div id="wp2android_analytics_title" style="margin-top:20px;"> <h1>wp2android Analytics</h1> <div style="clear:both;"> </div> <table width="100%"> <tr> <td align="center"> <div id="wp2android_subscriptions_chart" style="width: 400px; height: 400px;margin-top:50px;margin-left:50px;"></div> </td> <td align="center"> <div id="wp2android_sessions_chart" style="width: 400px; height: 400px;margin-top:50px;margin-left:50px;"></div> </td> </tr> <tr> <td align="center" colspan=2> <div id="wp2android_models_chart" style="width: 700px; height: 400px;margin-top:50px;"></div> </td> </tr> </table> <?php }
/** * {@inheritdoc} */ protected function _request($url, $args, Data $options) { $httpClient = new \WP_Http(); $apiResponse = $httpClient->request($url, array('body' => $args, 'method' => $options->get('method'), 'headers' => $options->get('headers'), 'timeout' => $options->get('timeout'), 'user-agent' => $options->get('user_agent'), 'sslverify' => $options->get('ssl_verify'), 'redirection' => 20)); if ($options->get('debug') && $apiResponse instanceof \WP_Error) { $apiResponse = array('body' => implode($apiResponse->get_error_messages())); } return $apiResponse; }
public function request($url, $args = array()) { $http = new WP_Http(); if (isset($args['data'])) { $args['body'] = $args['data']; unset($args['data']); } return $http->request($url, $args); }
public function validate_domain_networking($posted_url, $code = false) { $string_valid = $this->validate_url_string($posted_url); if ($string_valid === true) { $url_glue = false === strpos($posted_url, '?') ? '?' : '&'; $url = trailingslashit($posted_url) . ($code ? '/' . $code . '/' : '') . $url_glue . '____icl_validate_domain=1'; $client = new WP_Http(); $response = $client->request($url, 'timeout=15'); } return isset($response) && !is_wp_error($response) && $response['response']['code'] == '200' && ($response['body'] === '<!--' . untrailingslashit(get_home_url()) . '-->' || $response['body'] === '<!--' . untrailingslashit(get_site_url()) . '-->') ? 1 : 0; }
/** * @param string $url * @param array $params * @param string $method * @param bool $has_return_value * * @throws RuntimeException * * @return null|string */ private function call_remote_api($url, $params, $method, $has_return_value = true) { $context = $this->filter_request_params($params, $method); $response = $this->http->request($url, $context); if (is_wp_error($response) || $has_return_value && (bool) $response === false || isset($response['response']['code']) && $response['response']['code'] > 400) { $sanitized_context = WPML_TranslationProxy_Com_Log::sanitize_data($context); $sanitized_response = WPML_TranslationProxy_Com_Log::sanitize_data($response); throw new RuntimeException('Cannot communicate with the remote service params: `' . serialize($sanitized_context) . '` response: `' . serialize($sanitized_response) . '`'); } return $response; }
function ml_configuration_connection_test_callback() { global $ml_server_host; $request = new WP_Http(); $url = "{$ml_server_host}"; $result = $request->request($url, array('method' => 'GET', 'timeout' => 10)); if ($result) { print_r($result); } else { echo "Request returned NULL"; } die; }
function googl_shortlink($url) { $http = new WP_Http(); $headers = array('Content-Type' => 'application/json'); $result = $http->request('https://www.googleapis.com/urlshortener/v1/url', array('method' => 'POST', 'body' => '{"longUrl": "' . $url . '"}', 'headers' => $headers)); $result = json_decode($result['body']); $shortlink = $result->id; if ($shortlink) { add_post_meta($post_id, '_googl_shortlink', $shortlink, true); return $shortlink; } else { return $url; } }
function import_wordpress_comments(&$wxr, $timestamp, $eof = true) { $http = new WP_Http(); $response = $http->request(DISQUS_IMPORTER_URL . 'api/import-wordpress-comments/', array('method' => 'POST', 'timeout' => 60, 'body' => array('forum_url' => $this->short_name, 'forum_api_key' => $this->forum_api_key, 'response_type' => 'php', 'wxr' => $wxr, 'timestamp' => $timestamp, 'eof' => (int) $eof))); if ($response['response']['code'] !== 200) { // hack $this->api->last_error = $response->errors; return -1; } $data = unserialize($response['body']); if (!$data || $data['stat'] == 'fail') { return -1; } return $data; }
public function request($url, $args = array()) { if (file_exists(LFAPPS__PLUGIN_PATH . '/../vip-init.php')) { if (isset($args['data'])) { $args['body'] = $args['data']; unset($args['data']); } return vip_safe_wp_remote_get($url, $args); } else { $http = new WP_Http(); if (isset($args['data'])) { $args['body'] = $args['data']; unset($args['data']); } return $http->request($url, $args); } }
function wpdev_ajax_check_bk_news() { $v = array(); if (class_exists('wpdev_bk_personal')) { $v[] = 'wpdev_bk_personal'; } if (class_exists('wpdev_bk_biz_s')) { $v[] = 'wpdev_bk_biz_s'; } if (class_exists('wpdev_bk_biz_m')) { $v[] = 'wpdev_bk_biz_m'; } if (class_exists('wpdev_bk_biz_l')) { $v[] = 'wpdev_bk_biz_l'; } if (class_exists('wpdev_bk_multiuser')) { $v[] = 'wpdev_bk_multiuser'; } $obc_settings = array(); $ver = get_bk_option('bk_version_data'); if ($ver !== false) { $obc_settings = array('subscription_key' => maybe_serialize($ver)); } $params = array('action' => 'get_news', 'subscription_email' => isset($obc_settings['subscription_email']) ? $obc_settings['subscription_email'] : false, 'subscription_key' => isset($obc_settings['subscription_key']) ? $obc_settings['subscription_key'] : false, 'bk' => array('bk_ver' => WPDEV_BK_VERSION, 'bk_url' => WPDEV_BK_PLUGIN_URL, 'bk_dir' => WPDEV_BK_PLUGIN_DIR, 'bk_clss' => $v), 'siteurl' => get_option('siteurl'), 'siteip' => $_SERVER['SERVER_ADDR'], 'admin_email' => get_option('admin_email')); $request = new WP_Http(); $result = $request->request(OBC_CHECK_URL . 'info/', array('method' => 'POST', 'timeout' => 15, 'body' => $params)); if (!is_wp_error($result) && $result['response']['code'] == '200' && true) { $string = $result['body']; //$string = str_replace( "'", ''', $string ); echo $string; } else { // Some error appear echo '<div id="bk_errror_loading">'; if (is_wp_error($result)) { echo $result->get_error_message(); } else { echo $result['response']['message']; } echo '</div>'; echo ' <script type="text/javascript"> '; echo ' document.getElementById("bk_news").style.display="none";'; echo ' jQuery("#bk_news_section").animate({opacity:1},3000).slideUp(1500);'; echo ' </script> '; } }
/** * Fetches a secured oauth url and returns the response body. * * @param string $uri * @param mixed $arguments * @param string $method * @param array $httpHeaders * @return string */ public function fetch($uri, $arguments = array(), $method = 'GET', $httpHeaders = array()) { $requestParams = array(); $requestParams['method'] = $method; $oAuthHeader = $this->getOAuthHeader($uri, $arguments, $method); $requestParams['headers'] = array_merge($httpHeaders, $oAuthHeader); // arguments will be passed to uri for GET, to body for POST etc. if ($method == 'GET') { $uri .= '?' . http_build_query($arguments); } else { if (count($arguments)) { $requestParams['body'] = $arguments; } } $request = new WP_Http(); //$uri = str_replace('api.dropbox.com', 'localhost:12346', $uri); $result = $request->request($uri, $requestParams); return array('httpStatus' => $result['response']['code'], 'body' => $result['body']); }
function ajax_sendsms() { $ret = array('success' => false); if (!wp_verify_nonce(trim($_POST['_wpnonce']), 'send-sms-form')) { $ret['message'] = '非法访问!'; _json_die($ret); } $register_id = (int) trim($_POST['register-id']); $phone = trim($_POST['mobile']); if (!is_numeric($phone) || strlen($phone) != 11) { $ret['message'] = '非法手机号码!' . $phone; _json_die($ret); } $options = get_option("salon_options"); $body = array('CorpID' => $options['sms-username'], 'Pwd' => $options['sms-password'], 'Mobile' => $phone, 'Content' => trim($_POST['sms-text'])); $pattern = "/【.*?】\$/"; if (!preg_match($pattern, $body['Content'])) { $ret['message'] = '短信内容不包含签名,无法发送!'; _json_die($ret); } $url = 'http://120.132.132.102/WS/BatchSend2.aspx'; $request = new WP_Http(); $response = $request->request($url, array('method' => 'POST', 'body' => $body)); //$response = array('response' => array('code'=>200 ),'body'=>1); if ((int) $response['response']['code'] == 200) { $code = (int) $response['body']; if ($code > 0) { //取短信报告 $ret['success'] = true; $ret['message'] = '短信发送成功!'; //修改已发短信状态 if ($register_id > 0) { update_post_meta($register_id, 'send_sms', 1); wp_publish_post($register_id); } } else { $ret['message'] = '短信发送失败!' . $response['body']; } } else { $ret['message'] = "http错误! code=" . $response['response']['code']; } _json_die($ret); }
/** * Helper function to send post http requests * * @param array $params the parameters to send * @param string $url the url to access * @param string $method the connection method * @param array $headers the custom headers to send with the request * * @return array as follows: * array( * 'headers'=>an array of response headers, such as "x-powered-by" => "PHP/5.2.1", * 'body'=>the response string sent by the server, as you would see it with you web browser * 'response'=>an array of HTTP response codes. Typically, you'll want to have array('code'=>200, 'message'=>'OK'), * 'cookies'=>an array of cookie information * ) */ function wiziapp_general_http_request($params, $url, $method = 'POST', $headers = array()) { global $wp_version; /* * Wordpress 2.7 and above have a built in class for that. * Using it will let us be more flexible with our requirements. */ if (!class_exists('WP_Http', false)) { include_once ABSPATH . WPINC . '/http.php'; } // Now, the HTTP request: $body = $params; $headers = array_filter($headers); $GLOBALS['WiziappLog']->write('debug', "Contacting wiziapp server with headers: " . print_r($headers, TRUE) . " and params: " . print_r($params, TRUE), "http_requests.wiziapp_general_http_request"); $request = new WP_Http(); $result = $request->request($url, array('method' => $method, 'timeout' => 5, 'blocking' => TRUE, 'body' => $body, 'sslverify' => FALSE, 'headers' => $headers)); $GLOBALS['WiziappLog']->write('info', "The result was: " . print_r($result, TRUE), "http_requests.wiziapp_general_http_request"); return $result; }
function feedpress_api_call($url, $params = array(), $type='GET') { $options = get_option('feedpress'); $json = array(); $url .= '.json'; $params['key'] = '50d45a6bef51d'; $params['token'] = $options['feedpress_token']; $request = new WP_Http; if ($type == 'GET') { $qs = http_build_query($params, '', '&'); $result = $request->request('http://api.feedpress.it/'.$url.'?'.$qs); if (is_array($result) && isset($result['response']['code']) && $result['response']['code'] == 200) { $json = json_decode($result['body']); } } elseif ($type == 'POST') { $result = $request->request('http://api.feedpress.it/'.$url, array( 'method' => 'POST', 'body' => $params ) ); if (is_array($result) && isset($result['response']['code']) && $result['response']['code'] == 200) { $json = json_decode($result['body']); } } return $json; }