Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用curl(php)获取数组的键值_Php_Api_Arrays_Curl - Fatal编程技术网

如何使用curl(php)获取数组的键值

如何使用curl(php)获取数组的键值,php,api,arrays,curl,Php,Api,Arrays,Curl,我想使用API,但它会打印很多信息,我不知道如何获得数组的一些键值 <?php $query = "SELECT * FROM kvk WHERE adres='Wit-geellaan 158'"; $host = "http://api.openkvk.nl/php/"; $url = $host ."/". rawurlencode($query); $curl = curl_init(); curl_setopt($curl, CURLOPT_FOLLOWLOCATION,

我想使用API,但它会打印很多信息,我不知道如何获得数组的一些键值

<?php
$query = "SELECT * FROM kvk WHERE adres='Wit-geellaan 158'";
$host  = "http://api.openkvk.nl/php/";
$url   = $host ."/". rawurlencode($query);

$curl = curl_init();
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0); 

curl_exec($curl);

curl_close($curl);
?>
提前谢谢

您好,
要获取所有键和值,请执行以下操作:

$server_output = curl_exec($curl);
var_dump($server_output);
要仅获取密钥列表,请执行以下操作:

$server_output = curl_exec($curl);
ksort($server_output);
foreach ( $server_output AS $key => $val ) {
  echo "$key\n";
}
上述代码是危险的,可能不应按原样使用。他们可能会在响应中放入任何令人讨厌的内容,您会对其进行评估(评估行),这可能会对您的服务器造成不良影响。我会检查他们是否有更好的API,它不会以那种格式发送响应。(json或XML更好)


如果不是,您可能希望考虑手动解析响应数组,而不是使用
eval

响应是字符串而不是数组
ksort
foreach
期望使用数组。您好thnx对于您的响应,解析错误:语法错误,意外的$end-in/var/www/clients/client5/web6/web/test.php(20):eval()'d第1行代码警告:array_-keys()[function.array-keys]:第一个参数应该是第27行的/var/www/clients/client5/web6/web/test.php中的一个数组,这是我使用eval时得到的错误
$server_output = curl_exec($curl);
ksort($server_output);
foreach ( $server_output AS $key => $val ) {
  echo "$key\n";
}
//Use the cURL setting to put the result into a variable rather than printing it    
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 

//store the result rather than print (as we set CURLOPT_RETURNTRANSFER)
$result = curl_exec($curl);
if ( $result === false ){
    //something went wrong, handle the error
}

//evaluate the array result and store it. (Please don't use this line in production code)
//as the $result string is from a untrusted source
eval('$array = '.$result.';');

//then you can, for example, get a list of the types
$types = $array[0]['RESULT']['TYPES'];


//or some keys
$keys = array_keys($array[0]['RESULT']);