-1

I can get the JSON file from a weather site, using numeric value of latitude and longitude of a location with this code:

 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL,'https://weathersite.xyz/complete.json?lat=38.59&lon=-8.43');
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5');
 $fIOURL = curl_exec($ch);
 curl_close($ch) ;

However, what I really want is to use php variables ($latitude and $longitude) instead of these numeric values in the url, but when I try to replace them it doesn't work. As an example,

 $latitude="38.50";
 $longitude="-8.43";
 ....
 curl_setopt($ch, CURLOPT_URL,"https://weathersite.xyz/complete.json?lat={$latitude}&lon={longitude}");
 ....

I also tried all possibilities of single and double quoting the variables in the url like I do in html, but nothing seems to work with curl_setopt. Is there any solution for including php variables inside a url in curl_setopt?

  • 1
    Why shouldn't that be possible? The URL you are calling is a string like all others – Nico Haase Jun 28 '21 at 13:32
  • 1
    This has nothing to do with URLs specifically, just learn how to concatenate a string – ADyson Jun 28 '21 at 13:36
  • @esquew thanks. I am very familiar with single and double quoting strings in php, and I have tried all possibilities but this url only works with numeric values and gives no response if I replace them by php variables. – Meteo Litoral Jun 28 '21 at 13:38
  • 1
    Then you need to share more details. The code you've shown doesn't even declare values for these two variables – Nico Haase Jun 28 '21 at 13:40
  • 1
    Please show what you tried then, exactly. Because the version you've shown us using variables also uses a single quoted string, which, as you claim you already know, won't interpolate the PHP variables within it. I feel that if you really already knew that, you wouldn't have tried it like that. – ADyson Jun 28 '21 at 13:43
  • 1
    Have you tried generating the URL first, write it to a variable, and dump that? This could help to spot the problem – Nico Haase Jun 28 '21 at 13:53
  • 1
    Thanks for the update. `&lon={longitude}` ...typo, should be `&lon={$longitude}`. Then it works fine: http://sandbox.onlinephpfunctions.com/code/a5c8effcadf56a1556f3f58d1e459580bff82454 – ADyson Jun 28 '21 at 14:14
  • @Nico Hasse thanks I spotted the problem. Lat and long could not be more than 4 decimals and I was using 6! – Meteo Litoral Jun 28 '21 at 14:17
  • 2
    Ah so basically you showed us sample data which didn't actually reproduce the issue. And it was nothing to do with generating the URL, it was the specific content which was the problem, causing the request to fail (rather than the URL string to be incorrectly generated). – ADyson Jun 28 '21 at 14:18

1 Answers1

1

Make sure you are using the correct type of quotes:

curl_setopt($ch, CURLOPT_URL,"https://weathersite.xyz/complete.json?lat={$latitude}&lon={$longitude}");

The curly braces are not a must in this case. I just added them for readability.

Here are some more details regarding string concatenation in PHP

This note explains your case exactly

Cornel Raiu
  • 2,758
  • 3
  • 22
  • 31