/** @return CUrl */ private function createCurl($url, array $post, $filename) { $request = new Curl\Request($url); $request->setPost($post, array('file' => $filename)); $curl = new Curl\CurlSender(); $curl->setTimeout(30); $curl->options = array('verbose' => 0, 'ssl_verifypeer' => 0, 'ssl_verifyhost' => 2) + $curl->options; $request->setSender($curl); return $request; }
/** * @param string $url * @param string|array $data * @return \Kdyby\Curl\Response * @throws iDefendCurlException */ public function send($url, $data = '') { try { $request = new Request($this->url . $url); $request->setSender($this->curlSender); return $request->post(Json::encode($data)); } catch (CurlException $e) { throw new iDefendCurlException($e->getMessage()); } }
/** * @param Request $request * @param CurlWrapper $curl * @param string $requestId * @param integer $cycles * @throws BadStatusException * @throws FailedRequestException * @return Response */ protected function finishRequest(Request $request, CurlWrapper $curl, $requestId, $cycles) { // request failed if (!$curl->response) { throw new FailedRequestException($curl, $this->queriedRequest); } // build & check response $response = $this->buildResponse($curl); if (($statusCode = $response->headers['Status-Code']) >= 400 && $statusCode < 600) { throw new BadStatusException($response->headers['Status'], $request, $response); } // force redirect on Location header if ($this->isForcingFollowRedirect($curl, $response)) { $request = $this->queriedRequest->followRedirect($response); $response = $this->sendRequest($request, ++$cycles)->setPrevious($response); // override } // log response if ($this->logger && isset($requestId)) { $this->logger->response($response, $requestId); } // return return $response; }
/** * @param Request $request * @param string|array $query * @return mixed * @throws ChainException */ private function sendRequest(Request $request, $query = NULL) { try { $response = NULL; if (!empty($query)) { $request->setSender($this->sender); $response = $request->get($query); } else { $response = $this->sender->send($request); } return Json::decode($response->getResponse(), Json::FORCE_ARRAY); } catch (\Kdyby\Curl\CurlException $ex) { $response = Json::decode($ex->getResponse()->getResponse()); throw new ChainException($response->message, $ex->getCode()); } }
*/ function normalize(array $a) { if ($a['distance'] === PHP_INT_MAX) { return PHP_INT_MAX; } else { return (int) ($a['distance'] * 100000); } } $baseUrl = 'https://graph.facebook.com/'; $ids = [207251779638.0, 11081890741.0, 517762121588320.0, 10196659501.0, 6597757578.0, 118428791504396.0, 23680604925.0]; $token = ''; $pragueLocation = (object) ['lat' => 50.085506, 'lon' => 14.426427]; $result = []; foreach ($ids as $id) { $request = new Request($baseUrl . '/' . $id); try { $response = $request->get(['access_token' => $token]); $response = $response->getResponse(); $decodedResponse = Json::decode($response, Json::FORCE_ARRAY); if (isset($decodedResponse['location']) && isset($decodedResponse['location']['latitude']) && isset($decodedResponse['location']['longitude'])) { $pageLocation = (object) ['lat' => $decodedResponse['location']['latitude'], 'lon' => $decodedResponse['location']['longitude']]; $latDistance = $pageLocation->lat - $pragueLocation->lat; $lonDistance = $pageLocation->lon - $pragueLocation->lon; $distance = sqrt(pow($latDistance, 2) + pow($lonDistance, 2)); } else { $distance = PHP_INT_MAX; } $page = ['id' => $decodedResponse['id'], 'distance' => $distance, 'name' => $decodedResponse['name'], 'country' => NULL, 'city' => NULL, 'likes_count' => $decodedResponse['likes'], 'checkins_count' => $decodedResponse['checkins']]; if (isset($decodedResponse['location'])) { $page['country'] = $decodedResponse['location']['country'];
/** * Changes order status to paid. * * @param string $orderId * @param string $userEmail * @param string $apiKey */ public function orderPaid($orderId, $userEmail, $apiKey) { $userEmail = urlencode($userEmail); $url = $this->url . "/{$orderId}?userEmail={$userEmail}&apiKey={$apiKey}"; $request = new Request($url); $request->get(); }
/** * @param array $data * @throws CommunicationFailedException * @return array */ private function process($data) { $this->onRequest($data); $data = array('USER' => $this->username, 'PWD' => $this->password, 'SIGNATURE' => $this->signature, 'VERSION' => self::API_VERSION) + $data; $request = new Curl\Request($this->host, $data); $request->setSender($this->curlSender); $request->options['verbose'] = TRUE; if (strpos($request->getUrl()->getHost(), '.sandbox.') !== FALSE) { $request->setCertificationVerify(FALSE); $request->options['ssl_verifyHost'] = FALSE; } try { $response = $request->post(http_build_query($data)); $resultData = self::parseNvp($response->getResponse()); $this->onSuccess($resultData, $response->getInfo()); return $resultData; } catch (Curl\FailedRequestException $e) { $this->onError($e, $e->getInfo()); throw new CommunicationFailedException($e->getMessage(), 0, $e); } catch (Curl\CurlException $e) { $this->onError($e, $e->getResponse() ? $e->getResponse()->info : array()); throw new CommunicationFailedException($e->getMessage(), 0, $e); } }
public function getErrorCode() { return $this->curl->getCode(); }
/** * @param $url * @return CurlRequest */ private static function prepareNewCurlRequest($url) { $curlRequest = new CurlRequest($url); $curlSender = new CurlSender(); $curlSender->options['USERPWD'] = 'test:test'; //TODO LM credentials!!! $curlSender->options['timeout'] = 300; //request timeout in seconds $curlRequest->setSender($curlSender); return $curlRequest; }
/** * Withdraws btc. * * @param string $btcAmount * @param string $address * @return array Withdrawal info */ public function withdrawBtc($btcAmount, $address) { $nonce = $this->getNonce(); $params = array('key' => $this->apiKey, 'nonce' => $nonce, 'signature' => $this->getSignature($nonce), 'amount' => $btcAmount, 'address' => $address); $request = new Request(static::BTC_WITHDRAWAL_URL); $request->setTrustedCertificate(CertificateHelper::getCaInfoFile()); $response = $request->post($params); return json_decode($response->getResponse(), TRUE); }
/** * Returns transactions for given address ordered from the oldest. * * @param IAddress $address * @return array of Transaction */ public function getTransactions(IAddress $address) { if (isset($this->addressTransactions[$address->getId()])) { return $this->addressTransactions[$address->getId()]; } // construct first page url $url = static::ADDRESS_API_URL . $address->getId() . '?limit=' . static::ADDRESS_API_LIMIT; // fetch the page $request = new Request($url); $request->setCertificationVerify(FALSE); $response = $request->get()->getResponse(); // parse the response $data = json_decode($response, TRUE); $page = 0; // loop over all pages $transactions = array(); while (count($data['txs']) > 0) { foreach ($data['txs'] as $tx) { if (isset($tx['block_height'])) { // include only confirmed transactions $transaction = $this->getTransaction($tx['hash']); if (!in_array($transaction, $transactions)) { $transactions[] = $this->getTransaction($tx['hash']); } } } // if there is only one page, end the loop during the first run if ($page === 0 && $data['n_tx'] <= static::ADDRESS_API_LIMIT) { break; } // count next offset $page++; $offset = $page * static::ADDRESS_API_LIMIT - $page * static::ADDRESS_API_BUFFER; $nextUrl = "{$url}&offset={$offset}"; // fetch the page $request = new Request($nextUrl); $request->setCertificationVerify(FALSE); $response = $request->get()->getResponse(); // parse the response $data = json_decode($response, TRUE); } // the oldest first $this->addressTransactions[$address->getId()] = array_reverse($transactions); // return result return $this->addressTransactions[$address->getId()]; }
/** * Returns TRUE if address is a valid bitcoin address. * * @param string $address * @return bool */ public function isValidAddress($address) { // construct first page url $url = static::ADDRESS_API_URL . $address . '?limit=1'; // fetch the page $request = new Request($url); $request->setCertificationVerify(FALSE); try { $request->get()->getResponse(); return TRUE; } catch (BadStatusException $e) { return FALSE; } }
/** * @param string $endpoint * * @return Request */ private function createRequest($endpoint) { $request = new Request($this->url . $endpoint); $request->setSender($this->sender); $request->headers['Authorization'] = 'apikey ' . $this->apiKey; return $request; }