简体   繁体   English

如何在Jupyter笔记本中通过python传递PHP中的JSON格式对象?

[英]How could I echo a JSON format object in PHP passed through python in Jupyter notebook?

This is my python (.ipynb) code: 这是我的python(.ipynb)代码:

import requests
import json

url='http://localhost/test.php'
payload = {'name':'Borja'}
headers = {'Content-type': 'application/json'}
r = requests.post(url, data=json.dumps(payload),headers=headers)
print(r.status_code)
print(r.headers['Content-Type'])
print(r.url)
print(r.encoding)
print(r.content)
print(r.text)

This is the output of the .ipynb: 这是.ipynb的输出: .ipynb输出

and this is my php code: 这是我的PHP代码:

<?php

$json = file_get_contents("php://input");

echo "$json";

$json1=json_encode($json);

echo "<h1>$json1</h1>";

?>

This is what i get from the php: 这是我从php中得到的:

PHP的输出

Since the HTTP method for the request is POST, you can directly encode the POST array. 由于请求的HTTP方法是POST,因此您可以直接对POST数组进行编码。

<?php

$json = json_encode($_POST);

echo "<h1>{$json}</h1>";

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM