I need to get cart id using the following api http://localhost/rest/V1/carts/mine but when I run that api using the following code

   $headers = array("Content-Type: application/json",
       'Authorization: Bearer token'
   curl_setopt($ch, CURLOPT_URL, $url.'/rest/V1/carts/mine');
   //curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");  
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

I get the following error

 [message] => %fieldName is a required field.
[parameters] => stdClass Object
        [fieldName] => customerId

although I know that that request sent with empty body


First you need to get customer token by following code

$baseUrl = 'http://localhost/proj2/';
        $token_url = $baseUrl.'index.php/rest/V1/integration/customer/token';
        $ch = curl_init($token_url);

        $data['username'] = 'customer_username';
        $data['password'] = 'customer_password';

        ////////////////  getting admin token from api
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Length: " . strlen(json_encode($data))));

        $token = curl_exec($ch);

        $customer_token = (json_decode($token));

Then pass token in your code after bearer. And after your code write

$customer_info = curl_exec($ch);
$customer_array = json_decode($customer_info);
| improve this answer | |
  • I did so and get same error – Yomna Mansour Nov 21 '18 at 11:45
  • 1
    have you added curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); after curl_init ? – Ashar Riaz Nov 21 '18 at 11:49
  • also add your new code – Ashar Riaz Nov 21 '18 at 11:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.