doupu3211
2017-11-13 09:02
采纳率: 100%
浏览 58
已采纳

如何在浏览器中获取magento API调用响应

I gave up after 2 days. I want to get list of all products from magento2 api in browser. First I tried "http://mydomain.con/index.php/rest/V1/products?Authorization=Bearer_TOKENHERE?searchCriteria=" but it didn’t work (still not authenticated). The next thing I’ve tried was to call api with php curl:

<?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);

?>

Unfortunately, this call get me a perfect answer in terminal in php storm (id, name, sku, price, etc in json) but if I want to get answer by browser it shows me Error 404 Request URL not found in server (when file is actually uploaded).

</div>

图片转代码服务由CSDN问答提供 功能建议

我在2天后放弃了。 我想在浏览器中获取magento2 api中所有产品的列表。 首先我尝试了“ http://mydomain.con/index .php / rest / V1 /产品?授权= Bearer_TOKENHERE?searchCriteria = “但它不起作用(仍未经过身份验证)。 接下来我试过用php curl调用api:

 &lt;?php 
 
 //测试1个产品
 
 //用于身份验证的API URL 
 
 $ apiURL =”http://mydomain.con/index.php/rest  / V1 / integration / admin / token“; 
 
 
 
 //用URL传递参数
 
 $ data = array(”username“=&gt;”test“,”password“=&gt;”  123456qwe“); 
 
 $ data_string = json_encode($ data); 
 
 
 
 $ ch = curl_init($ apiURL); 
 
 ncurl_setopt($ ch,CURLOPT_CUSTOMREQUEST,”POST“); \  r 
_ ncurl_setopt($ ch,CURLOPT_POSTFIELDS,$ data_string); 
 
 
 
 
 
 ncurl_setopt($ ch,CURLOPT_RETURNTRANSFER,true); 
 
 ncurl_setopt($ ch,CURLOPT_HTTPHEADER,array(“Content-Type:application / json”,“Content”  -Length:“。strlen($ data_string))); 
 
 $ token = curl_exec($ ch); 
 
 
 
 //解码生成的令牌并将其保存在变量中
 
 $ 标记= json_decode($令牌); 
 
 
 
 // ****  ************************************** // 
 
 
 
 // 将上面的标记用于标题
 
 $ headers = array(“Authorization:Bearer”。$ token); 
 
 
 
 // API URL以获取所有Magento 2模块
 
 $ requestUrl ='  http://mydomain.con/index.php/rest/V1/products/24-MB01'; 
 
 
 
 
 $ ch = curl_init($ requestUrl); 
 
 
 
 
 
 
 
 
 
 
 ... <($ ch,CURLOPT_HTTPHEADER,  $ headers); 
 
 ncurl_setopt($ ch,CURLOPT_RETURNTRANSFER,true); 
 
 $ result = curl_exec($ ch); 
 
 
 
 //解码结果
 
 $ result = json_decode  ($ result); 
 
 
 
 //打印结果
 
 
 
print_r($ requestUrl); 
 
 
 
print_r($ result); 
 
 
 \  n?&gt;   
 
  
 
  
 
 
 
 

不幸的是,这个电话让我在终端中得到了完美的答案 在php风暴(json中的id,name,sku,price等)但是如果我想通过浏览器得到答案它会显示我在服务器中找不到错误404请求URL(当文件实际上传时)。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanniu3385 2017-11-13 09:25
    已采纳

    Sounds like a problem somewhere in web server. Check .htaccess in document root and host config in apache.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题