简体   繁体   中英

PHP variable inside curl_init URL

I use the curl function to get information from a certain webpage. In the following code the URL is static:

$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);

But the part's lat lng (12.869446,54.990799) in the URL need to be php variables like: $lng , $lat .

My first solution doesn't work:

$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');

My second solution with " doesn't work either:

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

Can anyone help me with the best solution?

变量可以在“”内使用。

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

You can embed variables in a string this way:

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

Or for better readability also add {} around variables (IDE can often make a proper code highlighting to such syntax):

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

Or alternatively you can concatenate strings with variables.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM