Heim > Artikel > Backend-Entwicklung > PHP如何通过URL访问,获得新的URL 【调用百度地图】
我是想在手机微信上进行一段导航
当前导航的URL地址如果录入到IE地址栏中是能正常访问的:http://api.map.baidu.com/direction?origin=latlng:24.481428,118.124813|name:测试起点&destination=latlng:24.503361,118.139877|name:测试终点&mode=driving®ion=厦门&output=html
录入后,回车会发现地址变成了:
http://map.baidu.com/?l=&s=nav%26sn%3D1%24%24%24%2413149737.11%2C2794316.21%24%24%E6%B5%8B%E8%AF%95%E8%B5%B7%E7%82%B9%24%24%24%24%24%24%26en%3D1%24%24%24%2413151414.05%2C2796984.18%24%24%E6%B5%8B%E8%AF%95%E7%BB%88%E7%82%B9%24%24%24%24%24%24%26sc%3D194%26ec%3D194
想知道如果用PHP如何实现 根据原来的URL,请求后得到新的URL?谢谢。
转码的原因是 上面带中文的,我发现微信里,IPHONE导航页面打不开,是空白的。
但是如果通过URL请求后,得到的新URL,配置在微信里是可以正常打开百度地图的。
所以我想写一个函数来处理这个,求教高手,谢谢。
原?的url是http://api.map.baidu.com/direction?origin=latlng:24.481428,118.124813|name:测试起点&destination=latlng:24.503361,118.139877|name:测试终点&mode=driving®ion=厦门&output=html
新的url是http://map.baidu.com/?l=&s=nav%26sn%3D1%24%24%24%2413149737.11%2C2794316.21%24%24%E6%B5%8B%E8%AF%95%E8%B5%B7%E7%82%B9%24%24%24%24%24%24%26en%3D1%24%24%24%2413151414.05%2C2796984.18%24%24%E6%B5%8B%E8%AF%95%E7%BB%88%E7%82%B9%24%24%24%24%24%24%26sc%3D194%26ec%3D194
用新的url就可以打?了,?的不行,???
所以你想可以根??url?取新url然後使用?
原?的url是http://api.map.baidu.com/direction?origin=latlng:24.481428,118.124813|name:测试起点&destination=latlng:24.503361,118.139877|name:测试终点&mode=driving®ion=厦门&output=html
新的url是http://map.baidu.com/?l=&s=nav%26sn%3D1%24%24%24%2413149737.11%2C2794316.21%24%24%E6%B5%8B%E8%AF%95%E8%B5%B7%E7%82%B9%24%24%24%24%24%24%26en%3D1%24%24%24%2413151414.05%2C2796984.18%24%24%E6%B5%8B%E8%AF%95%E7%BB%88%E7%82%B9%24%24%24%24%24%24%26sc%3D194%26ec%3D194
用新的url就可以打?了,?的不行,???
所以你想可以根??url?取新url然後使用?
<?php$url = 'http://api.map.baidu.com/direction?origin=latlng:24.481428,118.124813|name:测试起点&destination=latlng:24.503361,118.139877|name:测试终点&mode=driving®ion=厦门&output=html';$newurl = getMapUrl($url);echo $newurl;function getMapUrl($url){ $result = get_headers($url, true); return isset($result['Location'])? $result['Location'] : '';}?>
<?php$url = 'http://api.map.baidu.com/direction?origin=latlng:24.481428,118.124813|name:测试起点&destination=latlng:24.503361,118.139877|name:测试终点&mode=driving®ion=厦门&output=html';$newurl = getMapUrl($url);echo $newurl;function getMapUrl($url){ $result = get_headers($url, true); return isset($result['Location'])? $result['Location'] : '';}?>
试试.
$url = 'http://api.map.baidu.com/direction?origin=latlng:24.481428,118.124813|name:测试起点&destination=latlng:24.503361,118.139877|name:测试终点&mode=driving®ion=厦门&output=html';$newurl = getMapUrl($url);echo $newurl;function getMapUrl($url){ file_get_contents($url); $result = $http_response_header; if($result){ foreach($result as $val){ if(substr($val,0,10)=='Location: '){ return str_replace('Location: ','', $val); } } } return '';}
urlencode?
试试.
$url = 'http://api.map.baidu.com/direction?origin=latlng:24.481428,118.124813|name:测试起点&destination=latlng:24.503361,118.139877|name:测试终点&mode=driving®ion=厦门&output=html';$newurl = getMapUrl($url);echo $newurl;function getMapUrl($url){ file_get_contents($url); $result = $http_response_header; if($result){ foreach($result as $val){ if(substr($val,0,10)=='Location: '){ return str_replace('Location: ','', $val); } } } return '';}
private function getHeadersNew($url){ $ch= curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_NOBODY, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 1); $f=curl_exec($ch); curl_close($ch); $h=explode("\n",$f); $r=array(); foreach( $h as $t){ $rr=explode(":",$t,2); if(count($rr)==2 ){ $r[$rr[0]]=trim($rr[1]);} } return $r; }