Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将PHP API调用转换为Python_Php_Python_Python 3.x - Fatal编程技术网

将PHP API调用转换为Python

将PHP API调用转换为Python,php,python,python-3.x,Php,Python,Python 3.x,我有一个现有的API,我连接到使用PHP和发送图像,代码看起来像这样 $url = 'https://example.com/api'; $ch = \curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Subscription-Key: 345sdf4')); curl_setopt($ch,

我有一个现有的API,我连接到使用PHP和发送图像,代码看起来像这样

$url = 'https://example.com/api';
$ch = \curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Subscription-Key: 345sdf4'));
curl_setopt($ch, CURLOPT_POSTFIELDS, "{
            \"fileurl\":\"" . $this->_config['server_config']['server_url'] . $my_image . "\"
        }");
我正在尝试将其转换为Python3,我知道我需要
请求
,所以到目前为止,我已经有了这个

import requests

api_url_base = 'https://example.com/api'
headers = {'Content-Type': 'application/json',
       'Subscription-Key': '345sdf4'}
但就我所知,如何添加图像?我已经知道图像的绝对路径

根据此,您可以执行以下操作:

import requests
import json

url = 'https://example.com/api'
body = {'name': 'something'}
headers = {'content-type': 'application/json'}

r = requests.post(url, data=json.dumps(body), headers=headers)

根据此,您可以执行以下操作:

import requests
import json

url = 'https://example.com/api'
body = {'name': 'something'}
headers = {'content-type': 'application/json'}

r = requests.post(url, data=json.dumps(body), headers=headers)

您的
POST
正文类似于
json.dumps(dict({'fileurl':myUrl}))
您的
POST
正文类似于
json.dumps(dict({'fileurl':myUrl}))