/** * Scraps og:title off the page content * @param string $url * @return string */ public function scrap($url) { $title = 'Unable to parse'; $this->request->setMethod(HTTP_METH_GET); $this->request->setUrl($url); try { $response = $this->request->send(); $this->crawler->addHtmlContent($response->getBody()); $subCrawler = $this->crawler->filterXPath('//head/meta[@property="og:title"]'); $meta = $subCrawler->getNode(0); if ($meta) { $title = $meta->getAttribute('content'); } } catch (Exception $e) { $title = $e->getMessage(); } return $title; }
/** * Promise handler * * @param array|PromiseInterface $promise * @param Generator $gen */ private function give($promise, Generator $gen) { if ($promise instanceof PromiseInterface) { $promise->then(function ($result) use($gen) { if ($promise = $gen->send($result)) { $this->give($promise, $gen); } }); } else { all($promise)->then(function ($results) use($gen) { if ($promise = $gen->send($results)) { $this->give($promise, $gen); } }); } $this->client->send(); }
<?php use http\Client, http\Client\Request; use http\QueryString; //$params = new QueryString; $params["foo"] = "bar"; $params["bar"] = "foo"; $request = new Request("POST", "http://www.google.com"); $request->getBody()->append($params); $request->setContentType("application/x-www-form-urlencoded"); $client = new Client(); $client->enqueue($request); $client->send(); $response = $client->getResponse($request); printf("Sent:\n%s\n\n", $response->getParentMessage()); printf("%s returned '%s'\n%s\n", $response->getTransferInfo("effective_url"), $response->getInfo(), $response->getBody()); ?> Sent: POST http://example.com HTTP/1.1 Content-Type: application/x-www-form-urlencoded Content-Length: 7 bar=foo http://example.com/ returned 'HTTP/1.1 200 OK' <!doctype html> <html> <head> <title>Example Domain</title>