gpt4 book ai didi

rest - "Server cannot understand Content-Type HTTP"检索 token 时出错

转载 作者:行者123 更新时间:2023-12-02 19:13:31 25 4
gpt4 key购买 nike

$adminUrl='http://localhost/magento/index.php/rest/V1/integration/admin/token';

$data = array("username" => "myname", "password" => "mypassword");
$data_string = json_encode($data);
$ch = curl_init($adminUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type=> application/json',
'Content-Length=> ' . strlen($data_string))
);
$token = curl_exec($ch);
print_r($token);

我已经尝试了上面的代码,但无法获取 token ,而是返回错误消息:

({"message":"Server cannot understand Content-Type HTTP header media type application/x-www-form-urlencoded"},

如何解决此错误?

最佳答案

//Authentication rest API magento2.Please change url accordingly your url
$adminUrl='http://127.0.0.1/magento2/index.php/rest/V1/integration/admin/token';
$ch = curl_init();
$data = array("username" => "wsuser", "password" => "password123");

$data_string = json_encode($data);
$ch = curl_init($adminUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$token = curl_exec($ch);
$token= json_decode($token);

来自https://blog.i13websolution.com/magento-2-rest-api-example/

关于rest - "Server cannot understand Content-Type HTTP"检索 token 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36942429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com