简体   繁体   English

在不使用Curl的情况下在PHP中进行API调用

[英]Make an API call in PHP without using Curl

I want to make the following API call in PHP: 我想在PHP中进行以下API调用:

https://www.googleapis.com/freebase/v1/search?indent=true&filter=%28any+type%3A%2Fcvg%2Fcomputer_videogame%29

How can I do so without using curl? 如何在不使用卷曲的情况下这样做?

使用PHP方法: file_get_contents()http//us1.php.net/file_get_contents

This returns json. 这将返回json。 You should use json_decode() and file_get_contents() for this. 您应该使用json_decode()file_get_contents()

Example: 例:

<?php

$file = file_get_contents("https://www.googleapis.com/freebase/v1/search?indent=true&filter=%28any+type%3A%2Fcvg%2Fcomputer_videogame%29");

$json = json_decode($file, true);

echo "<pre>";
print_r($json);
echo "</pre>";

?>

If it's a simple GET request, file_get_contents can do that reasonably well : 如果它是一个简单的GET请求,file_get_contents可以很好地做到这一点:

$return = file_get_contents('https://www.googleapis.com/freebase/v1/search?indent=true&filter=%28any+type%3A%2Fcvg%2Fcomputer_videogame%29');

Provided you can process the return body (depends on content-type), and do not need to process errors/special http headers/authentication. 如果您可以处理返回正文(取决于内容类型),并且不需要处理错误/特殊http标头/身份验证。

Use " file_get_contents " with the right configuration. 使用具有正确配置的“ file_get_contents ”。

http://fr.php.net/file_get_contents http://fr.php.net/file_get_contents

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

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