alwex/php-casperjs

Set Headers [here is my Code] and a little Bug

Closed this issue · 3 comments

Hi, what about a Function to set the Headers? So you can use:

 $this->client->setHeaders([
                    'Accept'          => $_SERVER['HTTP_ACCEPT'],
                    'Accept-Language' => $_SERVER['HTTP_ACCEPT_LANGUAGE'],
                    'Cache-Control'   => 'max-age=0, must-revalidate',
                    'Connection'      => 'Keep-Alive',
                    'User-Agent'      => $_SERVER['HTTP_USER_AGENT'],
            ]);

The Function will be:

        public function setHeaders(array $headers)
        {
            $headersScript = "
casper.page.customHeaders = {
";
            if (!empty($headers)) {
                $headerLines = [];
                foreach ($headers as $header => $value) {
                    // Current version of casperjs will not decode gzipped output
                    if ($header == 'Accept-Encoding') {
                        continue;
                    }
                    $headerLine = "    '{$header}': '";
                    $headerLine .= (is_array($value)) ? implode(',', $value) : $value;
                    $headerLine .= "'";
                    $headerLines[] = $headerLine;
                }
                $headersScript .= implode(",\n", $headerLines)."\n";
            }
            $headersScript .= "};";
            $this->_script .= $headersScript;

            return $this;
        }

And my Issue is the following:

$this->client->fillForm('form#tsf', ['#lst-ib' => 'Test'], false);

This returns nothing. After this, the HTML is empty.
Here is the complete code:

$this->client = new Casper();
$this->client->start('http://www.google.com');
$this->client->fillForm('form#tsf', ['#lst-ib' => 'Test'], false);
$this->client->run();
echo $this->client->getHTML();

alwex commented

Hi, for the setHeader function, can you create a pull request?
I will look into the bug.
Thank for the info

alwex commented

I don't think it is a bug, it is more like you are trying to fill an input that does not exists in the context of your virtual browser
[info] [remote] attempting to fetch form element from selector: 'form#tsf'
and then the script stop because the element does not exists.

try this line instead:
$client->fillForm('form[action="/search"]', ['q' => 'Test'], false);

Ok, thats right. Now it works. Thank you.

I have submitted the Request.