'Fixing data fetching from an API (PHP)

I have this code that sends a 6 digit BIN to an API and returns the data in an echo: (But that code doesn't work anymore)

$bin = str_replace(' ', '', $num);
$bin     = substr($bin, 0, 6);
$getbank = explode($bin, file_get_contents("http://bins.pro/search?action=searchbins&bins=" . $bin . "&bank=&country="));
$checkk = explode("</td><td>", $getbank[2]);
$namabnk = explode("</td></tr>", $checkk[5]);
$ccbrand = $checkk[2];
$ccbank  = $namabnk[0];
$cctype  = $checkk[3];
$ccklas  = $checkk[4];

echo '{"pasa":0,"info":"<font color=blue><b>Yes</b></font> | '.$otro.' [Datos: <font color=purple><b>' . $ccbrand . '</b></font><font color=red><b> - </b></font><font color=blue><b>' . $ccbank . '</b></font><font color=red><b> - </b></font><font color=blue><b>' . $cctype . '</b></font><font color=red><b> - </b></font><font color=blue><b>' . $ccklas . '</b></font>]"}';

And I found this code on Github from a telegram bot (which I just updated the API Link to a working one) but I don't know how to adapt it to my original code.

//Bin Lookup
if(strpos($message, "!bin") === 0){
    $bin = substr($message, 5);
    $curl = curl_init();
    curl_setopt_array($curl, [
    CURLOPT_URL => "https://bins-su-api.vercel.app/api/".$bin,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "GET",
    CURLOPT_HTTPHEADER => [
    "accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    "accept-language: en-GB,en-US;q=0.9,en;q=0.8,hi;q=0.7",
    "sec-fetch-dest: document",
    "sec-fetch-site: none",
    "user-agent: Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1"
   ],
   ]);

 $result = curl_exec($curl);
 curl_close($curl);
 $data = json_decode($result, true);
 $bank = $data['data']['bank'];
 $country = $data['data']['country'];
 $brand = $data['data']['vendor'];
 $level = $data['data']['level'];
 $type = $data['data']['type'];
 $result1 = $data['result'];

Could someone tell me how to adapt it to the original code? Frankly I know very little PHP and I get very confused looking at that cURL thing. I need only the data that is looked at in both codes:

  • Bank
  • Country
  • Brand
  • Type
  • Level

Thank you in advance.



Solution 1:[1]

Assuming that $bin is a string of 6 digits, then

$jsonString = file_get_contents('https://bins-su-api.vercel.app/api/'.$bin);
if($jsonString)
{
  $jsonValue = json_decode($jsonString);
  if($jsonValue AND $jsonValue->result)
  {
    $bank = $jsonValue->data->bank;
    $country = $jsonValue->data->country;
    $brand = $jsonValue->data->vendor;
    $type = $jsonValue->data->type;
    $level = $jsonValue->data->level;
  }
}

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 IVO GELOV