本文目录一览:
php中如何调用接口以及编写接口代码详解
可以用curl获取借楼的信息。
所谓接口,就是提供一个url,只要你满足它要求的参数,就能得到你要的数据。比如你拿到一个接口,带上所需的参数,复制到地址栏同样能得到。不过最好用程序得到。file_get_contents也可以用,不过有局限性。所以我建议用curl。给你一个函数,挺好用的。
function request($url,$https=true,$method='GET',$data=null){
$ch = curl_init();//初始化,得到资源
curl_setopt($ch, CURLOPT_URL,$url); //请求数据的路径
curl_setopt($ch, CURLOPT_HEADER,false);//是否输出头
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //不直接输出结果
//curl_setopt ($ch, CURLOPT_SAFE_UPLOAD, 0);//兼容php之后的版本
if($https){
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //是否验证主机
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //是否进行证书验证
}
if($method=='POST'){
curl_setopt($ch, CURLOPT_POST, true); //POST传输
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //传输数据
}
$content_json = curl_exec($ch);
if ($content_json === false) {
return "网络请求出错: " . curl_error($ch);
}
curl_close($ch);
return $content_json;
}
用php curl请求高德地图数据不全怎么办
因为,PHP CURL库默认1024字节的长度不等待数据的返回,所以你那段代码需增加一项配置:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
给你一个更全面的封装方法:
function req_curl($url, $status = null, $options = array())
{
$res = '';
$options = array_merge(array(
'follow_local' = true,
'timeout' = 30,
'max_redirects' = 4,
'binary_transfer' = false,
'include_header' = false,
'no_body' = false,
'cookie_location' = dirname(__FILE__) . '/cookie',
'useragent' = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1',
'post' = array() ,
'referer' = null,
'ssl_verifypeer' = 0,
'ssl_verifyhost' = 0,
'headers' = array(
'Expect:'
) ,
'auth_name' = '',
'auth_pass' = '',
'session' = false
) , $options);
$options['url'] = $url;
$s = curl_init();
if (!$s) return false;
curl_setopt($s, CURLOPT_URL, $options['url']);
curl_setopt($s, CURLOPT_HTTPHEADER, $options['headers']);
curl_setopt($s, CURLOPT_SSL_VERIFYPEER, $options['ssl_verifypeer']);
curl_setopt($s, CURLOPT_SSL_VERIFYHOST, $options['ssl_verifyhost']);
curl_setopt($s, CURLOPT_TIMEOUT, $options['timeout']);
curl_setopt($s, CURLOPT_MAXREDIRS, $options['max_redirects']);
curl_setopt($s, CURLOPT_RETURNTRANSFER, true);
curl_setopt($s, CURLOPT_FOLLOWLOCATION, $options['follow_local']);
curl_setopt($s, CURLOPT_COOKIEJAR, $options['cookie_location']);
curl_setopt($s, CURLOPT_COOKIEFILE, $options['cookie_location']);
if (!empty($options['auth_name']) is_string($options['auth_name']))
{
curl_setopt($s, CURLOPT_USERPWD, $options['auth_name'] . ':' . $options['auth_pass']);
}
if (!empty($options['post']))
{
curl_setopt($s, CURLOPT_POST, true);
curl_setopt($s, CURLOPT_POSTFIELDS, $options['post']);
//curl_setopt($s, CURLOPT_POSTFIELDS, array('username' = 'aeon', 'password' = '111111'));
}
if ($options['include_header'])
{
curl_setopt($s, CURLOPT_HEADER, true);
}
if ($options['no_body'])
{
curl_setopt($s, CURLOPT_NOBODY, true);
}
if ($options['session'])
{
curl_setopt($s, CURLOPT_COOKIESESSION, true);
curl_setopt($s, CURLOPT_COOKIE, $options['session']);
}
curl_setopt($s, CURLOPT_USERAGENT, $options['useragent']);
curl_setopt($s, CURLOPT_REFERER, $options['referer']);
$res = curl_exec($s);
$status = curl_getinfo($s, CURLINFO_HTTP_CODE);
curl_close($s);
return $res;
}
如何调用高德地图api
先要去注册一个高德平台账号,然后创建一个应用,可以拿到一个appkey。通过这个appkey 在java里面用http请求他的接口就可以拿到信息了,返回的应该是json格式的数据吧。他的接口文档平台上面有。
如何使用php自动定位当前城市
百度地图或者高德地图的api 接口里面有,直接调用接口就可以了