Search code examples
phpurlforeach

Editing the scope of an API URL


I am editing an API scope based on the site's URL so if I have example.com/?Wab_id=15 it will edit the API scope to

?scope=accepted&view=films&wab_id=15

I was thinking I could have something like

$ApiData = file_get_contents('http://example.com/api/?scope=accepted&view=films/&wab_id=$id');

and then using Get to retrieve the id that's being passed into the URL to edit the URL of the API. I also tried looping though the entire JSON and then calling the a key inside of the array but also didn't get much luck. My code is below:

$ApiData = file_get_contents('http://example.com/api/?scope=accepted&view=films');
$obj = json_decode($ApiData, true);
$data = $obj;
//here you load $data with whatever you want.
$id = $_GET['id'];
foreach ($data[$id] as $key=>$value){
echo "$key -> $value<br>";
}
?>

but this returns a error of:

Invalid argument supplied for foreach()

I've also tried looping through the Muti array with a foreach inside of a foreach and have displayed the values the code and results are below:

$obj = json_decode($ApiData, true);
$data = $obj;
//here you load $data with whatever you want.
 
foreach ( $data as $film ){
  foreach ( $film as $key=>$val ){
    echo "$key -> $val<br>";
  }
}

results

uid -> 95
wab_id -> 95
title -> La Batalla de los Invisibles
title_en -> Battle of the Invisibles
syn_sm ->
syn_med -> 
syn_lg -> 
form -> 
genre -> 
language -> 
subtitle_lang -> 
year -> 
runtime -> 
place_sub_city -> 
place_sub_state -> 
place_sub_country -> 
place_film_country -> Mexico
place_dir_city -> 
place_dir_state -> 
place_dir_country -> 
accepted -> 1
festival_year -> 2014
trailer -> 
links ->

Solution

  • As I mentioned in my comment:

    Are you passing wab_id, Wab_id, or id to your server where you will access with $_GET? Because it's not clear under which key you are passing it to yourself and if you are using the correct one - which may be the issue you are having right now.

    After you have that squared away manipulating the API params should be a simple affair:

    $api = "http://example.com/api/";
    $params = array(
       'scope' => 'accepted',
       'view' => 'films',
    );
    
    // you need to match the key you are using here to what you are passing
    // to your URL
    if (isset($_GET['id'])) {
      $params['wab_id'] = $_GET['id'];
    }
    
    $url = $api . '?' . http_build_query($params);
    

    Now for the last part, you really should use cURL, Http... or really any thing other than file_get_contents because it doesn't really give you a very good way to handle errors you may come across. I'll give you a quick example using cURL:

    $client = curl_init();
    curl_setopt_array($client, array(
       CURLOPT_RETURNTRASNFER => true,
       CURLOPT_URL => $url // the one we dynamically built above
    ));
    
    $responseText = curl_exec($client);
    
    if ($responseText !== false) {
       $responseInfo = curl_getinfo($client);
    
       if ($responseInfo['http_code'] === 200) {
           // http status ok - you may need to take action based 
           // on other http status codes but I'm not going to delve into that here.
           
           $data = json_decode($responseText, true);
           print_r($data);
       
        } else {
          printf('Error accessing data: HTTP Status %s', $responseInfo['http_code'];
        }
    } else {
      printf('Error: (%s) %s', curl_errno($client), curl_error($client));
    }
    
    curl_close($client);