权声明:本文为博主原创文章,未经博主允许不得转载。
file_get_contents方式
调用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;