调用http://apistore.baidu.com网站的接口

调用http://apistore.baidu.com网站的接口

curl方式

$ch = curl_init();
$url = 'http://apis.baidu.com/chazhao/ipsearch/ipsearch?ip=114.114.114.114';
$header = array(
    'apikey: your_baidu_appkey',
);
// 添加apikey到header
curl_setopt($ch, CURLOPT_HTTPHEADER  , $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// 执行HTTP请求
curl_setopt($ch , CURLOPT_URL , $url);
$res = curl_exec($ch);

var_dump(json_decode($res,true));

file_get_contents方式

$opt = array(  
    'http' => array(   
        'header' => "apikey:your_baidu_appkey"
    )  
);  
  
$context = stream_context_create($opt);  

$url="http://apis.baidu.com/chazhao/ipsearch/ipsearch?ip=114.114.114.114";
$html=file_get_contents($url,false,$context);
echo $html;


你可能感兴趣的:(调用http://apistore.baidu.com网站的接口)