-1

The following PHP code is working fine passing a specific IP Address to curl_init:

$ch = curl_init('https://ipgeolocation.abstractapi.com/v1/?api_key=my_key&ip_address=5.79.66.162');

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    
curl_setopt($ch, CURLOPT_HEADER, 0);  

$data = curl_exec($ch);    
curl_close($ch);  

$yummy = json_decode($data);     
$country= $yummy->country;    //this is working fine. country is being passed

===========================================

But I need to pass the ip address in a variable in the curl_init. However you cannot pass a PHP variable between single quotes. So the following curl_init statement is NOT working.

$ip="5.79.66.162";
$ch = curl_init('https://ipgeolocation.abstractapi.com/v1/?api_key=my_key&ip_address=$ip');

How does the code above = need to be changed in order for the call to work using a variable?

I would appreciate any help anyone can offer.

Alvaro Flaño Larrondo
  • 5,516
  • 2
  • 27
  • 46
John
  • 3
  • 1

1 Answers1

0

However you cannot pass a PHP variable between single quotes

Exactly, so you can use doble quotes here or string concatenation:

$ip="5.79.66.162";
$ch = curl_init("https://ipgeolocation.abstractapi.com/v1/?api_key=my_key&ip_address=$ip");

or

$ip="5.79.66.162";
$ch = curl_init('https://ipgeolocation.abstractapi.com/v1/?api_key=my_key&ip_address=' . $ip);
Alvaro Flaño Larrondo
  • 5,516
  • 2
  • 27
  • 46