使用 PHP 创建一个 API 服务器,从另一个 api 获取某些数据(示例:api.themoviedb.org)
Creating an API server with PHP that gets certain data from another api ( Example: api.themoviedb.org)
我正在创建一个 API 用于获取某些数据(例如来自 api.themoviedb.org 的某些电影),然后我需要 return 此数据作为 JSON 目的。我想知道是否有人对我迷路时如何解决这个问题提供了一些意见。
我知道我需要从我的 api 向 public api 发出 GET 请求,其中包含特定的搜索条件,显然还有一个 api 键。然后我需要 return 将此数据返回给发出搜索请求的用户。
请问有什么建议吗?
只需像往常一样设计您的API,我指的是端点、路由、输出格式等
要从其他网络资源中检索数据,您可以使用:
- php
curl
wrapper(最好)
<?php
$postData = 'whatever';
$headers = [
'Content-Type: application/json',
'Auth: depends-on-your-api',
];
$ch = curl_init("http://your-remote-api.url");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// for POST request:
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
// end for POST request
$response = curl_exec($ch);
$apiResponseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
<?php
$postData = 'whatever';
$contextData = [
'http' => [
'method' => 'POST',
'header'=> "Content-type: application/json\r\n"
. "Auth: depends-on-your-api\r\n",
'content' => $postData
]
];
$response = file_get_contents(
'http://your-remote-api.url',
false,
stream_context_create($contextData)
);
链接指向 PHP 文档的特定部分,可以提示如何进一步进行。
我正在创建一个 API 用于获取某些数据(例如来自 api.themoviedb.org 的某些电影),然后我需要 return 此数据作为 JSON 目的。我想知道是否有人对我迷路时如何解决这个问题提供了一些意见。
我知道我需要从我的 api 向 public api 发出 GET 请求,其中包含特定的搜索条件,显然还有一个 api 键。然后我需要 return 将此数据返回给发出搜索请求的用户。
请问有什么建议吗?
只需像往常一样设计您的API,我指的是端点、路由、输出格式等
要从其他网络资源中检索数据,您可以使用:
- php
curl
wrapper(最好)
<?php
$postData = 'whatever';
$headers = [
'Content-Type: application/json',
'Auth: depends-on-your-api',
];
$ch = curl_init("http://your-remote-api.url");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// for POST request:
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
// end for POST request
$response = curl_exec($ch);
$apiResponseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
<?php
$postData = 'whatever';
$contextData = [
'http' => [
'method' => 'POST',
'header'=> "Content-type: application/json\r\n"
. "Auth: depends-on-your-api\r\n",
'content' => $postData
]
];
$response = file_get_contents(
'http://your-remote-api.url',
false,
stream_context_create($contextData)
);
链接指向 PHP 文档的特定部分,可以提示如何进一步进行。