How to send Custom Headers using PHP Gouttee

守給你的承諾、 提交于 2019-12-04 19:28:23

Can you try to check result of Goutte

$status_code = $client->getResponse()->getStatus();
echo $status_code;

This is source code I had success with Guzzle In index.php

<?php
    ini_set('display_errors', 1);
?>
<html>
<head><meta charset="utf-8" /></head>
<?php
    $begin = microtime(true);
    require 'vendor/autoload.php';
    require 'helpers/helper.php';
    $client = new GuzzleHttp\Client([
        'base_uri' => 'http://www.yellowpages.com.au',
        'cookies' => true,
        'headers' =>  [
            'Accept'          => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
            'Accept-Encoding' => 'zip, deflate, sdch', 
            'Accept-Language' => 'en-US,en;q=0.8', 
            'Cache-Control'   => 'max-age=0',
            'User-Agent'      => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0'
        ]
    ]);
    $helper = new Helper($client);
    $mostViewed = $helper->getPageTest();
?>
<html>

In helper.php file

<?php
use GuzzleHttp\ClientInterface;
use Symfony\Component\DomCrawler\Crawler;
class Helper{
    protected $client;
    protected $totalPages;
    public function __construct(ClientInterface $client){
        $this->client       = $client;
        $this->totalPages   = 3;
    }
    public function query()
    {
        $queries = array(
            'clue'  => 'Builders',
            'locationClue'  => 'Sydney%2C+2000',
            'mappable' => 'true',
            'selectedViewMode' => 'list'
        );
        // print_r($queries);
        return $this->client->get('search/listings', array('query' => $queries));
    }
    public function getPageTest()
    {
        $responses = $this->query();
        $html = $responses->getBody()->getContents();
        echo $html;
        exit();
    }
}
?>

And result I got

Hope this helpful!!!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!