简体   繁体   中英

executing curl cmd in php should return json but instead returns array

Just wondering if there is a setting that I am missing in my curl command that is preventing the data from coming back as a json string and instead returning it as an array.

 $cmd='curl -d @/home/wazilly/public_html/recipe.json -H "Content-Type: 
application/json" "https://api.edamam.com/api/nutrition-details?
app_id=XXXXX&app_key=97d3b48d3a8366572c8012a142e28f50"';
exec($cmd,$result);
echo $result; //prints Array

According to the documentation , exec always takes an array as its second parameter, which it fills with all the lines of output of the executed command.

I'm not certain what the output of your command would be like, though. You'd be better off doing a var_dump() on the $result to be certain. But since you're dealing with curl, why not simply use the PHP curl extension ?

Check the prototype for exec() in the PHP manual :

string exec ( string $command [, array &$output [, int &$return_var ]] )

PHP returns an array. If you want the raw output instead, which I assume might actually be JSON, try this instead:

$result = shell_exec($cmd);
echo $result;

我建议您使用json_encode(array())将$ result数组转换为JSON格式

echo json_encode($result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM