调用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; 




发布了30 篇原创文章 · 获赞 30 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/guoqian1408/article/details/52506080