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?

2
  • Possible duplicate of PHP - concatenate or directly insert variables in string Commented Feb 21, 2017 at 14:02
  • Did you really did any search before asking? Look this question. All you need is concatenate your variable with the string, and there is many ways doing this. Commented Feb 21, 2017 at 14:03

2 Answers 2

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");
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.