public function doRequest($url, array $params = array()) { if (empty($params)) { $postData = ''; } else { $postData = json_encode($params); } $sign = sha1($this->apiKey . '/' . $this->prefix . $url . $postData . $this->apiSecret); if ($postData) { $request = new \Buzz\Message\Request('POST', $this->prefix . $url, $this->host); $request->setContent($postData); } else { $request = new \Buzz\Message\Request('GET', $this->prefix . $url, $this->host); } $request->setHeaders(array('X-Rest-ApiKey' => $this->apiKey, 'X-Rest-ApiSign' => $sign, 'Content-Type' => 'application/json')); $response = new \Buzz\Message\Response(); $this->buzzBrowser->send($request, $response); $responseData = json_decode($response->getContent(), true); if (!$response->isOk()) { $errors = $responseData['errors']; if (is_array($errors)) { foreach ($errors as $e) { throw new RestException($e['message'], $e['code']); } } } if (is_array($responseData) == false) { throw new \Exception('Invalid json response'); } return $responseData; }
public function create(array $options = array()) { $options = array_merge(array('authenticate' => true, 'force_urlencoded' => false, 'raw' => false), $options); $proxy = $this->configuration()->get('proxy'); if (!empty($proxy)) { $options = array_merge(array('proxy' => $proxy), $options); } $connection = new \Buzz\Message\Request(); if (!$this->authentication()->oauthed() && !$this->authentication()->authenticated() && $this->authentication()->unauthedRateLimited()) { $options = array_merge($this->authentication()->unauthedRateLimitParams(), $options); } $listener = false; if ($options['authenticate'] && $this->authentication()->authenticated()) { $authentication = $this->authentication()->authentication(); $listener = new \Buzz\Listener\BasicAuthListener($authentication['login'], $authentication['password']); } if ($options['force_urlencoded']) { $connection->addHeader('Content-Type: application/x-www-form-urlencoded'); } else { $connection->addHeader('Content-Type: application/json'); } $connection->addHeader('User-Agent: ' . $this->configuration()->get('user_agent')); return array('connection' => $connection, 'listener' => $listener, 'options' => $options); }
public function shareOneAction() { $socialCookie = $this->getSocialCookie(); if (!$socialCookie) { echo "login first"; } $request = new \Buzz\Message\Request(); $request->setHost('https://openapi.baidu.com'); $params = array("access_token" => $socialCookie['access_token'], "content" => "bigbang测试分享微博", "url" => "http://www.baidu.com"); $request->setResource("/social/api/2.0/share?" . http_build_query($params)); $response = new \Buzz\Message\Response(); $client = $this->getCurlClient(); $client->send($request, $response); if ($response->isOk()) { var_dump($response->getContent()); } else { echo "error"; } exit; }
/** * Get the host. * * @param Buzz\Message\Request $request * @return string */ protected function getHost($request) { return $request->getHost(); }