【问题标题】:PHP : How to submit "JSON" via the PUT method?PHP:如何通过 PUT 方法提交“JSON”?
【发布时间】:2016-07-19 07:05:15
【问题描述】:

我正在制作(并测试)我的小型 PHP API。将JSON 作为数据类型提交时,两种 GET/POST 方法都可以。

PUT 中的问题

  • 我无法通过PUT 方法提交JSON 数据。当我这样做时,服务器端得到了空数据。
  • 但是当我不使用json 作为数据类型(并且只使用纯文本数据)时,我可以成功接收和解析数据

这是我的测试用例。

客户端(提交)

(通过 PHP 提交) submit.php:

$data = array("fruit"=>"watermelon", "destination"=>"germany");
$data = json_encode($data);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/api.php");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data)));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$respond = curl_exec($ch);
curl_close($ch);
print_r($respond);

(通过邮递员提交):

服务器端(接收/解析)

api.php:

$decoded_input = json_decode(file_get_contents("php://input"), true);
parse_str($decoded_input, $putdata);

header('Content-type: application/json');
echo json_encode( $putdata );

输出

[]

问题

所以似乎在服务器端接收/解析是问题

  • 如何通过 PUT 方法提交 JSON 数据类型?
  • 我的服务器端 (Apache + PHP) 中是否有一些设置可以启用(允许)PUT 方法中的 json 数据类型?

** 我只是无法通过 PUT 方法获得 JSON 作品。感谢大家的热心帮助。

【问题讨论】:

  • 你能收到“decoded_input”的值吗?
  • 嗨@HariKrishnan.P,decoded_input 为空白。请问有什么建议吗? (我还稍微编辑(改进)了这个问题)。谢谢:))
  • 什么是客户端响应“print_r($respond);”你能在 CURL init() 之前打印“数据”数组值吗
  • 为什么在api.php 中使用parse_str()?在您的情况下,它应用于数组。只需删除此行。在json_decode 之后你应该有 php 数组。
  • 嗨@Wazelin,当我按照你的建议尝试时,我得到了null。 (我只是无法通过 PUT 提交 JSON。我已经尝试过许多提交工具):(

标签: php json apache http-headers put


【解决方案1】:
<?php
$decoded_input = json_decode(file_get_contents("php://input"), true);

//Here you have usual php array stored in $decoded_input. Do some stuff with it.

header('Content-type: application/json');
echo json_encode($decoded_input);

【讨论】:

  • 我用的和你的一模一样,但是得到的是空(或)空白数据。请指教。
【解决方案2】:

抱歉,无法评论,Rep 低。

几周前,我还玩弄了一些简单的 API 调用,并编写了一个小函数来处理它们。

public function callAPI($method, $url, $data = false) {

    $ch = curl_init ();

    switch ($method) {
        case "POST" :
            curl_setopt ( $ch, CURLOPT_POST, 1 );

            if ($data) {
                curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
                curl_setopt ( $ch, CURLOPT_HTTPHEADER, array (
                    'Content-Type: application/json',
                    'Content-Length: ' . strlen ( $data ) 
                ));
            }
            break;
        case "PUT" :
            curl_setopt ( $ch, CURLOPT_PUT, 1 );
            break;
        case "GET" :
            //No settings required
        break;
    }

    curl_setopt ( $ch, CURLOPT_URL, $url );
    curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );

    $responde = curl_exec ( $ch );

    curl_close ( $ch );

    return $responde;
}

PUT似乎还需要其他设置

curl_setopt ( $ch, CURLOPT_PUT, 1 );

AFAIK 你的选项curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); 只会改变发送的字符串,而不是实际的方法

来源:https://curl.haxx.se/libcurl/c/CURLOPT_CUSTOMREQUEST.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-17
    • 2014-02-11
    • 1970-01-01
    • 1970-01-01
    • 2014-08-23
    • 1970-01-01
    相关资源
    最近更新 更多