php - PHP 中的 Api - 接受curl 请求并处理

标签 php api curl

不确定是否有人可以帮助我解决问题。

我必须为我工作的公司编写一些 php,让我们能够与接受 JSON 主体的 API 集成。我使用了 cUrl 方法,并且该脚本运行良好。

如果我想构建另一个 php 页面来接受我发送的请求,我该怎么做?

假设我想让某人向我发送相同的请求,然后希望他们发送的信息进入我的数据库,如何将他们的请求转换为 php 字符串?

这是我发送的代码。

<?
$json_string = json_encode(array("FirstName" => $name, "MiddleName" => " ", "LastName" => $last));;
// echo $json_string;
// jSON URL which should be requested
$json_url = 'http://www.exampleurl.com';
// jSON String for request
// Initializing curl
$ch = curl_init( $json_url );
// Configuring curl options
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array(
    'Accept: application/json;charset=utf-8',
    'Content-Type: application/json;charset=utf-8',
    'Expect: 100-continue',
    'Connection: Keep-Alive') ,
CURLOPT_POSTFIELDS => $json_string
);
// Setting curl options
curl_setopt_array( $ch, $options );
// Getting results
$result =  curl_exec($ch); // Getting jSON result string
echo $result;
$myArray = json_decode($result);
$action = $myArray->Action;
?>

最佳答案

要从您将收到的 POST 中获取原始数据,您可以使用 $postData = file_get_contents('php://input');

http://php.net/manual/en/reserved.variables.post.php

然后您将 json_decode() 该 POST 的内容重新转换为 JSON。

http://php.net/manual/en/function.json-decode.php

关于php - PHP 中的 Api - 接受curl 请求并处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13808347/

相关文章:

php - Mysql保存12月31日文章14 :25 but data function showing the date in 2019

php - 如何获取视频的嵌入代码

php - 错误 :error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure

php - REST 风格的 PHP : how does it work from client side to server side?

php - 如何在 Windows 上安装 cURL?

PHP FILTER_VALIDATE_EMAIL 无法正常工作

php - 无法在 Yii2 swift 邮件程序中打开文件进行阅读

php - Symfony2/学说 : findBy multiple queries that can be NULL

asp.net - Shutterfly 订单 API 。

python - 过滤/子请求以获得结果