如何在api中展示产品json

时间:2017-08-14 12:39:56

标签: php api magento magento2

我遇到问题,我无法在magento2中获得产品json?任何人都可以解决我的问题,我成功获得令牌但无法在php中获取产品json

这是我的以下php代码

<?php     
$userData = ["username" => "admin", "password" => "admin_password"];

    $ch = curl_init("https://www.experian-ccmp.com/rest/V1/integration/admin/token");
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($userData));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Lenght: " . strlen(json_encode($userData))));

    $token = curl_exec($ch);

    $ch = curl_init("https://www.experian-ccmp.com/rest/V1/products");
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); //  method
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($emailcontent));
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
    $result = curl_exec($ch);
    echo $result;
    die;
    $result = json_decode($result, 1);
    echo '<pre>';print_r($result);
?>

这是我在json中获得的响应,字段名称是必需的

{"message":"%fieldName is a required field.","parameters":{"fieldName":"product"},"trace":"#0 \/home2\/bhagnani\/public_html\/experian-ccmp\/vendor\/magento\/framework\/Webapi\/ServiceInputProcessor.php(131): Magento\\Framework\\Webapi\\ServiceInputProcessor->processInputError(Array)\n#1 \/home2\/bhagnani\/public_html\/experian-ccmp\/vendor\/magento\/module-webapi\/Controller\/Rest\/InputParamsResolver.php(101): Magento\\Framework\\Webapi\\ServiceInputProcessor->process('Magento\\\\Catalog...', 'save', Array)\n#2 \/home2\/bhagnani\/public_html\/experian-ccmp\/vendor\/magento\/module-webapi\/Controller\/Rest.php(299): Magento\\Webapi\\Controller\\Rest\\InputParamsResolver->resolve()\n#3 \/home2\/bhagnani\/public_html\/experian-ccmp\/vendor\/magento\/module-webapi\/Controller\/Rest.php(216): Magento\\Webapi\\Controller\\Rest->processApiRequest()\n#4 \/home2\/bhagnani\/public_html\/experian-ccmp\/var\/generation\/Magento\/Webapi\/Controller\/Rest\/Interceptor.php(37): Magento\\Webapi\\Controller\\Rest->dispatch(Object(Magento\\Framework\\App\\Request\\Http))\n#5 \/home2\/bhagnani\/public_html\/experian-ccmp\/vendor\/magento\/framework\/App\/Http.php(135): Magento\\Webapi\\Controller\\Rest\\Interceptor->dispatch(Object(Magento\\Framework\\App\\Request\\Http))\n#6 \/home2\/bhagnani\/public_html\/experian-ccmp\/vendor\/magento\/framework\/App\/Bootstrap.php(258): Magento\\Framework\\App\\Http->launch()\n#7 \/home2\/bhagnani\/public_html\/experian-ccmp\/index.php(39): Magento\\Framework\\App\\Bootstrap->run(Object(Magento\\Framework\\App\\Http))\n#8 {main}"}

感谢您提前提出意见

2 个答案:

答案 0 :(得分:0)

而不是 $ ch = curl_init(“https://www.experian-ccmp.com/rest/V1/products”);

尝试此操作以获取所有产品列表

$ ch = curl_init(“https://www.experian-ccmp.com/rest/V1/products?searchCriteria=”);

希望这有帮助!

答案 1 :(得分:0)

你的网址没问题。

只改变:

$ch = curl_init("https://www.experian-ccmp.com/rest/V1/products");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); //  method

于:     curl_setopt($ ch,CURLOPT_CUSTOMREQUEST,&#34; GET &#34;); //方法

检查:http://devdocs.magento.com/swagger/