Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 无法使用Symfony Dom爬虫获取元素的值_Php_Symfony_Dom_Guzzle_Domcrawler - Fatal编程技术网

Php 无法使用Symfony Dom爬虫获取元素的值

Php 无法使用Symfony Dom爬虫获取元素的值,php,symfony,dom,guzzle,domcrawler,Php,Symfony,Dom,Guzzle,Domcrawler,我正在使用guzzle POST方法获取URL。它正在工作并返回我想要的页面。但问题是,当我想在该页面的表单中获取输入元素的值时,爬虫程序什么也不返回。我不知道为什么 PHP: <?php use Symfony\Component\DomCrawler\Crawler; use Guzzle\Http\Client; $client = new Client(); $request = $client->get("https://example.com"); $response

我正在使用guzzle POST方法获取URL。它正在工作并返回我想要的页面。但问题是,当我想在该页面的表单中获取输入元素的值时,爬虫程序什么也不返回。我不知道为什么

PHP:

<?php
use Symfony\Component\DomCrawler\Crawler;
use Guzzle\Http\Client;

$client = new Client();

$request = $client->get("https://example.com");
$response = $request->send();
$getRequest = $response->getBody();
$cookie = $response->getHeader("Set-Cookie");


$request = $client->post('https://example.com/page_example.php', array(
    'Content-Type' => 'application/x-www-form-urlencoded',
    'Cookie' => $cookie
    ), array(
        'param1' => 5,
        'param2' => 10,
        'param3' => 20
    ));

$response = $request->send();
$pageHTML = $response->getBody();

//fetch orderID
$crawler = new Crawler($pageHTML);
$orderID = $crawler->filter("input[name=orderId]")->attr('value');//there is only one element with this name

echo $orderID; //returns nothing

您不必创建爬虫程序:

$crawler = $client->post('https://example.com/page_example.php', array(
'Content-Type' => 'application/x-www-form-urlencoded',
'Cookie' => $cookie
), array(
    'param1' => 5,
    'param2' => 10,
    'param3' => 20
)); 
$orderID = $crawler->filter("input[name=orderId]")->attr('value');
这假设您的帖子没有被重定向,如果被重定向,您应该在调用筛选函数之前添加:

$this->assertTrue($client->getResponse()->isRedirect());
$crawler = $client->followRedirect();

刚刚测试,它的工作。尝试获取爬虫对象,而不是创建它。(新答案)我使用
字符串数组表示法:$string[$I]
substr()
substring
函数创建了一个dom爬虫程序来实现这个目标。谢谢你的时间和努力