0

Am trying to read a script api which returns an array:

$api_key = "Uez4TWYH6OAQQHoUcICWJ8UUFYmwQ";
$file_id = "LNzcOp2b1352047884";
$ch=curl_init(); 
curl_setopt($ch,CURLOPT_URL,'http://ads.ngsms.tk/api/info.php?api_key='.$api_key.'&file_id='.$file_id); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$api = curl_exec($ch); $api = print_r($api); 
Echo $api['file_id'];

The above displays:

Array ( 
   [file_id] => LNzcOp2b1352047884 
   [file_name] => sic_ftp_3rda.sisx 
   [file_type] => application/octet-stream 
   [file_size] => 65144
)

How can I get the file_name and other values?

4
  • $api["file_name"]... but im seeing some more problems with your script. you are overriding the $api variable.. why? Commented Nov 4, 2012 at 19:09
  • its not workin.. Its diplay 'A' Commented Nov 4, 2012 at 19:17
  • @Lordcash have you tried the full code part of my answer? Commented Nov 4, 2012 at 19:24
  • @Lordcash The last block of code on the answer copy and paste it all and it should work. Commented Nov 4, 2012 at 19:27

2 Answers 2

1

To get any value within the array reference it in the same way you have done for file_id but instead replace the key with the value you are after e.g:

$api['file_id'];
$api['file_name'];
$api['file_type'];
$api['file_size'];

Then to simply show this on the page, you can use echo:

echo    $api['file_id'];
echo    $api['file_name'];
echo    $api['file_type'];
echo    $api['file_size'];

So your full code would be:

    <?php
$api_key = "Uez4TWYH6OAQQHoUcICWJ8UUFYmwQ";
$file_id = "LNzcOp2b1352047884";
$ch=curl_init(); 
curl_setopt($ch,CURLOPT_URL,'http://ads.ngsms.tk/api/info.php?api_key='.$api_key.'&file_id='.$file_id); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$api = curl_exec($ch); 
//print_r($api);
$fileid = get_string_between($api, "[file_id] => ", "[file_name] ");
$filename = get_string_between($api, " [file_name] => ", " [file_type] => ");
$filetype = get_string_between($api, " [file_type] => ", " [file_size] =>");
$filesize = get_string_between($api, " [file_size] => ", ")");
echo "ID: $fileid <br /> name: $filename <br />type: $filetype   <br /> size: " .$filesize;
function get_string_between($string, $start, $end){
    $string = " ".$string;
    $ini = strpos($string,$start);
    if ($ini == 0) return "";
    $ini += strlen($start);
    $len = strpos($string,$end,$ini) - $ini;
    return substr($string,$ini,$len);
}
Sign up to request clarification or add additional context in comments.

6 Comments

it displayed Filename: A, pls if you have php installed on your system, pls try it out and help me.. The api_key and file_id is working
@Lordcash I've narrowed it down, to the fact that inside $api is not actually an array, but a string of an array, which is why it is not being parsed, but give me a sec and I will get a solution.
@Lordcash most eloquent way I could think of but it works see edit
ok letme try it. But i wrote the api my self, and am sure i return printed array, any ideas on where i went wrong in the info.php
it worked thanks, buh can you tell me how i would make the code in d api print out array i.e correct the error? Because i wrote them both, and i am testing them before realeasing the script
|
0

The simplest solution just popping to my mind would be the exact same one already being the last command.

echo $api['file_name'] // to print it
$file_name = $api['file_name'];

Remove the $api = from before the print_r.

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.