如何使用CURL从其他网站获取数据到我的网站

时间:2014-04-10 02:04:47

标签: php curl

我想从这个网站获取数据:https://vrl.lta.gov.sg/lta/vrl/action/pubfunc?ID=EnquireTransferFee

这是我使用CURL的代码:

function postPage($url, $pvars, $referer, $timeout){
    if(!isset($timeout))
        $timeout=30;
    $curl = curl_init();
    $post = http_build_query($pvars);
    if(isset($referer)){
        curl_setopt ($curl, CURLOPT_REFERER, $referer);
    }
    curl_setopt ($curl, CURLOPT_URL, $url);
    curl_setopt ($curl, CURLOPT_TIMEOUT, $timeout);
    curl_setopt ($curl, CURLOPT_USERAGENT, sprintf("Mozilla/%d.0",rand(4,5)));
    curl_setopt ($curl, CURLOPT_HEADER, 0);
    curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt ($curl, CURLOPT_POST, 1);
    curl_setopt ($curl, CURLOPT_POSTFIELDS, $post);
    curl_setopt ($curl, CURLOPT_HTTPHEADER,
        array("Content-type: application/x-www-form-urlencoded"));
    $html = curl_exec ($curl);
    curl_close ($curl);
    return $html;
}
$vars = array(
    'vehicleNo'    => 'SFT4228H',
    'transferDate' => '10042014'
);

$result = postPage('test.php', $vars, 'https://vrl.lta.gov.sg/lta/vrl/action/pubfunc?ID=EnquireTransferFee', '30');

print "Result:".$result; //Show data in my website 

=>如何获取此数据

1 个答案:

答案 0 :(得分:0)

正如您在此处所看到的那样,您似乎正在使用curl来请求“test.php”。

尝试在函数调用中切换它们:

$result = postPage('https://vrl.lta.gov.sg/lta/vrl/action/pubfunc?ID=EnquireTransferFee', $vars, 'test.php', '30');