0

我正在尝试读取返回数组的脚本 api:

$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'];

以上显示:

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

如何获取 file_name 和其他值?

4

2 回答 2

1

要获取数组中的任何值,请以与 file_id 相同的方式引用它,但将键替换为您之后的值,例如:

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

然后要简单地在页面上显示它,您可以使用 echo:

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

所以你的完整代码是:

    <?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);
}
于 2012-11-04T19:11:56.447 回答
0

我想到的最简单的解决方案与已经是最后一个命令的解决方案完全相同。

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

从. $api =_print_r

于 2012-11-04T19:10:58.910 回答