是否可以使用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);
答案 0 :(得分:1)
第一个:我正在使用Firebug
加载项,看看会发生什么
我们有这个链接
https://apis.pos.com.my/apigateway/as2corporate/api/v2trackntracewebapijson/v1/?id=EP024922993MY&Culture=En
在标题请求中,我们可以看到require X-User-Key
现在我们必须找到X-User-Key =>我们可以查看来源
现在我们构建源代码
<?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)
结果你会喜欢这个