【发布时间】:2012-12-10 20:06:32
【问题描述】:
不确定是否有人可以帮助我解决问题。
我必须为我工作的公司编写一些 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 数组中查找 json_string。
-
@Hast 看起来不像是在使用 $_POST 数组,而是在使用 JSON。在这种情况下,他将不得不从 POST 正文中获取原始数据和/或从该正文中寻找他想要的单个 JSON 对象以获得他需要的内容。他可以做一个 http 查询格式的版本,但我不相信这是示例显示的内容。