【问题标题】:PHP curl send PUT request from json filePHP curl从json文件发送PUT请求
【发布时间】:2020-02-27 10:21:39
【问题描述】:

我得到了一个 json 文件,它需要转换成 php curl,但我不知道该怎么做。有人可以启发我或指导我吗?

这里是给出的json数据

"request": {
        "auth": {
            "type": "basic",
            "basic": [
                {
                    "key": "password",
                    "value": "",
                    "type": "string"
                },
                {
                    "key": "username",
                    "value": "",
                    "type": "string"
                }
            ]
        },
        "method": "PUT",
        "header": [
            {
                "key": "Content-Type",
                "name": "Content-Type",
                "value": "application/x-www-form-urlencoded",
                "type": "text"
            },
            {
                "key": "Authorization",
                "value": "Basic M6NDhhODhjYWUwY2Y0NDE",
                "type": "text",
                "disabled": true
            }
        ],
        "body": {
            "mode": "formdata",
            "formdata": [
                {
                    "key": "invoice",
                    "type": "file",
                    "src": "file.xml"
                },
                {
                    "key": "invoice",
                    "type": "file",
                    "src": [],
                    "disabled": true
                }
            ],
            "options": {
                "raw": {
                    "language": "xml"
                }
            }
        }
    }

以及使用PHP curl 创建PUT 请求的要求。我希望有人可以提供帮助:)

我在这里看到了一个示例 PHP cURL HTTP PUT,但我不知道在哪里放置 usernamepassword 凭据

【问题讨论】:

  • 有一个available curl_opt 的列表:CURLOPT_USERNAMECURLOPT_USERPWD 可能会有所帮助

标签: php api curl put


【解决方案1】:

您可以使用标题选项(-H)例如:

curl -XPUT -H 'Content-Type: application/x-www-form-urlencoded' -H 'Authorization: Basic M6NDhhODhjYWUwY2Y0NDE' -d '>>body content here<<' 'example.com'

所以 php 等效为:

curl_setopt($curl, CURLOPT_HTTPHEADER, $headers)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-29
    • 2020-10-10
    • 1970-01-01
    相关资源
    最近更新 更多