2017-02-21 69 views
-1

我使用curl函数从某个网页获取信息。在下面的代码的URL是静态的:curl_init中的PHP变量URL

$curl = curl_init('http://00.00.0.00/route/v1/driving/12.869446,54.990799;12.045227,54.044362?overview=full&steps=true'); 
curl_setopt($curl, CURLOPT_PORT, 5000); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 5000); 
$result = curl_exec($curl); 

但在URL所需要的零件的LAT LNG(12.869446,54.990799)是PHP变量,如:$lng$lat

我的第一个解决方案不起作用:

$lat = '54.990799'; 
$lng = '54.990799'; 
$curl = curl_init('http://00.00.0.00/route/v1/driving/$lng,$lat;12.045227,54.044362?overview=full&steps=true'); 

我与“第二个解决方案无法正常工作或:

$curl = curl_init('http://00.00.0.00/route/v1/driving/"$lng","$lat";12.045227,54.044362?overview=full&steps=true'); 

谁能帮我最佳的解决方案

+0

的可能的复制[PHP - 串联或直接插入在字符串变量(http://stackoverflow.com/questions/5605965/php-concatenate-or-directly-insert-variables-in-string) –

+0

在问之前你有没有做过任何搜索?看看[这](http://stackoverflow.com/questions/5605965/php-concatenate-or-directly-insert-variables-in-string)的问题。你所需要的只是把你的变量和字符串连接起来,并且有很多方法可以做到这一点。 –

回答

0

变量可用于“”内。

$curl = curl_init("http://00.00.0.00/route/v1/driving/{$lng},{$lat};12.045227,54.044362?overview=full&steps=true"); 
0

您可以嵌入在一个字符串变量是这样的:

$curl = curl_init("http://00.00.0.00/route/v1/driving/$lng,$lat;12.045227,54.044362?overview=full&steps=true"); 

还是为了更好的可读性也增加{}各地变量(IDE往往能作出适当的代码高亮这样的语法):

$curl = curl_init("http://00.00.0.00/route/v1/driving/{$lng},{$lat};12.045227,54.044362?overview=full&steps=true"); 

或者或者,您可以将字符串与变量连接起来。

$curl = curl_init('http://00.00.0.00/route/v1/driving/' . $lng . ',' . $lat . ';12.045227,54.044362?overview=full&steps=true');