skip to Main Content

I am stuck to a problem, I am unable to get the products json in magento2? Can any one solve my problem, I get the token successfully but unable to get the products json in php

Here is my following php code

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

here is what I get the response in json, field name is required

{"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}"}

Thanks for the comments in advance

2

Answers


  1. Instead of
    $ch = curl_init(“https://www.experian-ccmp.com/rest/V1/products“);

    Try this to get all product list

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

    Hope this helps!

    Login or Signup to reply.
  2. Your URL are ok.

    Only change:

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

    to:
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, “GET“); // method

    Check: http://devdocs.magento.com/swagger/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search