Question

I tried to update customer data with magento customer API like code below.

<?php 
public function cek()
{
    $jsonObject = json_decode(file_get_contents("http://xxx.xxxxxx.com:50001/Services/Magento.asmx/getCustomerList"));
    $userData = array("username" => "xxxxxx", "password" => "xxxxxx");
    $ch = curl_init("http://xxx.xxxxxxx.com/index.php/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);
    $cek = [];
    $simpan = [];

    for ($i=0; $i< count($jsonObject); $i++) {
        if ($jsonObject[$i]->email) {
          $cek[] = $jsonObject[$i]; 
        }
    }

    $allCustomers = $this->getCollection();

    foreach ($allCustomers as $eachCustomer) {
        $custPrefix = $eachCustomer->getPrefix();
        $custId = $eachCustomer->getId();

        for ($i=0; $i < count($cek); $i++) { 
            if ($custPrefix == $cek[$i]->customerId) {
                $customerData = [
                    'customer' => [
                        'id' => (int)$custId,
                        "firstname" => $cek[$i]->firstName,
                        "lastname" => "Update",
                        "storeId" => 1,
                        "websiteId" => 1
                    ]
                ];

                $ch = curl_init("http://xxx.xxxxxxxxx.com/index.php/rest/V1/customers/10");
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($customerData));
                curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));

                $result = curl_exec($ch);
                $result = json_decode($result, 1);
            }
        }
    }

    return $result;
}

public function getCollection()
{
    //Get customer collection
    return $this->_customers->getCollection();
}

?>

But when i tried to var_dump the result is array(2) { ["message"]=> string(44) "No such entity with %fieldName = %fieldValue" ["parameters"]=> array(2) { ["fieldName"]=> string(10) "customerId" ["fieldValue"]=> int(10) } }

Is there something i do wrong?

Was it helpful?

Solution

$ch = curl_init("http://xxx.xxxxxxxxx.com/index.php/rest/V1/customers/10"); should be $ch = curl_init("http://xxx.xxxxxxxxx.com/index.php/rest/V1/customers/" . $custId);

OTHER TIPS

Got the same Error because I had no Customer in my System. I registered an account and got my result over the api.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top