Making Http request in PHP

Hello Group, Good day, I wonder if you could help with this.
I have found a service that looks up the carrier for cell phone numbers. They have an API that allows me to enter this url into the browser address field:
http://www.carrierservice.com/index.php/api/lookup?key=MyKey&number=MyNumber
I get back the following response in the browser window:
{“Response”:{“creditBalance”:“1000”,“number”:“MyNumber”,“carrier_type”:“mobile”,“carrier”:“Verizon”}} I would like to call this API from within my code and get the response into a variable so I can parse it out.
Any direction you might be able to provide would be appreciated.
Thanks

Hi bwalk!!

You can use file_get_contents to get the information and then json_decode to parse it, like this:


$param = Array(
    "key" => "MyKey",
    "number" => "MyNumber"
);

$url = "http://www.carrierservice.com/index.php/api/lookup?" . http_build_query($param);

$response = file_get_contents($url);
$data = json_decode($response, true);

if(empty($data["Response"])){
    echo "Error while retrieving information. Response:<pre>$response</pre>";
}else{
    echo "Balance: {$data["Response"]["creditBalance"]}<br><br>";
    echo "<pre>";
    print_r($data);
    echo "</pre>";
}

Hello Mamede!!
Thanks for your response, works perfectly!
I wish I could camp out inside your head for awhile.
Once again you have come to my rescue…

You are always welcome bwalk! :slight_smile: