/** * Sends request to the URL and acquires data from the response. * * @param $url * @return mixed * @throws \Exception */ protected function get($url) { $response = \Guzzle::get($url); if ($response->getStatusCode() === 200) { return json_decode($response->getBody()); } throw new \Exception('Request was not successful: ' . $response->getStatusCode()); }
protected static function get($segments) { // // Authenticate organization to retrieve full list of members. // FIXME: without authentication, only the public list is being retrieved. // $client_id = ''; $client_secret = ''; return \Guzzle::get(self::API_URL . "{$segments}?client_id={$client_id}&client_secret={$client_secret}")->json(); }
private function _getGithub() { Guzzle\Http\StaticClient::mount(); try { $response = Guzzle::get('https://api.github.com/orgs/codezeus/repos'); if ($response->getStatusCode() == 200) { return json_decode($response->getBody()); } } catch (\Exception $e) { } return []; }
function __construct($url, $body = null) { if ($body) { $this->url = $url; $this->body = $body; } else { $response = Guzzle::get($url); $this->url = $response->getEffectiveUrl(); // important if redirected $this->body = $response->getBody(); } $this->crawler = new Crawler($this->body); }
public function fire($job, $data) { $content = Content::findOrFail($data['id']); $url = Config::get('app.iframely_host') . '/oembed'; $response = Guzzle::get($url, ['query' => ['url' => $content->url]])->json(); $content->type = $response['type']; $content->save(); if ($data['thumbnail'] && array_key_exists('thumbnail_url', $response)) { $content->setThumbnail($response['thumbnail_url']); } $content->autoThumbnail(); WS::send(json_encode(['topic' => 'content.' . $content->getKey() . '.thumbnail', 'url' => $content->getThumbnailPath(100, 75)])); $content->unset('thumbnail_loading'); $job->delete(); }
function perform() { $ps = DB::prepare('INSERT INTO listings SET scraped=FALSE, code=:code, title=:title, link=:link, date=:date, price=:price, neighborhood=:neighborhood'); array_map(function ($url) use($ps) { $code = substr($url, 30, 3); // SUPER brittle obvs $crawler = new Crawler(Guzzle::get($url)->getBody()); $crawler->filter('.row > .txt')->each(function ($node) use($ps, $code) { try { $a = $node->filter('.pl > a.hdrlnk'); $ps->execute([':code' => $code, ':title' => $a->text(), ':link' => $a->attr('href'), ':date' => strftime('%Y-%m-%d', strtotime($node->filter('.pl > .date')->text())), ':price' => ($n = $node->filter('.l2 > .price')) && $n->count() ? preg_replace('/\\D/', '', $n->text()) : null, ':neighborhood' => ($n = $node->filter('.l2 > .pnr > small')) && $n->count() ? $n->text() : null]); } catch (Exception $e) { Logger::error($e->getMessage(), $ps->errorinfo()); } }); }, ['http://newyork.craigslist.org/nfa/', 'http://newyork.craigslist.org/roo/', 'http://newyork.craigslist.org/sub/']); }
function perform() { $q = DB::query('SELECT link, neighborhood FROM listings WHERE scraped != TRUE', PDO::FETCH_ASSOC); $ps = DB::prepare('UPDATE listings SET scraped=TRUE, street=:street, description=:description, lat=:lat, lng=:lng WHERE link=:link'); /* Guzzle::sendAll(array_map(function ($listing) { return Guzzle::createRequest('GET', 'http://newyork.craigslist.org' . $listing['link']); }, iterator_to_array($q)), ['complete' => function ($event) use($ps) { try { $body = $event->getResponse()->getBody(); $crawler = new Crawler($body); $readability = new Readability($body); $street = $crawler->filter('.mapAndAttrs > .mapbox > div.mapaddress'); $ps->execute([ ':link' => parse_url($event->getRequest()->getUrl())['path'], ':lat' => null, ':lng' => null, ':street' => $street->count() ? $street->text() : null, ':description' => $readability->init() ? trim(strip_tags(tidy_parse_string($readability->getContent()->innerHTML, [], 'UTF8'))) : null ]); } catch (Exception $e) { Logger::error($e->getMessage(), $ps->errorinfo()); } }]); */ foreach ($q as $listing) { try { $body = Guzzle::get('http://newyork.craigslist.org' . $listing['link'])->getBody(); $crawler = new Crawler($body); $readability = new Readability($body); $street = $crawler->filter('.mapAndAttrs > .mapbox > div.mapaddress'); $url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . ($street->count() ? $street->text() : $listing['neighborhood']); $json = json_decode(Guzzle::get($url)->getBody(), true); $loc = isset($json['results'][0]) ? $json['results'][0]['geometry']['location'] : null; $ps->execute([':link' => $listing['link'], ':lat' => isset($loc['lat']) ? $loc['lat'] : null, ':lng' => isset($loc['lng']) ? $loc['lng'] : null, ':street' => $street->count() ? $street->text() : null, ':description' => $readability->init() ? trim(strip_tags(tidy_parse_string($readability->getContent()->innerHTML, [], 'UTF8'))) : null]); } catch (Exception $e) { Logger::error($e->getMessage(), $ps->errorinfo()); } } }
<?php use Guzzle\Http\Client; $oclcNumber = $_GET['oclcNumber']; $url = "https://worldcat.org/circ/availability/sru/service?x-registryId=" . $config['institution'] . "&query=no:ocm" . $oclcNumber; $client = new Client($guzzleOptions); $headers = array('Authorization' => 'Bearer ' . $accessToken->getValue()); // Set the guzzle headers $guzzleOptions['headers'] = $headers; // And execute the request (in this case a simple GET with no request body) try { $response = \Guzzle::get($url, $guzzleOptions); // Define the namespaces for parsing $availabilityResponse = simplexml_load_string($response->getBody(true)); $holdings = $availabilityResponse->xpath('//holdings/holding'); echo '<p>holding count' . count($holdings) . '</p>'; } catch (\Guzzle\Http\Exception\BadResponseException $error) { // Or display the error, if one occurs echo '<div class="error">'; echo $error->getResponse()->getStatusCode(); echo $error->getRequest(); echo '</div>'; } ?> <html> <head> <title>Availability Detail Screen for <?php echo $oclcNumber; ?> </title> <style type="text/css">
#!/usr/bin/php <?php $config = ['require_services' => ['sf_event_dispatcher'], 'git_urls' => ['https://github.com/yfix/guzzle.git~v3.7.4' => 'guzzle3/'], 'autoload_config' => ['guzzle3/src/Guzzle/' => 'Guzzle'], 'example' => function () { Guzzle\Http\StaticClient::mount(); $response = Guzzle::get('http://google.com'); echo strlen($response); }]; if ($return_config) { return $config; } require_once __DIR__ . '/_yf_autoloader.php'; new yf_autoloader($config);