PHP请求失败,而Python请求成功

2024-06-16 12:53:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用PHP向站点发送请求。在此之前,我曾尝试使用Python发送相同的请求,结果成功。但当我尝试使用PHP时,失败了:

Notice: file_get_contents(): Content-type not specified assuming application/x-www-form-urlencoded in C:\xampp\index.php on line 24

Warning: file_get_contents(https://api.prime.date/chat/dialogs/by-criteria): failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error in C:\xampp\index.php on line 24
bool(false)

Python代码:

url = "https://api.prime.date/chat/dialogs/by-criteria"

headers = CaseInsensitiveDict()
headers["authority"] = "api.prime.date"
headers["method"] = "POST"
headers["path"] = "/chat/dialogs/by-criteria"
headers["scheme"] = "https"
headers["accept"] = "application/json"
headers["accept-encoding"] = "gzip, deflate, br"
headers["accept-language"] = "uk-UA,uk;q=0.9,en-US;q=0.8,en;q=0.7"
headers["content-length"] = "75"
headers["Content-Type"] = "application/json"
headers["cookie"] = cookies here
headers["origin"] = "https://prime.date"
headers["sec-ch-ua"] = '"Chromium";v="86", "\"Not\\A;Brand";v="99", "Google Chrome";v="86"'


data = '{"criteria": [], "limit": 50, "cursor": "", "idReferredUser": 6295007}'
resp = requests.post(url, headers=headers, data=data)

print(resp.json())

PHP代码:

<?php
$url = "https://api.prime.date/chat/dialogs/by-criteria";
$data = http_build_query(
    array('criteria' => [],
          'limit' => 50,
          'cursor' => '',
          'idReferredUser' => '6295007'));
$options = array(
    'http' => array(
        'authority' => 'api.prime.date',
          'method' => 'POST',
          'path' => '/chat/dialogs/by-criteria',
          'scheme' => 'https',
          'accept' => 'application/json',
          'accept-encoding' => 'gzip, deflate, br',
          'accept-language' => 'uk-UA,uk;q=0.9,en-US;q=0.8,en;q=0.7',
          'content-length' => '75',
          'Content-Type' => 'application/json',
          'cookie' => cookies here,
          'origin' => 'https://prime.date',
          'sec-ch-ua' => '"Chromium";v="86", "\"Not\\A;Brand";v="99", "Google Chrome";v="86"',
          'content' => $data));
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result)
?>

有什么想法吗


Tags: httpsapijsonurldatadatebyapplication
1条回答
网友
1楼 · 发布于 2024-06-16 12:53:57

如果您有标题[“内容类型”]=“应用程序/json”,则必须传递json字符串。 在python版本中,我看到您传递的是json字符串,在php版本中,您传递的是uri编码的查询字符串。 尝试:

$data = json_encode([
 'criteria' => [],
 'limit' => 50,
 'cursor' => '',
 'idReferredUser' => '6295007')
])

或者在php版本中将内容类型更改为其他类型

相关问题 更多 >