Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.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
在Javascript中使用API中的json(用PHP加载)_Php_Javascript_Json_Api_Curl - Fatal编程技术网

在Javascript中使用API中的json(用PHP加载)

在Javascript中使用API中的json(用PHP加载),php,javascript,json,api,curl,Php,Javascript,Json,Api,Curl,我有一个关于在javascript中使用json(用php加载)的问题。 我从API加载JSON。有人说我最好用卷发 我搜索了有关curl的信息,但没有找到我需要的。 这就是我现在所做的: $url='http://api.urlname.com/api/gateway/call/1.4/getApp?appid=2631'; $str = file_get_contents($url); $data = json_decode($str); 有人知道我怎么做吗 提前谢谢 是的,cURL比仅仅

我有一个关于在javascript中使用json(用php加载)的问题。
我从API加载JSON。有人说我最好用卷发

我搜索了有关curl的信息,但没有找到我需要的。 这就是我现在所做的:

$url='http://api.urlname.com/api/gateway/call/1.4/getApp?appid=2631';
$str = file_get_contents($url);
$data = json_decode($str);
有人知道我怎么做吗

提前谢谢

是的,cURL比仅仅使用
file\u get\u contents
更高级,你可以用cURL做很多事情

如果我们重写您的代码并使用cURL,它将如下所示:

$ch = curl_init("http://api.urlname.com/api/gateway/call/1.4/getApp?appid=2631");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$str = curl_exec($ch);
curl_close($ch);
$data = json_decode($str);

正如我所说的,cURL是一个非常可定制的应用程序,如果您希望更深入地处理复杂的请求,您总是可以看到。

RTLM:有什么问题吗?如果这样做有效,那么为什么需要使用cURL呢?还有,这与JavaScript有什么关系?我如何在JavaScript代码中使用json数据?@niels123我不知道你的确切意思。您是否通过AJAX调用此PHP函数?如果这是您的情况,那么只需回显PHP中的JSON数据即可。谢谢AzizAG!你是最棒的@尼尔斯123很高兴它起作用了!