简体   繁体   English

curl php 从下一页获取数据

[英]curl php get data from the next page

Is it possible to get data from this site using curl php?是否可以使用 curl php 从该站点获取数据?

MyCarInfo我的车信息

I'm using php5 on shared host.我在共享主机上使用 php5。

This is what i've so far:这是我到目前为止:

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);

The output come out like this:输出是这样的:

string(173) "
Object moved to here.

"

Any help would be much appreciated.任何帮助将非常感激。 Thanks.谢谢。

the website requires that you have a session id cookie and a code called "ssX" that must match before you submit a search, and when those 2 do match, you get a 302 Found http redirect that you must follow.. you dont obtain a session id cookie, nor an ssX code, nor do you follow 302 redirects.该网站要求您有一个会话 id cookie 和一个名为“ssX”的代码,在您提交搜索之前必须匹配,当这两个匹配时,您将获得302 Found http 重定向,您必须遵循该重定向。您没有获得会话 id cookie,也不是 ssX 代码,也不遵循 302 重定向。 fix those and try again.修复这些并重试。

using hhb_curl from https://github.com/divinity76/hhb_.inc.php/blob/master/hhb_.inc.php , here's a working example code:使用来自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 );

output:输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM