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

来源:互联网 发布:手机简单编曲软件 编辑:程序博客网 时间:2024/05/21 10:30

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

curl方式

[php] view plain copy 在CODE上查看代码片派生到我的代码片
  1. $ch = curl_init();  
  2. $url = 'http://apis.baidu.com/chazhao/ipsearch/ipsearch?ip=114.114.114.114';  
  3. $header = array(  
  4.     'apikey: your_baidu_appkey',  
  5. );  
  6. // 添加apikey到header  
  7. curl_setopt($ch, CURLOPT_HTTPHEADER  , $header);  
  8. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
  9. // 执行HTTP请求  
  10. curl_setopt($ch , CURLOPT_URL , $url);  
  11. $res = curl_exec($ch);  
  12.   
  13. var_dump(json_decode($res,true));  

file_get_contents方式
[php] view plain copy 在CODE上查看代码片派生到我的代码片
  1. $opt = array(    
  2.     'http' => array(     
  3.         'header' => "apikey:your_baidu_appkey"  
  4.     )    
  5. );    
  6.     
  7. $context = stream_context_create($opt);    
  8.   
  9. $url="http://apis.baidu.com/chazhao/ipsearch/ipsearch?ip=114.114.114.114";  
  10. $html=file_get_contents($url,false,$context);  
  11. echo $html
0 0
原创粉丝点击