Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.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 解析页面以获取输入,然后提交包含此输入值的帖子_Php_Curl - Fatal编程技术网

Php 解析页面以获取输入,然后提交包含此输入值的帖子

Php 解析页面以获取输入,然后提交包含此输入值的帖子,php,curl,Php,Curl,我想解析页面,其中是表单,该表单包含令牌输入 需要获取此令牌值并使用我的输入发送 这是他们添加令牌输入之前我使用的代码 $username=@$\u POST['user']; $password=@$_POST['password']; $to=@$_POST['to']; $text=@$_POST['text']; $loginUrl=''; $sendUrl=''; $ch=curl_init(); curl_setopt($ch,CURLOPT_URL,$loginUrl); curl

我想解析页面,其中是表单,该表单包含令牌输入 需要获取此令牌值并使用我的输入发送

这是他们添加令牌输入之前我使用的代码

$username=@$\u POST['user'];
$password=@$_POST['password'];
$to=@$_POST['to'];
$text=@$_POST['text'];
$loginUrl='';
$sendUrl='';
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$loginUrl);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0(X11;Linux x86_64)AppleWebKit/537.36(KHTML,像Gecko)Ubuntu Chromium/32.0.1700.107 Chrome/32.0.1700.107 Safari/537.36');
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,“user=$username&password=$password”);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_COOKIESESSION,true);
curl_setopt($ch,CURLOPT_COOKIEJAR,'cookie name');
curl_setopt($ch,CURLOPT_COOKIEFILE,'cookie.txt');
$answer=curl_exec($ch);
if(旋度误差($ch)){
回波旋度错误($ch)。“/n”;
}
//发送
curl_setopt($ch,CURLOPT_URL,$sendUrl);
curl_setopt($ch,CURLOPT_POSTFIELDS,“recipients=$to&message_body=$text”);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_COOKIESESSION,true);
curl_setopt($ch,CURLOPT_COOKIEJAR,'cookie name send');
curl_setopt($ch,CURLOPT_COOKIEFILE,'cookie send.txt');
$answer=curl_exec($ch);
if(旋度误差($ch)){
回波旋度错误($ch)。“/n”;
}
回音$答案;
这是我想要解析的页面


不,没有此令牌我无法发布


我所需要做的就是首先解析这个页面以获取令牌,同时用这个令牌发送帖子

以下是我想尝试的:

<?php
$page = file_get_contents("https://wherever-the-form-is.com");
$dom = new DOMDocument();
$dom->loadHTML( $page );

// Get a list of all inputs
$inputs = $dom->getElementsByTagName( 'input' );
$total = $inputs->length;
$token = false;

// Loop through inputs looking for one with the right name
for( $i = 0; $i < $total; $i++ ) {
    if ( $inputs->item($i)->getAttribute('name') == 'csrf_token' ) {
        // When you find the right name, record the value and break out of the loop
        $token = $inputs->item($i)->getAttribute('value');
        break;
    }
}

if ( $token ) {
    // Your code here
}

Stevish是正确的,您应该使用DOMDocument,但我建议使用稍微不同的方法:循环表单的所有输入子项,例如

$domd=@DOMDocument::loadHTML($answer);
$xp=new DOMXPath($domd);
$inputs=array();
foreach($xp->query("//form[@name='user_action']//input") as $input){
    $inputs[$input->getAttribute("name")]=$input->getAttribute("value");
}
那应该留给你

$inputs=array (
  'csrf_token' => '7e71ea58eaaa55986b0fdc71b2d44c92',
  'user' => '',
  'password' => '',
  '' => 'á¨áá¡ááá',
)
。。另外,您在这里编码$to或$message,如果$message包含
blabla&to=morebala
,您认为会发生什么?它将覆盖您以前的$to,并使您的$to变量不相关,您需要对这些垃圾进行url编码,所以

curl_setopt($ch, CURLOPT_POSTFIELDS, "recipients=".urlencode($to)."&message_body=".urlencode($text));
或者更好的方法是使用http\u build\u query

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(
    array(
        "recipients" => $to,
        "message_body" => $text
    )
));
。。。甚至更好

$domd=@DOMDocument::loadHTML($answer);
$xp=new DOMXPath($domd);
$inputs=array();
foreach($xp->query("//form[@name='user_action']//input") as $input){
    $inputs[$input->getAttribute("name")]=$input->getAttribute("value");
}
$inputs["recipients"]=$to;
$inputs["message_body"]=$text;
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($inputs));

显然,还需要进行更多的错误处理。比如,如果
$page
是空的或者DOMDocument无法解析它呢?都德,你应该学习DOMXPath:)所有这些都可以被
$token=(新的DOMXPath($dom))->查询(//input[@name='csrf_-token']->item(0)->getAttribute(“value”)