1

I want to create weather informer that shows weather forecast by visitor's IP.

I'm trying to place variable $ip to the URL but it doesn't work. When I place real IP instead of .$ip. it works.

What am I doing wrong?

$ip=$_SERVER['REMOTE_ADDR'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://free.worldweatheronline.com/feed/weather.ashx?key=xxxxxxxxxxxxxxx&q=.$ip.&localObsTime&num_of_days=5&format=json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$outputJson = curl_exec($ch);
 if ($outputJson === FALSE) {
 echo 'Error: '.curl_error($ch);
 }

 echo '<pre> ';
 print_r($outputJson);   
 echo '</pre> ';  
2
  • when you do print_r do you have the correct output at least ? Commented Jun 25, 2011 at 12:36
  • @Tarek Yes, It returns json data (if IP is real, not a variable) Commented Jun 25, 2011 at 12:42

4 Answers 4

2

You have got some unnecessary dots before and after $ip:

Use any of following:

"http://...$ip..."
"http://...{$ip}..."
"http://..." . $ip . "...";
Sign up to request clarification or add additional context in comments.

Comments

1

Try doing

curl_setopt($ch, CURLOPT_URL, "http://free.worldweatheronline.com/feed/weather.ashx?key=xxxxxxxxxxxxxxx&q=".$ip."&localObsTime&num_of_days=5&format=json");

1 Comment

because of the double quotes that has no effect.
1

you don't need to concatenate the string since you're using doublequotes. so you either do:

curl_setopt($ch, CURLOPT_URL, "http://free.worldweatheronline.com/feed/weather.ashx?key=xxxxxxxxxxxxxxx&q=$ip&localObsTime&num_of_days=5&format=json");

in the url.

Comments

0

you are using the string concatenation operator inside the string. either use

"http://free.worldweatheronline.com/feed/weather.ashx?key=xxxxxxxxxxxxxxx&q=$ip&localObsTime&num_of_days=5&format=json"

or

'http://free.worldweatheronline.com/feed/weather.ashx?key=xxxxxxxxxxxxxxx&q='.$ip.'&localObsTime&num_of_days=5&format=json'

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.