0

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?

Jakub Matczak
  • 15,341
  • 5
  • 46
  • 64
Banzouzi
  • 11
  • 5
  • Possible duplicate of [PHP - concatenate or directly insert variables in string](http://stackoverflow.com/questions/5605965/php-concatenate-or-directly-insert-variables-in-string) – Elias Soares Feb 21 '17 at 14:02
  • Did you really did any search before asking? Look [this](http://stackoverflow.com/questions/5605965/php-concatenate-or-directly-insert-variables-in-string) question. All you need is concatenate your variable with the string, and there is many ways doing this. – Elias Soares Feb 21 '17 at 14:03

2 Answers2

3

Variables can be used inside "".

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

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');
Jakub Matczak
  • 15,341
  • 5
  • 46
  • 64