php - Foreach 循环和 cURL 请求的问题
问题描述
我是PHP
编程初学者。我有这个脚本,我试图从外部网站多次获取一个字符串,每次都使用不同的“登录”数据。我正在使用PHP
,cURL
和. 事实是,我的代码似乎只有在我不使用构造来循环整个操作时才有效。但我不知道我还能如何重复此操作,不时更改数据。DOM
XPath
foreach
情况是:我刚刚登录,现在网站要求我再填写两个字段,这些字段是进入下一页所必需的,我可以在其中获取所需的字符串。代码的下一部分包含在一个if
块中。
// A function to automatically select the form fields:
function form_fields($xpath, $query) {
$inputs = $xpath->query($query);
$fields = array();
foreach ($inputs as $input) {
$key = $input->attributes->getNamedItem('name')->nodeValue;
$type = $input->nodeName;
$value = $input->attributes->getNamedItem('value')->nodeValue;
$fields[$key] = $value;
}
return $fields;
}
// Executing the XPath queries to fill the fields:
$opzutenza = 'incarichi';
$action = $xpath->query("//form[@name='fm_$opzutenza']")->item(0)->attributes->getNamedItem('action')->nodeValue;
curl_setopt($ch, CURLOPT_URL, $action);
$fields = form_fields($xpath, "//form[@name='fm_$opzutenza']/input");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
$html = curl_exec($ch);
$dom = new DomDocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
// The strings that I need to get depend on each value contained in this select element:
$options = $xpath->query("//select[@name='sceltaincarico']/option");
$partiteiva = array();
foreach($options as $option){
$partiteiva[] = $option->nodeValue;
unset($partiteiva[0]);
}
} // -----------> END OF 'IF' BLOCK
$queriesNA = array();
foreach ($partiteiva as $piv) {
$queryNA = ".//select[@name='sceltaincarico']/option[text()='$piv']";
$queriesNA[] = $queryNA;
}
// And this is the problematic loop:
foreach($queriesNA as $querypiv){
$form = $xpath->query("//form[@name='fm_scelta_tipo_incarico']")->item(0);
$action = $form->attributes->getNamedItem('action')->nodeValue;
@$option = $xpath->query($querypiv, $form);
curl_setopt($ch, CURLOPT_URL, $action);
$fields = [
'sceltaincarico' => $option->item(0)->attributes->getNamedItem('value')->nodeValue,
'tipoincaricante' => 'incDiretto'
];
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields)); // ----> Filling the last field
curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, 'https://website.com/dp/api');
curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, 'https://website.com/cons/cons-services/sc/tokenB2BCookie/get');
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
$http = curl_exec($ch);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_NOBODY, false);
function parse_headers($http) {
$headers = explode("\r\n", $http);
$hdrs = array();
foreach($headers as $h) {
@list($k, $v) = explode(':', $h);
$hdrs[trim($k)] = trim($v);
}
return $hdrs;
}
$hdrs = parse_headers($http);
$tokens = array(
"x-token: ".$hdrs['x-token'],
"x-b2bcookie: ".$hdrs['x-b2bcookie']
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $tokens);
curl_setopt($ch, CURLOPT_URL, "https://website.com/cons/cons-services/rs/disclaimer/accetta"); // Accepting the disclaimer...
curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, "https://website.com/portale/web/guest/home");
$html = curl_exec($ch); // Finally got to the page that I need
$dom = new DomDocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
// Selecting the string:
$string = $xpath->query("//div[@class='informativa']/strong[2]");
$nomeazienda = array();
foreach ($string as $str) {
$nomeazienda[] = $str->childNodes->item(0)->nodeValue;
}
// Going back to the initial page so the loop can start again from the beginning:
$piva_page = 'https://website.com/portale/scelta-utenza-lavoro?....';
curl_setopt($ch, CURLOPT_URL, $piva_page);
$html = curl_exec($ch);
$dom = new DomDocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
}
curl_close($ch);
这些是错误消息:
注意:试图获取非对象的属性“属性”......
致命错误:未捕获的错误:在 null 上调用成员函数 getNamedItem()...
错误:在 null 上调用成员函数 getNamedItem()...
该函数getNamedItem()
是故障循环之后的第一个函数,“属性”也是如此。
解决方案
推荐阅读
- javascript - 当它在画布中连接时,如何在 4 行之间填充颜色?
- markdown - Markdown 格式的 Nikola 目录
- gis - 无法使用 react-map 和 react-map-draw 中的 queryRenderedFeatures 在边界框中获取图层属性
- r - 基于函数外的其他变量创建变量
- java - 如何将新应用程序添加到 ADFS 2.0
- javascript - 如果多次单击 setInterval() 按钮,则不会触发 clearInterval()
- python - 在 Ubuntu 和 Windows 中使用相同的代码读取 ros bag 文件会产生不同的格式
- python - 计算火炬张量的协方差(二维特征图)
- jenkins - 如何在詹金斯中运行多个执行窗口批处理
- javascript - 我如何确保当我单击一个图标时,会出现另一个带有 React.js 的页面