【问题标题】:file_get_content('php://input') return wrong datafile_get_contents('php://input') 返回错误数据
【发布时间】:2022-01-23 18:24:27
【问题描述】:

我想将完整的 json 数据发送到我的页面,我使用 file_get_contents('php://input') 并使用 json_decode() 将其转换为 json。但是对于不和谐的嵌入,file_get_contents() 返回错误的数据。

Json 字符串:{"embeds":[{"title":"Hello!","description":"Hi!"}]}

file_get_contents() 返回:embeds=title&embeds=description

我使用 python requests 模块发送 json :

import requests

data = {"embeds":[{"title":"Hello!","description":"Hi! :grinning:"}]}

headers = {'Content-Type': 'application/json'}

r = requests.post('https://example.com', data=data, headers=headers)

print(r.text)

替代 file_get_contents() 或其他?

【问题讨论】:

  • 我认为问题出在前端。你能附上你用来发送数据的代码吗?
  • 首先提取嵌入,然后在嵌入中提取嵌入索引 0 处的 json 数据,该数据现在是完全形成的 json 数据。
  • @AmirrezaNoori 我使用 print_r() 函数来获取 file_get_contents() return $data = file_get_contents('php://input'); print_r($data);
  • 我的意思是,你如何将这个 JSON 发送到 PHP 页面?
  • 哦,我使用python请求模块@AmirrezaNoori

标签: php json file-get-contents


【解决方案1】:

问题来自 Python 脚本中的请求。 data 参数应该作为 JSON 模式的字符串发送。将import json 添加到您的代码中并使用data=json.dumps(data) 而不是data=data

更多关于json.dumps的信息在这里:https://docs.python.org/3/library/json.html

r = requests.post('https://example.com', data=json.dumps(data), headers=headers)

【讨论】:

    猜你喜欢
    • 2012-08-09
    • 2017-09-30
    • 2015-01-24
    • 2011-07-19
    • 1970-01-01
    • 2016-12-27
    • 2013-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多