/** * Send an API query with OAuth authorization * * @param array $post Post data * @param object $ch Curl handle * @return array API results */ function api_query($post, &$ch = null) { global $settings; $headerArr = array('oauth_consumer_key' => $settings['gConsumerKey'], 'oauth_token' => $settings['gTokenKey'], 'oauth_version' => '1.0', 'oauth_nonce' => md5(microtime() . mt_rand()), 'oauth_timestamp' => time(), 'oauth_signature_method' => 'HMAC-SHA1'); $signature = sign_request('POST', $settings['apiUrl'], $post + $headerArr); $headerArr['oauth_signature'] = $signature; $header = array(); foreach ($headerArr as $k => $v) { $header[] = rawurlencode($k) . '="' . rawurlencode($v) . '"'; } $header = 'Authorization: OAuth ' . join(', ', $header); if (!$ch) { $ch = curl_init(); } curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_URL, $settings['apiUrl']); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post)); curl_setopt($ch, CURLOPT_HTTPHEADER, array($header)); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_USERAGENT, $settings['gUserAgent']); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $data = curl_exec($ch); if (!$data) { header("HTTP/1.1 {$settings['errorCode']} Internal Server Error"); echo 'Curl error: ' . htmlspecialchars(curl_error($ch)); exit(0); } $ret = json_decode($data); if ($ret === null) { header("HTTP/1.1 {$settings['errorCode']} Internal Server Error"); echo 'Unparsable API response: <pre>' . htmlspecialchars($data) . '</pre>'; exit(0); } return $ret; }
/** * Send an API query with OAuth authorization * * @param array $post Post data * @return array API results */ function signed_api_query($post) { global $settings; $headerArr = array('oauth_consumer_key' => $settings['gConsumerKey'], 'oauth_token' => $settings['gTokenKey'], 'oauth_version' => '1.0', 'oauth_nonce' => md5(microtime() . mt_rand()), 'oauth_timestamp' => time(), 'oauth_signature_method' => 'HMAC-SHA1'); $signature = sign_request('POST', $settings['apiUrl'], $post + $headerArr); $headerArr['oauth_signature'] = $signature; $header = array(); foreach ($headerArr as $k => $v) { $header[] = rawurlencode($k) . '="' . rawurlencode($v) . '"'; } $header = 'Authorization: OAuth ' . join(', ', $header); $data = http_request($settings['apiUrl'], $post, $header); $ret = json_decode($data); if ($ret === null) { header("HTTP/1.1 {$settings['errorCode']} Internal Server Error"); echo 'Unparsable API response: <pre>' . htmlspecialchars($data) . '</pre>'; exit(0); } return $ret; }