0

Столкнулся с проблемой что яндекс не предоставляет своё api напрямую по js, хочу сделать php прослойку, чтобы тупо подменял содержимое. Т.e в Js я пропишу site.com, а он будет выводить мне тот же ответ что бы я получал через api-metrika.yandex.ru , получится ли такое реализовать на php? может есть либы готовые для этой странной задачи?)

1 ответ 1

-1

Нужно будет установить 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>
0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.