如何在浏览器

时间:2017-11-13 09:02:04

标签: php rest api curl magento2

我在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风暴的终端(json中的id,name,sku,price等)得到了完美的答案,但是如果我想通过浏览器得到答案它会显示我在服务器中找不到错误404请求URL (实际上传文件时)。

1 个答案:

答案 0 :(得分:1)

在Web服务器的某个地方听起来像个问题。检查文档根目录中的.htaccess和apache中的主机配置。