/** * Выполняет запрос к API и возвращает json ответа * * @param $api * @param array $options * @return mixed */ public function getRequestData($api, array $options = []) { $headers = []; $response = $this->client->get($api, $headers, $options)->send(); //todo throw exception if status != 200 and log it $result = json_decode($response->getBody(), true); return $result; }
/** * Send a GET request * * @param string $uri * @return \Guzzle\Http\Message\Response */ public function get($uri) { // add the authorization header $headers['Authorization'] = "Bearer {$this->_generateBearerToken()}"; $headers['X-ApplicationId'] = $this->appId; $response = $this->client->get($uri, $headers)->setAuth('developers.chromedia', 'cfe-developers', 'Bearer')->send(); return $response; }
public function downloadTorrent($torrentId) { $request = $this->restClient->get(self::T411_API_BASE_URL . '/torrents/download/' . $torrentId); $request->addHeader('Authorization', $this->token); $response = $request->send(); $result = $response->getBody(true); $this->handleResponse($result); //Enregistre le torrent quelque part $detailsTorrent = $this->torrentDetails($torrentId); $torrentPath = $this->tmpFolder . '/' . $detailsTorrent->rewritename . '.torrent'; file_put_contents($torrentPath, $result); return $torrentPath; }
/** * Execute the query. * * @param string $url * @param array $params * * @return string|boolean */ public function execute($url, $params = []) { $uri = rtrim(rtrim($this->site, '/') . '/' . ltrim($url, '/') . '?' . http_build_query($params), '?'); try { $result = $this->client->get($uri, ['timeout' => 10])->getBody(true); return $this->format === 'json' ? json_decode($result) : (string) $result; } catch (\Exception $e) { if ($this->isRetry) { return false; } $this->isRetry = true; $this->site = str_replace('https://', 'http://', $this->site); return $this->execute($url, $params); } }
public function productAction() { $client = new Client('http://stroyka.by'); $request = $client->get('/'); $request->send(); $resault = $request->getResponse()->getBody(true); $crawler = new Crawler(); // $crawler->addHTMLContent($resault); // $res = $request->getResponse()->getH; // var_dump($res);exit(); // $text = utf8_decode($resault); $crawler = new Crawler($resault); $resault = $crawler->filter('ul.b-categories')->html(); // // // $crowler2 = new Crawler($resault); // // $resault = $crowler2->filter('span.b-categories__name'); // // $nodeValues = $crawler->filter('span.b-categories__name')->each(function (Crawler $node, $i) { // return utf8_decode($node->text()); // }); // var_dump(mb_detect_encoding($nodeValues[0]));exit(); // foreach($nodeValues as $nv) { // $category = new Category(); // $category->setName($nv); // $this->getDoctrine()->getManager()->persist($category); // } // $this->getDoctrine()->getManager()->flush(); // var_dump($nodeValues); // var_dump($resault); // exit; return $this->render('AcmeAppBundle:Default:product.html.twig', array('result' => $resault)); }
/** * Execute the query. * * @param string $url * @param array $params * * @return string|boolean */ public function execute($url, $params = []) { $uri = rtrim(rtrim($this->site, '/') . '/' . ltrim($url, '/') . '?' . http_build_query($params), '?'); try { $result = $this->client->get($uri, ['timeout' => 10])->getBody(true); return $this->format === 'json' ? json_decode($result) : (string) $result; } catch (\Exception $e) { if ($this->isRetry) { $msg = "Error connecting to the Extension Marketplace:\n" . $e->getMessage(); throw new ExtensionsInfoServiceException($msg, $e->getCode(), $e); } $this->isRetry = true; $this->site = str_replace('https://', 'http://', $this->site); return $this->execute($url, $params); } }
public function resolve() { $deps = array($this->package); $resolved = array(); $guzzle = new Client('http://packagist.org'); while (count($deps) > 0) { $package = $this->rename(array_pop($deps)); if (!$package) { continue; } try { $response = $guzzle->get('/packages/' . $package . '.json')->send()->getBody(true); } catch (\Exception $e) { continue; } $package = json_decode($response); if (!is_null($package)) { foreach ($package->package->versions as $version) { if (!isset($version->require)) { continue; } foreach ($version->require as $dependency => $version) { if (!in_array($dependency, $resolved) && !in_array($dependency, $deps)) { $deps[] = $dependency; $deps = array_unique($deps); } } } echo " * Package {$package->package->name} was added\n"; $resolved[] = $package->package->name; } } return $resolved; }
/** * Get digest credits */ public function getDigest($date) { $client = new Client('https://graph.facebook.com/oauth/access_token?client_id={company_id}&client_secret={company_secret}&grant_type=client_credentials', array('company_id' => $this->companyId, 'company_secret' => $this->companySecret)); $response = $client->get()->send(); if (200 !== $response->getStatusCode()) { throw new Exception("Can't get access token using passed company id and secret"); } $token = $response->getBody(); parse_str($token); $dailyDigestUrl = "https://paymentreports.facebook.com/{company_id}/report?date={date}&type=digest&access_token={access_token}"; $client = new Client($dailyDigestUrl, array('company_id' => $this->companyId, 'access_token' => $access_token, 'date' => $date)); $response = $client->get()->send(); if (200 !== $response->getStatusCode()) { throw new \Exception("Can't download daily detail report"); } $cacheDir = $this->container->get('kernel')->getRootDir() . '/cache'; $extractDir = $cacheDir . '/tmp-facebook-report'; @mkdir($extractDir, 0755, true); $zipFileName = tempnam($extractDir, "fb_detail_report") . ".zip"; file_put_contents($zipFileName, $response->getBody()); $zip = new \ZipArchive(); if ($zip->open($zipFileName) === TRUE) { $zip->extractTo($extractDir); $zip->close(); @unlink($zipFileName); $finder = new Finder(); $finder->files()->name($this->companyId . '_digest_*.csv')->in($extractDir); foreach ($finder as $file) { $stop = true; $header = array(); $rows = array(); foreach (file($file->getRealpath()) as $line) { if (preg_match('/^SH.*credits_digest$/', $line)) { $stop = false; continue; } if (preg_match('/^SF/', $line)) { $stop = true; continue; } if (preg_match('/^CH/', $line)) { $line = preg_replace('/^CH,/', '', $line); $header = str_getcsv($line); continue; } if (!$stop) { $line = preg_replace('/^SD,/', '', $line); $columns = str_getcsv($line); $row = array_combine($header, $columns); $rows[] = $row; } } @unlink($file->getRealpath()); return $rows; } } else { throw new Exception("Can't unzip facebook report archive"); } }
protected function getData() { $client = new Client(); $response = $client->get(static::DATA_SOURCE_URL)->send(); if (200 !== $response->getStatusCode()) { throw new RemoteServerProblem('Data is not available'); } return $this->convertData(json_decode($response->getBody(), true)); }
/** * Execute the query. * * @param string $url * @param array $params * * @return string|boolean */ public function execute($url, $params = array()) { $uri = rtrim(rtrim($this->site, '/') . '/' . ltrim($url, '/') . '?' . http_build_query($params), '?'); try { if ($this->deprecated) { /** @deprecated remove when PHP 5.3 support is dropped */ $result = $this->client->get($uri, array('timeout' => 10))->send()->getBody(true); } else { $result = $this->client->get($uri, array('timeout' => 10))->getBody(true); } return $this->format === 'json' ? json_decode($result) : (string) $result; } catch (\Exception $e) { if ($this->isRetry) { return false; } $this->isRetry = true; $this->site = str_replace('https://', 'http://', $this->site); return $this->execute($url, $params); } }
/** * {@inheritDoc} */ public function getContent($url) { $guzzle = new Client(); try { $content = (string) $guzzle->get($url)->send()->getBody(); } catch (\Exception $e) { $content = null; } return $content; }
/** * Execute the query. * * @param string $url * @param array $params * * @return string|boolean */ public function execute($url, $params = array()) { $uri = rtrim(rtrim($this->site, '/') . '/' . ltrim($url, '/') . '?' . http_build_query($params), '?'); try { if ($this->deprecated) { /** @deprecated remove when PHP 5.3 support is dropped */ $result = $this->client->get($uri, array('timeout' => 10))->send()->getBody(true); } else { $result = $this->client->get($uri, array('timeout' => 10))->getBody(true); } return $this->format === 'json' ? json_decode($result) : (string) $result; } catch (\Exception $e) { if ($this->isRetry) { $msg = "Error connecting to the Extension Marketplace:\n" . $e->getMessage(); throw new ExtensionsInfoServiceException($msg, $e->getCode(), $e); } $this->isRetry = true; $this->site = str_replace('https://', 'http://', $this->site); return $this->execute($url, $params); } }
/** * @param string $relation * @param array $parameters * @throws ClientErrorResponseException * @return Navigator|array|string|int|bool|float */ public function getFile($relation, array $parameters = []) { $templatedUrl = $this->getRelation($relation); $url = $this->renderUri($templatedUrl, $parameters); try { $request = $this->guzzleClient->get($url); $query = $this->guzzleClient->send($request); } catch (ClientErrorResponseException $e) { throw $e; } return (string) $query->getBody(); }
public function execute(Request $request) { $guzzle = new GuzzleClient(); if ($request->getVerb() == 'GET') { $guzzleRequest = $guzzle->get($request->getUrlWithQueryString()); } if ($request->getHeader()) { list($headerName, $headerValue) = $request->getHeader(); $guzzleRequest->setHeader($headerName, $headerValue); } $this->response = $guzzleRequest->send(); return $this->getBody(); }
protected function fetchUserDetails(AccessToken $token, $force = true) { $url = $this->urlUserDetails($token); try { $client = new GuzzleClient(); $request = $client->get($url, array('Authorization' => 'Bearer ' . $token)); $response = $request->send(); $xml_response = $response->xml(); } catch (BadResponseException $e) { $raw_response = explode("\n", $e->getResponse()); throw new IDPException(end($raw_response)); } return $xml_response; }
/** * @param array $data * Data that will be sent to the Graph * * @param string|null $path * The path of the request. Some operations like Batch don't need it. * If the $path is complete, with all query parameters, $data will be ignored * * @return \stdClass[] */ public function curl($data = array(), $path = '', $method = 'post') { $fails = 0; $this->guzzle_client->setBaseUrl('https://graph.facebook.com'); if (substr($path, 0, 26) != 'https://graph.facebook.com') { $data['access_token'] = $this->getAccessToken(); } do { if ($fails > 0) { usleep($fails * 20000 + rand(0, 1000000)); } $result = null; switch ($method) { case 'get': $path = $this->buildQuery($data, $path); $request = $this->guzzle_client->get($path); break; case 'post': foreach ($data as &$data_param) { if (is_array($data_param)) { $data_param = json_encode($data_param); } } $request = $this->guzzle_client->post($path, array(), $data); break; default: throw new \Exception('Available methods: post or get'); break; } try { $response = $request->send(); } catch (ClientErrorResponseException $e) { throw new OAuthException('Error', 0, $e); } if ($fails > self::MAX_REQUEST_ATTEMPTS) { throw new \Exception('Failed to connect to server ' . self::MAX_REQUEST_ATTEMPTS . ' times'); } $fails++; $result = json_decode($response->getBody(true), false, 512, JSON_BIGINT_AS_STRING); } while ($this->isNotValidResponse($result)); if (isset($result->paging) && isset($result->paging->next) && $result->paging->next != null) { //$next_result = $this->curl(array(), $result->paging->next, 'get'); usleep(1000); $next_result = $this->curl([], $result->paging->next, 'get'); /** @noinspection PhpUndefinedFieldInspection */ $result->data = array_merge($result->data, $next_result->data); } return $result; }
/** * @Route("/refresh") */ public function refreshAction() { $return = []; //http://overpass.osm.rambler.ru/cgi/interpreter?data=%5Bout:json%5D;node%5Bcraft_beer%3Dyes%5D%3Bout%3B $osm = new Guzzle('http://overpass-api.de/api'); $response = $osm->get('xapi?*[craft_beer=yes]')->send(); $locationsCollection = $this->getMongoCollection('CraftLocationBundle:Location'); $dm = $locationsCollection->getDocumentManager(); if ($response->getStatusCode() == 200) { $body = $response->getBody(); echo $body; $results = new \DOMDocument(); $results->loadXML($body); $resultsArray = []; $xpathDoc = new \DOMXPath($results); foreach ($xpathDoc->query('*[tag[@k="name"]]') as $location) { $id = (int) $location->getAttribute('id'); $locationDbObject = $locationsCollection->findOneBy(['osmId' => $id]); if ($locationDbObject === null) { $locationDbObject = new \Craft\LocationBundle\Document\Location(); $locationDbObject->setOsmId($id); $locationDbObject->setCreated(new Document\User(new \Craft\UserBundle\Document\User(), $_SERVER['REMOTE_ADDR'])); } $tags = []; foreach ($location->getElementsByTagName('tag') as $tag) { $tags[(string) $tag->getAttribute('k')] = (string) $tag->getAttribute('v'); } $geolocation = []; if ($location->hasAttribute('lat') && $location->hasAttribute('lon')) { $geolocation[] = ['lat' => $location->getAttribute('lat'), 'lon' => $location->getAttribute('lon')]; } else { foreach ($location->getElementsByTagName('nd') as $nd) { $nid = $nd->getAttribute('ref'); $refNode = $xpathDoc->query("//*[@id='{$nid}']")->item(0); $geolocation[] = ['lat' => $refNode->getAttribute('lat'), 'lon' => $refNode->getAttribute('lon')]; } } $locationDbObject->populateOsmTags($tags); $locationDbObject->setCoordinates($geolocation); $dm->persist($locationDbObject); } $dm->flush(); } return $return; }
/** * Execute the console command. * * @return mixed */ public function handle() { \Log::info('Fetch tweets'); $client = new Client('https://api.twitter.com/1.1/'); $auth = new Oauth(['consumer_key' => env('TWITTER_CONSUMER_KEY'), 'consumer_secret' => env('TWITTER_CONSUMER_SECRET'), 'token' => env('TWITTER_ACCESS_TOKEN'), 'token_secret' => env('TWITTER_ACCESS_TOKEN_SECRET')]); $client->addSubscriber($auth); //$response = $client->get('search/tweets.json?q=%23peukpoll&since=' . date('Y-m-d'))->send(); $response = $client->get('search/tweets.json?q=%23peukpoll')->send(); $data = $response->json()['statuses']; $tweets = array_fetch($response->json()['statuses'], 'text'); foreach ($tweets as $tweet) { $values[] = explode(' ', $tweet); } for ($i = 0; $i < count($values); $i++) { $user = User::where('username', $data[$i]['user']['screen_name'])->first(); if ($user == null) { $user = new User(); $user->username = $data[$i]['user']['screen_name']; $user->save(); } $tweet = Tweet::where('tweet', $data[$i]['id'])->first(); if ($tweet == null) { $tweet = new Tweet(); $tweet->x = $values[$i][0]; $tweet->y = $values[$i][2]; $tweet->score_x = 1; $tweet->score_y = 1; $tweet->tweet = $data[$i]['id']; $tweet->user()->associate($user); $tweet->save(); $numberOfUpcomingTweets = UpcomingTweet::all()->count(); if ($numberOfUpcomingTweets > 0) { $upcomingTweet = new UpcomingTweet(); $upcomingTweet->end = date('Y-m-d H:i:s', strtotime('+ 1 year')); $upcomingTweet->tweet()->associate($tweet); $upcomingTweet->save(); } else { $upcomingTweet = new UpcomingTweet(); $upcomingTweet->end = date('Y-m-d H:i:s', strtotime('+ 1 hour')); $upcomingTweet->tweet()->associate($tweet); $upcomingTweet->save(); } } } }
/** * Actually sends a request to the last.fm API * @param array $data The data to be submitted to the server * @param string $method Either a get or post HTTP request * @return \SimpleXMLElement The response XML */ private function sendRequest(array $data = [], $method = 'get') { if ($method === 'post') { $request = $this->client->post(''); $request->addPostFields($data); } else { $request = $this->client->get(''); $request->getQuery()->merge($data); } try { $response = $request->send(); } catch (BadResponseException $e) { return simplexml_load_string((string) $e->getResponse()->getBody()); } catch (CurlException $e) { return null; } return simplexml_load_string((string) $response->getBody()); }
/** * Append the comment counts to the given tricks. * * @param mixed $tricks * * @return mixed */ public function appendCommentCounts($tricks) { $tricks = $this->getValidTricks($tricks); $request = $this->prepareQuery($this->client->get(), $tricks); $response = $this->getResponse($request); if (is_null($response)) { foreach ($tricks as $trick) { $trick->comment_count = 0; } } else { foreach ($response as $comment) { foreach ($tricks as $trick) { if ($trick->id == $comment['identifiers'][0]) { $trick->comment_count = $comment['posts']; break; } } } } return $tricks instanceof Collection ? $tricks : $tricks[0]; }
/** * Function saying according to the status code if the injection was a success or not * @param RequestInterface $req * @param $url * @return array * @internal param SqlTarget $target */ public function goslingResponse(RequestInterface $req, $url) { $success = false; $res = $req->send(); $status_code = $res->getStatusCode(); if ($status_code == 200) { // Create a request that has a query string and an X-Foo header $request = $this->_guzzle->get($url); // Send the request and get the response $response = $request->send(); // Connection to DB $repo = $this->_em->getRepository('AppBundle:HtmlError'); $html_errors = $repo->findAll(); foreach ($html_errors as $html_error) { if (preg_match($html_error->getValue(), $response->getBody(true))) { $success = true; } } } $result = array("Success" => $success, "Status_code" => $status_code); return $result; }
public function execute($client = null) { if ($client == null) { $client = new GuzzleClient(); } $url = $this->protocol . '://' . $this->hostname . $this->path; switch ($this->verb) { case 'get': $request = $client->get($url); break; case 'post': $request = $client->post($url); break; case 'put': $request = $client->put($url); break; case 'delete': $request = $client->delete($url); break; } return new Response($request->send()); }
public function getOsmLocations(Loc\Mbr $mbr, $amenity, array $tags = []) { $amenityString = ''; if ($amenity !== null) { $amenityString = '["amenity"~"'; if (is_array($amenity)) { $amenityString .= implode('|', $amenity); } else { $amenityString .= $amenity; } $amenityString .= '"]'; } $tagString = ''; $osm = new Guzzle('http://overpass-api.de/api'); $response = $osm->get('xapi?*[craft_beer=yes]')->send(); }
/** * Performs an HTTP request. * * @param string $query * The url to fetch. * * @return string * The result. */ public function query($url) { $client = new Guzzle\Service\Client($url, array('ssl.certificate_authority' => FALSE)); $request = $client->get($url); $this->logger->AddInfo("Request " . $request->getUrl()); try { $response = $request->send(); $output = $response->getBody(TRUE); $this->logger->AddDebug("Response: " . $output); } catch (Guzzle\Http\Exception\BadResponseException $e) { $this->logger->addAlert("Something is wrong."); return FALSE; } if (!is_object($request)) { $this->logger->addAlert("Something is wrong."); return FALSE; } /* if (!$response->getHeader('Content-Type')->hasValue('application/json;charset=UTF-8')) { $this->logger->addAlert("The Content-Type header is wrong."); $this->logger->addAlert($output); return FALSE; } */ return $output; }
public function getMedias() { $url = sprintf('/v1/users/%s/media/recent/?access_token=%s', $this->_app['instagram.user_id'], $this->_app['instagram.access_token']); $response = $this->_client->get($url)->send(); return json_decode($response->getBody(true)); }
/** * Get all the users email addresses * * @param string $token * @return mixed */ protected function getVerifiedEmails($token) { $url = 'https://api.github.com/user/emails?access_token=' . $token; try { $client = new GuzzleClient(); $client->setBaseUrl($url); $request = $client->get()->send(); $response = $request->getBody(); } catch (BadResponseException $e) { // @codeCoverageIgnoreStart $raw_response = explode("\n", $e->getResponse()); d($raw_response); die; } return json_decode($response); }
public function getInfo($key, $default = null) { syslog(LOG_INFO, "getting info for key {$key} on request " . $_SERVER['REQUEST_URI']); if (empty($this->info)) { $this->info = array(); syslog(LOG_INFO, 'guzzle start for url ' . $this->url); $guzzle = new GuzzleClient($this->url); $guzzle->getEventDispatcher()->addListener('request.error', function (Event $event) { $event->stopPropagation(); }); $response = $guzzle->head()->send(); if (!$response->isSuccessful()) { $response = $guzzle->get()->send(); } if ($response->isSuccessful()) { $this->info = $response->getInfo(); } syslog(LOG_INFO, 'guzzle stop for url ' . $this->url); } return isset($this->info[$key]) ? $this->info[$key] : $default; }
/** * @param string $url * @return string */ public function getChunkByUrl($url) { $client = new Client(); $request = $client->get($url); $request->setHeader('Accept', '*/*'); $response = $this->sendRequest($request); return $response->getBody(true); }
/** * Send custom GET request to API resource * * @param string $url * @param array $data * @return array */ protected function getNextPartOfList($url, $data = array()) { $client = new Client(); $request = $client->get($url, array('Authorization' => $this->getOauthData(), 'Accept' => 'application/x-yametrika+json', 'Content-Type' => 'application/x-yametrika+json')); $response = $this->sendRequest($request)->json(); $response = array_merge_recursive($data, $response); if (isset($response['links']) && isset($response['links']['next'])) { $url = $response['links']; unset($response['rows']); unset($response['links']); $response = $this->getNextPartOfList($url, $response); } return $response; }
/** * @return string */ public function getLogin() { $client = new Client($this->getServiceUrl()); $request = $client->get('/?userinfo'); $response = $this->sendRequest($request); $result = explode(":", $response->getBody(true)); array_shift($result); return implode(':', $result); }