PHP:如何提交“JSON”通过PUT方法? [英] PHP : How to submit "JSON" via the PUT method?

查看:252
本文介绍了PHP:如何提交“JSON”通过PUT方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm making (and testing) my small PHP API. Both GET/POST methods are all fine while submitting the JSON as data-type.


  • 我无法通过 PUT JSON 数据c $ c>方法。当我这样做时,服务器端获得了空数据。

  • 但是当我不使用 json 作为数据类型时,(只使用纯文本数据),然后我就可以成功接收和解析数据。 < ----((这很奇怪!))

  • I cannot submit the JSON data via PUT method. When i do, the Server-side got empty data.
  • But when i DO NOT use json as data-type, (and just use the plain text data), then i can receive and parse the data, successfully. <----(( This is something weird! ))

这是我测试过的案例。

(通过PHP提交)submit.php:

(Submit via 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 );



输出



Output

[]



问题



因此,似乎在服务器端接收/解析是问题


  • 如何提交JSON数据类型通过PUT方法?

  • 我的服务器端是否有一些设置( Apache + PHP )来启用(允许) ) json 数据类型在 PUT 方法?

  • How to submit JSON data-type via the PUT method?
  • Are there some settings in my Server Side (Apache + PHP) to enable (to allow) the json data-type in PUT method?

**我无法通过PUT方法获得JSON。非常感谢所有人的帮助。

** I just can't get JSON works via the PUT method. Thanks all for kind helps.

推荐答案

对不起,无法发表评论,无法回复。

Sorry, can't comment, Rep to low.

几个星期前我也玩过简单的API调用,并且我有一些功能来处理它们。

Some weeks ago i also played around with simple API calls and i made a little function to handle them.

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); 仅更改发送的字符串,而不是实际方法

AFAIK your option curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); only changes the string which is sent, not the actual method

Soruce: https:// curl .haxx.se / libcurl / c / CURLOPT_CUSTOMREQUEST.html

这篇关于PHP:如何提交“JSON”通过PUT方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆