简体   繁体   English

简化php curl代码

[英]Simplify php curl code

I am trying to get a JSON from a url. 我试图从网址获取JSON。 My code is working but I think that I could do it better. 我的代码正在运行,但我认为我可以做得更好。 (remove json_decode or json_encode). (删除json_decode或json_encode)。 Right now this is what I have: (EDIT: I still want to use curl) 现在这就是我所拥有的:(编辑:我仍然想使用卷曲)

<?php
 $curl = curl_init('url');

 curl_setopt($curl, CURLOPT_URL, 'url');
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

 $result = curl_exec($curl);

 curl_close($curl);

 $data = json_decode($result, true);

 echo json_encode($data["name"]);

 ?>

You can try this: 你可以试试这个:

$data = json_decode(file_get_contents('php://input'));
print_r($data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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