首页  >  文章  >  后端开发  >  api怎么在php中使用

api怎么在php中使用

王林
王林原创
2019-10-16 11:46:452606浏览

api怎么在php中使用

1、用file_get_contents()函数

$params = array('key' => '8d284859d04cfeeea6b0771f754adb49',
   'location' => $_COOKIE["lng"].",".$_COOKIE["lat"]);

$url3 = "http://restapi.amap.com/v3/geocode/regeo";

if (strripos('?', $url3)) {
 $url4 = $url3 . http_build_query($params);
} else {
 $url4 = $url3 . '?' . http_build_query($params);
}

$ret2 = json_decode(file_get_contents($url4));

var_dump($ret);

2、用php的curl拓展

$url2 = 'http://www.sojson.com/open/api/weather/json.shtml';    
$params = array('city' => '长沙');   
if (strripos('?', $url2)) {
    $url = $url2 . http_build_query($params);
     }
else {
    $url = $url2 . '?' . http_build_query($params);
     }
     $data = 'city=长沙';
     $curlobj = curl_init();   
     curl_setopt($curlobj, CURLOPT_URL, $url); 
     curl_setopt($curlobj, CURLOPT_HEADER, 0);
     curl_exec($curlobj);

推荐教程:PHP视频教程

以上是api怎么在php中使用的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn