簡體   English   中英

Unable to POST data TO REST API Using PHP CURL but it work fine with Postman

[英]Unable to POST data TO REST API Using PHP CURL but it work fine with Postman

我正在嘗試使用 PHP/CURL 將數據發布到 REST API 並且似乎它不起作用,因為我得到 301 Moved Permanently 錯誤,但它適用於 Z6F061E1CAD5E2783CEA0B2976ED2F。 這是我從 Postman 生成的 PHP CURL 的片段

$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'api url',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => array('text' => 'test'),
CURLOPT_HTTPHEADER => array(
'api_key: key',
'Content-Type:application/json',
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;

它總是永久返回 301 Moved,請注意,如果我更改 API 密鑰,我會收到未經授權的錯誤,這意味着它會命中服務器,但我不確定我錯過了什么,我嘗試了多個標題組合。

在這方面的任何幫助將不勝感激。

引用 PHP 手冊:

CURLOPT_POSTFIELDS:此參數可以作為 urlencoded 字符串(如 'para1=val1&para2=val2&...')或作為字段名稱作為鍵和字段數據作為值的數組傳遞。 如果 value 是一個數組,則 Content-Type header 將設置為 multipart/form-data。

由於您使用數組,因此您需要使用multipart

請參閱此帖子: curl CURLOPT_POSTFIELDS 的 POST 格式

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM