首页 > 解决方案 > Foreach 循环和 cURL 请求的问题

问题描述

我是PHP编程初学者。我有这个脚本,我试图从外部网站多次获取一个字符串,每次都使用不同的“登录”数据。我正在使用PHP,cURL和. 事实是,我的代码似乎只有在我不使用构造来循环整个操作时才有效。但我不知道我还能如何重复此操作,不时更改数据。DOMXPathforeach

情况是:我刚刚登录,现在网站要求我再填写两个字段,这些字段是进入下一页所必需的,我可以在其中获取所需的字符串。代码的下一部分包含在一个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()是故障循环之后的第一个函数,“属性”也是如此。

标签: phpcurldomxpath

解决方案


推荐阅读