【发布时间】:2014-11-12 13:13:53
【问题描述】:
我是 Web 服务的新手,也是 PHP 和 MAMP 的新手。
我成功编写了 PHP GET 网络服务,但我不知道在 MAMP 中为 PHP 编写 POST 网络服务。
其实我想发布一个 JSON。
我们将衷心感谢任何帮助。
【问题讨论】:
标签: php arrays json web-services post
我是 Web 服务的新手,也是 PHP 和 MAMP 的新手。
我成功编写了 PHP GET 网络服务,但我不知道在 MAMP 中为 PHP 编写 POST 网络服务。
其实我想发布一个 JSON。
我们将衷心感谢任何帮助。
【问题讨论】:
标签: php arrays json web-services post
rest webservices 就是这么简单,看看吧。
<?php
$data = array('foo' => 'bar', 'red' => 'blue');
$ch = curl_init();
$post_values = array( 'json_data' => json_encode( $data ) );
curl_setopt($ch, CURLOPT_URL, 'http://localhost/server.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_values);
$data = curl_exec($ch);
if(!curl_errno($ch))
{
echo 'Received raw data' . $data;
}
curl_close($ch);
?>
server.php
<?php
$data = json_decode( $_POST['json_data'] );
// ... do something ...
header('Content-type: text/json');
print json_encode($response);
?>
您可以使用它来发布 JSON 数据
【讨论】: