如何在浏览器中获取 magento API 调用响应
How to get magento API call response in browser
2 天后我放弃了。我想在浏览器中从 magento2 api 获取所有产品的列表。首先,我尝试了“http://mydomain.con/index.php/rest/V1/products?Authorization=Bearer_TOKENHERE?searchCriteria=”,但它没有用(仍未通过身份验证)。我尝试的下一件事是用 php curl:
调用 api
<?php
//test for 1 product
//API URL for authentication
$apiURL="http://mydomain.con/index.php/rest/V1/integration/admin/token";
//parameters passing with URL
$data = array("username" => "test", "password" => "123456qwe");
$data_string = json_encode($data);
$ch = curl_init($apiURL);
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);
//decoding generated token and saving it in a variable
$token=json_decode($token);
//******************************************//
//Using above token into header
$headers = array("Authorization: Bearer ".$token);
//API URL to get all Magento 2 modules
$requestUrl='http://mydomain.con/index.php/rest/V1/products/24-MB01';
$ch = curl_init($requestUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
//decoding result
$result=json_decode($result);
//printing result
print_r($requestUrl);
print_r($result);
?>
不幸的是,这个电话让我在 php storm 的终端中得到了一个完美的答案(json 中的 id、名称、sku、价格等)但是如果我想通过浏览器得到答案显示错误 404 请求 URL 未在服务器中找到(实际上传文件时)。
听起来像是网络服务器某处的问题。检查文档根目录中的 .htaccess 和 apache 中的主机配置。
2 天后我放弃了。我想在浏览器中从 magento2 api 获取所有产品的列表。首先,我尝试了“http://mydomain.con/index.php/rest/V1/products?Authorization=Bearer_TOKENHERE?searchCriteria=”,但它没有用(仍未通过身份验证)。我尝试的下一件事是用 php curl:
调用 api<?php
//test for 1 product
//API URL for authentication
$apiURL="http://mydomain.con/index.php/rest/V1/integration/admin/token";
//parameters passing with URL
$data = array("username" => "test", "password" => "123456qwe");
$data_string = json_encode($data);
$ch = curl_init($apiURL);
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);
//decoding generated token and saving it in a variable
$token=json_decode($token);
//******************************************//
//Using above token into header
$headers = array("Authorization: Bearer ".$token);
//API URL to get all Magento 2 modules
$requestUrl='http://mydomain.con/index.php/rest/V1/products/24-MB01';
$ch = curl_init($requestUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
//decoding result
$result=json_decode($result);
//printing result
print_r($requestUrl);
print_r($result);
?>
不幸的是,这个电话让我在 php storm 的终端中得到了一个完美的答案(json 中的 id、名称、sku、价格等)但是如果我想通过浏览器得到答案显示错误 404 请求 URL 未在服务器中找到(实际上传文件时)。
听起来像是网络服务器某处的问题。检查文档根目录中的 .htaccess 和 apache 中的主机配置。