Php Foreach循环和cURL请求问题

Php Foreach循环和cURL请求问题,php,curl,dom,xpath,Php,Curl,Dom,Xpath,我是PHP编程的初学者。我有一个脚本,我尝试多次从外部网站获取字符串,每次都使用不同的“登录”数据。我使用的是PHP、cURL、DOM和XPath。事实上,只有当我不使用foreach构造来循环整个操作时,我的代码似乎才起作用。但我不知道如何才能重复这个操作,不时地更改数据 情况是:我刚刚登录,现在网站要求我再填写两个字段,这两个字段是进入下一页所必需的,在那里我可以获得我需要的字符串。代码的下一部分包含在if块中 // A function to automatically select th

我是
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);
以下是错误消息:

注意:正在尝试获取非对象的属性“attributes”

致命错误:未捕获错误:在null上调用成员函数getNamedItem()

错误:在null上调用成员函数getNamedItem()


函数
getNamedItem()
是故障循环之后的第一个函数,“属性”也是第一个函数。

发生错误的行是哪一行?
var\u dump($form)
data并将其发布到此处这些是发生错误的行:$form=$xpath->query(//form[@name='fm scelta\u tipo\u incontainico'])->item(0); $action=$form->attributes->getNamedItem('action')->nodeValue;这是“var_dump($form)”:object(domeElement)[10]public“tagName”=>string“form”(长度=4)public“schemaTypeInfo”=>null public“nodeName”=>string“form”(长度=4)public“nodeValue”=>string(太长,无法在此处写入!)public“nodeType”=>int 1 public“parentNode”=>string(省略对象值);(长度=22)…公共'childNodes'=>string'(省略对象值)'(长度=22)公共'firstChild'=>string'(省略对象值)'(长度=22)公共'lastChild'=>string'(省略对象值)'(长度=22)公共'nextSibling'=>string'(省略对象值)'(length=22)public'attributes'=>string'(object value省略)'(length=22)public'ownerDocument'=>string'(object value省略)'(length=22)public'namespaceURI'=>null public'prefix'=>string'(length=0)…public'localName'=>string'form'(length=4)public'baseURI'=>null public'textContent'=>string(与“nodeValue”关联的相同)