【问题标题】:curl php get data from the next pagecurl php 从下一页获取数据
【发布时间】:2017-01-22 12:21:30
【问题描述】:

是否可以使用 curl php 从本站获取数据?

MyCarInfo

我在共享主机上使用 php5。

这是我到目前为止所做的:

function httpPost($url,$params)
{
    $postData = '';
    //create name value pairs seperated by &
    foreach($params as $k => $v) 
    { 
        $postData .= $k . '='.$v.'&'; 
    }
    $postData = rtrim($postData, '&');

    $ch = curl_init();  

    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_HEADER, false); 
    curl_setopt($ch, CURLOPT_POST, count($postData));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);    

    $output=curl_exec($ch);

    curl_close($ch);
    return $output;
}

$host = "www.mycarinfo.com.my/NCDCheck/Online";

$params = array(
    "VehRegNo" => "TY4484",
    "NRIC" => "821004115453"
);

$url = "https://".$host."/"; 
$NCD = httpPost($url,$params);

var_dump($NCD);

输出如下:

string(173) "
Object moved to here.

"

任何帮助将不胜感激。谢谢。

【问题讨论】:

  • 这里可能是一个链接。PHP 响应。该链接的 href 值是多少?
  • 您可以尝试添加curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 看看是否适合您。
  • 错误:curl_setopt(): CURLOPT_FOLLOWLOCATION 在设置 open_basedir 时无法激活
  • 还有其他帮助吗? this 可能有帮助吗?

标签: php curl


【解决方案1】:

该网站要求您有一个会话 id cookie 和一个名为“ssX”的代码,必须在您提交搜索之前匹配,当这两个匹配时,您会得到一个必须遵循的 302 Found http 重定向。您没有获得会话 id cookie,也没有 ssX 代码,也没有遵循 302 重定向。修复这些并重试。

使用来自 https://github.com/divinity76/hhb_.inc.php/blob/master/hhb_.inc.php 的 hhb_curl,这是一个工作示例代码:

<?php
declare(strict_types = 1);
require_once ('hhb_.inc.php');
$hc = new hhb_curl ();
$hc->_setComfortableOptions ();
// i have a really slow internet connection right now.
$hc->setopt_array ( array (
        CURLOPT_CONNECTTIMEOUT => 10,
        CURLOPT_TIMEOUT => 20 
) );
// get a session id cookie, and the weird "ssx" value
$hc->exec ( 'https://www.mycarinfo.com.my/NCDCheck/Online' );
$html = $hc->getResponseBody ();
$matches = array ();
$rex = \preg_match ( '/ssX\s*\=\s*\\\'([^\']*)/', $html, $matches );
// hhb_var_dump($matches);die();
if ($rex !== 1) {
    throw new \RuntimeException ( 'failed to extract the ssX code!' );
}
$ssX = $matches [1];
$hc->setopt_array ( array (
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => http_build_query ( array (
                "VehRegNo" => "TY4484",
                "NRIC" => "821004115453",
                'ssX' => $ssX 
        ) ),
        CURLOPT_URL => 'https://www.mycarinfo.com.my/NCDCheck/Online' 
) );
$hc->exec ();
// hhb_var_dump ( $hc->getStdErr(),$hc->getResponseBody() );
$html = $hc->getResponseBody ();
$infoParsed = array ();
$domd = @\DOMDocument::loadHTML ( $html );
foreach ( $domd->getElementsByTagName ( "table" )->item ( 0 )->getElementsByTagName ( "tr" ) as $tr ) {
    $infoParsed [trim ( $tr->firstChild->textContent )] = trim ( $tr->firstChild->nextSibling->nextSibling->textContent );
}
hhb_var_dump ( $infoParsed );

输出:

HHB_VAR_DUMP_START
in "/home/hanshenrik/workspacephp/phptests2/test.php": on line "38": 1 variable
 hhb_var_dump($infoParsed)
argv[1] >>>$infoParsed<<<:array(7) {
  ["Vehicle Reg. No."]=>
  string(6) "TY4484"
  ["ID Number"]=>
  string(12) "821004115453"
  ["Next NCD Percentage"]=>
  string(3) "30%"
  ["Next NCD Effective Date"]=>
  string(10) "29/12/2016"
  ["Current Policy Period of Cover"]=>
  string(57) "29/12/2015
                -
                28/12/2016"
  ["Current NCD Percentage"]=>
  string(3) "25%"
  ["Current NCD Effective Date"]=>
  string(10) "29/12/2015"
}
HHB_VAR_DUMP_END

【讨论】:

  • 感谢@hanshenrik 的努力,我尝试了这段代码,但没有成功并且出现很多错误。是否有另一种更简单的方法或没有 hhb_curl?
  • 似乎用户可以一遍又一遍地使用相同的 ssX 值。例如:ImD1ocFFZcY5krAG0Y6CjEXjGasol/zQJo6LAOjv190=
  • @user3613026 最有可能是相同 cookie 会话 ID 的相同 ssX 代码。这意味着每次您获得一个新的 cookie 会话时,您可能还会获得一个新的 ssX 代码。
  • @user3613026 hhb_curl 只是一种方便,没有它你也可以做同样的事情。但是你得到了什么错误?
  • 以下是错误:- 警告:不支持在第 2 行的 /home/mysite.com/hhb_.inc.php 中声明 'strict_types' 解析错误:语法错误,意外 ':',期待 ' {' 在 /home/mysite.com/hhb_.inc.php 第 4 行
猜你喜欢
  • 1970-01-01
  • 2017-07-14
  • 1970-01-01
  • 1970-01-01
  • 2014-01-17
  • 2014-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多