php curl post request并获得结果响应

时间:2016-07-17 19:56:00

标签: php curl

是否可以使用curl获取此站点示例的结果(参见下图):http://poslaju.com.my/track-trace/#trackingIds=EP024922993MY

这是我到目前为止的代码,不起作用:

/*$params = array(
"trackingIds" => "EP024922993MY"
);*/

$url = rawurlencode("http://poslaju.com.my/track-trace/#trackingIds=EP024922993MY");

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_POST, 1);
//curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$reponse = curl_getinfo($ch);

curl_close($ch);

var_dump($result);
var_dump($reponse);

enter image description here

1 个答案:

答案 0 :(得分:1)

第一个:我正在使用Firebug加载项,看看会发生什么

enter image description here 这里有一个ajax从其他网站获取内容

我们有这个链接

https://apis.pos.com.my/apigateway/as2corporate/api/v2trackntracewebapijson/v1/?id=EP024922993MY&Culture=En

在标题请求中,我们可以看到require X-User-Key

enter image description here

现在我们必须找到X-User-Key =>我们可以查看来源

enter image description here

现在我们构建源代码

<?php
function _curl($url,$post="",$usecookie = false,$_sock = false,$timeout = false,$x_user_key = false) {  
    $ch = curl_init();
    if($post) {
        curl_setopt($ch, CURLOPT_POST ,1);
        curl_setopt ($ch, CURLOPT_POSTFIELDS, $post);
    }
    if($timeout){
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,$timeout);
    }
    if($_sock){
            curl_setopt($ch, CURLOPT_PROXY, $_sock);
            curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
    }
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'X-Apple-Tz: 0',
    'X-Apple-Store-Front: 143444,12'
    ));
    if($x_user_key){
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'X-User-Key: '.$x_user_key,
        'Referer: http://poslaju.com.my/track-trace/',
        'Origin: http://poslaju.com.my'
        ));
    }
    if ($usecookie) { 
        curl_setopt($ch, CURLOPT_COOKIEJAR, $usecookie); 
        curl_setopt($ch, CURLOPT_COOKIEFILE, $usecookie);    
    }
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
    $result=curl_exec ($ch); 
    curl_close ($ch); 
    return $result; 
}
function getStr($string,$start,$end){
    $str = explode($start,$string,2);
    $str = explode($end,$str[1],2);
    return $str[0];
}
$url = 'http://poslaju.com.my/track-trace/';
$result_curl = _curl($url,'','','','','');
$x_user_key = getStr($result_curl,'{ "X-User-Key": "','" }');

$id_track = 'EP024922993MY';
$url = 'https://apis.pos.com.my/apigateway/as2corporate/api/v2trackntracewebapijson/v1/?id='.$id_track.'&Culture=En';
$result_curl = _curl($url,'','','','',$x_user_key);
echo $result_curl;
?>

改变id的想法

您可以获得json内容

使用print_r(json_decode($content_you_got)

结果你会喜欢这个

enter image description here