Exemple #1
0
/**
 * Created by PhpStorm.
 * User: WhiteBlue
 * Date: 15/7/8
 * Time: 下午10:11
 */
function fetch()
{
    $client = new Client();
    $request = new Request('GET', 'http://api.bilibili.cn/index');
    $response = $client->send($request, ['timeout' => 2]);
    $json = json_decode($response->getBody());
    $count = 0;
    foreach ($json as $type => $value) {
        $sort = Sort::where('type', '=', $type)->first();
        if ($sort != null) {
            foreach ($value as $id => $content) {
                if (is_object($content)) {
                    if (Save::where('aid', '=', $content->aid)->first() == null) {
                        $save = new Save();
                        $save->aid = $content->aid;
                        $save->title = $content->title;
                        if (strlen($content->description) > 70) {
                            $save->content = mb_substr($content->description, 0, 70, 'utf-8') . '....';
                        } else {
                            $save->content = $content->description;
                        }
                        $save->href = 'http://www.bilibili.com/video/AV' . $content->aid;
                        $save->img = $content->pic;
                        $sort->saves()->save($save);
                        $count++;
                        $sort->update = date('Y:m:d');
                        $sort->save();
                    }
                }
            }
        }
    }
    event(new UpdateEvent());
    return $count;
}
 protected function getClient()
 {
     $description = new ServiceDescription(array('operations' => array('test' => array('httpMethod' => 'PUT', 'parameters' => array('ContentMD5' => array(), 'Body' => array('location' => 'body'))))));
     $client = new Client();
     $client->setDescription($description);
     return $client;
 }
 public function setUp()
 {
     $this->commandFactory = $this->getMock(self::CLASS_FACTORY, array('factory'));
     $guzzleClient = new Client();
     $guzzleClient->setCommandFactory($this->commandFactory);
     $this->client = new JanusClient($guzzleClient);
 }
Exemple #4
0
 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));
 }
 protected function getClient()
 {
     $service = ServiceDescription::factory(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'TestData' . DIRECTORY_SEPARATOR . 'test_service.xml');
     $client = new Client('http://www.google.com/');
     $client->setDescription($service);
     return $client;
 }
 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;
 }
Exemple #7
0
 function __construct()
 {
     $client = new Client('https://api.twitter.com/1.1/');
     $oauth = new OauthPlugin(['consumer_key' => 'NkGX5YSyTPPz2ONSA6nnTMjEv', 'consumer_secret' => 'stqZsBkUj6qOu1Jokv1vulwToqSd7ce8XqZEV17s6DVT1tqDoc', 'token' => '110823516-PyPFyDZwRZPDkZBdv3MkQfod8N1k6z6IWdYBocBE', 'token_secret' => 'mjrwp48ujjZ90ljLx3p8EVXAF8FrHPgL878UsoHNo51xT']);
     $client->addSubscriber($oauth);
     $this->client = $client;
 }
 /**
  * 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");
     }
 }
Exemple #9
0
 /**
  * Выполняет запрос к 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;
 }
 private function postJSON($url, $headers, $payload)
 {
     $client = new Client();
     $request = $client->createRequest('POST', $url, ["Content-Type" => "application/json;charset=UTF-8", "Accept" => "application/json"] + $headers, $payload);
     $response = $request->send();
     $json = $response->getBody(true);
     return json_decode($json);
 }
 /**
  * Initializes the BeHat context for every scenario
  *
  * @param array $parameters Context parameters defined in behat.yml (default.context.parameters...)
  */
 public function __construct(array $parameters)
 {
     $this->client = new \Guzzle\Service\Client();
     $this->client->setDefaultOption('exceptions', false);
     // disable exceptions: we want to test error responses
     self::$parameters = $parameters;
     $this->restObj = new \stdClass();
 }
 /**
  * Initializes initializer
  *
  * @param Client $client
  * @param array  $parameters
  */
 public function __construct(Client $client, array $parameters)
 {
     $this->client = $client;
     $this->parameters = $parameters;
     if (!empty($parameters['service_descriptions'])) {
         $this->client->setDescription(ServiceDescription::factory($parameters['service_descriptions']));
     }
 }
 /**
  * Send a POST request
  * 
  * @param string $uri
  * @param array $post_data
  * @param array $headers
  * @return \Guzzle\Http\Message\Response
  */
 public function post($uri, $post_data = array(), $headers = array())
 {
     // add the authorization header
     $headers['Authorization'] = "Bearer {$this->_generateBearerToken()}";
     $headers['X-ApplicationId'] = $this->appId;
     $response = $this->client->post($uri, $headers, $post_data)->setAuth('developers.chromedia', 'cfe-developers', 'Bearer')->send();
     return $response;
 }
Exemple #14
0
 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));
 }
 public function register()
 {
     $this->app->bind('yelp', function () {
         $client = new Client('http://api.yelp.com/v2/');
         $auth = new OauthPlugin(['consumer_key' => Config::get('yelp.consumer_key'), 'consumer_secret' => Config::get('yelp.consumer_secret'), 'token' => Config::get('yelp.token'), 'token_secret' => Config::get('yelp.token_secret')]);
         $client->addSubscriber($auth);
         return new YelpAPI($client);
     });
 }
Exemple #16
0
 /**
  * Tests a simple model
  * @depends testClientConstruct
  * @depends testFindPetsByStatus
  */
 public function testGetPetById(Client $client, $petId)
 {
     $petId = 3;
     // <- bug in petstore definition disallows pidId>100
     $pet = $client->getPetById(compact('petId'));
     $this->assertInstanceOf('\\Guzzle\\Service\\Resource\\Model', $pet);
     $this->assertEquals($petId, $pet['id']);
     $this->assertInternalType('string', $pet['name']);
 }
 public function testAllowsForJsonBasedArrayParamsFunctionalTest()
 {
     $description = new ServiceDescription(array('operations' => array('test' => new Operation(array('httpMethod' => 'PUT', 'parameters' => array('data' => array('required' => true, 'filters' => 'json_encode', 'location' => 'body')))))));
     $client = new Client();
     $client->setDescription($description);
     $command = $client->getCommand('test', array('data' => array('foo' => 'bar')));
     $request = $command->prepare();
     $this->assertEquals('{"foo":"bar"}', (string) $request->getBody());
 }
 /**
  * Register the service provider.
  *
  * @return void
  */
 public function register()
 {
     $this->app->bind('twitter', function () {
         $client = new Client('https://api.twitter.com/1.1');
         $auth = new OauthPlugin(['consumer_key' => Config::get('twitter.consumer_key'), 'consumer_secret' => Config::get('twitter.consumer_secret'), 'token' => Config::get('twitter.token'), 'token_secret' => Config::get('twitter.token_secret')]);
         $client->addSubscriber($auth);
         return new TwitterAPI($client);
     });
 }
 public function createEchaleGasClient()
 {
     $client = new Client();
     $client->setDescription(ServiceDescription::factory('config/services/echalegas.json'));
     $logger = new Logger('debug');
     $logger->pushHandler(new StreamHandler('logs/debug.log'));
     $logPlugin = new LogPlugin(new MonologLogAdapter($logger), MessageFormatter::DEBUG_FORMAT);
     $client->addSubscriber($logPlugin);
     return $client;
 }
 public function testAllowsForJsonBasedArrayParamsFunctionalTest()
 {
     $service = array('test' => new ApiCommand(array('method' => 'PUT', 'params' => array('data' => array('required' => true, 'type' => 'type:array', 'filters' => 'json_encode', 'location' => 'body')))));
     $description = new ServiceDescription($service);
     $client = new Client();
     $client->setDescription($description);
     $command = $client->getCommand('test', array('data' => array('foo' => 'bar')));
     $request = $command->prepare();
     $this->assertEquals(json_encode(array('foo' => 'bar')), (string) $request->getBody());
 }
 /**
  * @covers Geocoder\HttpAdapter\GuzzleHttpAdapter::__construct
  * @covers Geocoder\HttpAdapter\GuzzleHttpAdapter::getContent
  */
 public function testRetrievesResponse()
 {
     $historyPlugin = new HistoryPlugin();
     $mockPlugin = new MockPlugin(array(new Response(200, null, 'body')));
     $client = new Client();
     $client->getEventDispatcher()->addSubscriber($mockPlugin);
     $client->getEventDispatcher()->addSubscriber($historyPlugin);
     $adapter = new GuzzleHttpAdapter($client);
     $this->assertEquals('body', $adapter->getContent('http://test.com/'));
     $this->assertEquals('http://test.com/', $historyPlugin->getLastRequest()->getUrl());
 }
 public function testValidatesAdditionalParameters()
 {
     $description = ServiceDescription::factory(array('operations' => array('foo' => array('httpMethod' => 'PUT', 'parameters' => array('bar' => array('location' => 'header')), 'additionalParameters' => array('location' => 'json')))));
     $client = new Client();
     $client->setDescription($description);
     $command = $client->getCommand('foo');
     $command['bar'] = 'test';
     $command['hello'] = 'abc';
     $request = $command->prepare();
     $this->assertEquals('test', (string) $request->getHeader('bar'));
     $this->assertEquals('{"hello":"abc"}', (string) $request->getBody());
 }
    /**
     * {@inheritDoc}
     */
    public function getContent($url)
    {
        $guzzle = new Client();

        try {
            $content = (string) $guzzle->get($url)->send()->getBody();
        } catch (\Exception $e) {
            $content = null;
        }

        return $content;
    }
 protected function getMockedClient(Response $response)
 {
     $operation = new Operation(array('httpMethod' => 'GET', 'name' => 'Mock'));
     $service = new ServiceDescription();
     $service->addOperation($operation);
     $plugin = new MockPlugin();
     $plugin->addResponse($response);
     $client = new Client();
     $client->setDescription($service);
     $client->addSubscriber($plugin);
     return $client;
 }
 public function testVisitsLocationWithMultipleFiles()
 {
     $description = ServiceDescription::factory(array('operations' => array('DoPost' => array('httpMethod' => 'POST', 'parameters' => array('foo' => array('location' => 'postFile', 'type' => array('string', 'array')))))));
     $this->getServer()->flush();
     $this->getServer()->enqueue(array("HTTP/1.1 200 OK\r\nContent-Length:0\r\n\r\n"));
     $client = new Client($this->getServer()->getUrl());
     $client->setDescription($description);
     $command = $client->getCommand('DoPost', array('foo' => array(__FILE__, __FILE__)));
     $command->execute();
     $received = $this->getServer()->getReceivedRequests();
     $this->assertContains('name="foo[0]";', $received[0]);
     $this->assertContains('name="foo[1]";', $received[0]);
 }
Exemple #26
0
 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();
 }
 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;
 }
Exemple #28
0
 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;
 }
 public function testUsesProcessedModelsWhenEnabled()
 {
     $d = ServiceDescription::factory(array('operations' => array('foobar' => array('name' => 'foobar', 'httpMethod' => 'PUT', 'responseClass' => 'foo', 'responseType' => 'model', 'class' => 'Aws\\Common\\Command\\JsonCommand', 'parameters' => array('test' => array('location' => 'query')))), 'models' => array('foo' => array('type' => 'object', 'properties' => array('test' => array('type' => 'string', 'location' => 'json', 'filters' => array('strtoupper')))))));
     $response = new Response(200, array('Content-Type' => 'application/json'), '{"test":"bar"}');
     $client = new Client('http://localhost:1245');
     $client->setDescription($d);
     $this->setMockResponse($client, array($response));
     $command = $client->getCommand('foobar');
     $this->assertEquals(array('test' => 'bar'), $command->execute()->toArray());
     $this->setMockResponse($client, array($response));
     $command = $client->getCommand('foobar');
     $command->set('command.model_processing', true);
     $this->assertEquals(array('test' => 'BAR'), $command->execute()->toArray());
 }
 /**
  * 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);
     }
 }