Faced the problem that Yandex does not provide its api directly via js, I want to make a php interlayer, so that I stupidly replace the contents. That is, in Js I will register site.com, and it will output the same answer to me that I would receive via api-metrika.yandex.ru, will it be possible to implement this in php? Can there be any ready for this strange task?)
|
1 answer
You will need to install the composer require fabpot/goutte :
fetcher.php
use GuzzleHttp\Client; use GuzzleHttp\Cookie\CookieJar; class Fetcher { /** * @var bool|string */ private $_site = false; /** * @var Client */ private $_parser; /** * @var CookieJar */ private $_jar; /** * @var array|string */ private $_result = []; /** * Fetcher constructor. * * @param bool $siteName */ public function __construct($siteName = false) { $this->_jar = new CookieJar(); $this->_parser = new Client([ 'cookies' => $this->_jar ]); $this->_site = $siteName; $this->_parseData(); } /** * Parsing data from specific URI * * @return string */ private function _parseData() { return $this->_result = $this->_parser->get($this->_site) ->getBody() ->getContents(); } /** * Returns result * * @return array|string */ public function getResult() { return $this->_result; } } $result = new Fetcher($_POST['site']); echo $result->getResult(); return true; index. (html | php)
<input id="site-name" placeholder="Enter site address"> <div id="result-data"></div> <script> $('input#site-name').on('keydown', function (e) { if (e.keyCode == 13) { // Enter has been pressed down $.post('/fetcher.php', 'site=' + $(this).val(), function (response) { $('div#result-data').html(response); }); } }); </script> |