【问题标题】:Webservice for JSON post用于 JSON 帖子的 Web 服务
【发布时间】:2014-11-12 13:13:53
【问题描述】:

我是 Web 服务的新手,也是 PHP 和 MAMP 的新手。

我成功编写了 PHP GET 网络服务,但我不知道在 MAMP 中为 PHP 编写 POST 网络服务。

其实我想发布一个 JSON。

我们将衷心感谢任何帮助。

【问题讨论】:

    标签: php arrays json web-services post


    【解决方案1】:

    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 数据

    【讨论】:

    • 我可以将它用于 JSON Post
    • 谢谢马亨德拉。我一定会看的
    猜你喜欢
    • 1970-01-01
    • 2015-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-04
    • 1970-01-01
    • 2022-07-08
    相关资源
    最近更新 更多