是否可以使用curl php从此站点获取数据?
我在共享主机上使用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.
"
非常感谢任何帮助。感谢。
答案 0 :(得分: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