0

I am facing one issue while adding new address of a existing customer using its id. I am explaining my code below.

 <?php
include('db.php');
$response=array();
$adminUrl='http://magento-148486-425775.cloudwaysapps.com/index.php/rest/V1/integration/admin/token';
$ch = curl_init();
$data = array("username" => "raj", "password" => "raj@0987.");

$data_string = json_encode($data);                       
$ch = curl_init($adminUrl); 
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);
$email=$_POST['email'];
$company=$_POST['company'];
$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$mobile=$_POST['mobile'];
$street=$_POST['street'];
$city=$_POST['city'];
$state=$_POST['state'];
$pin=$_POST['pin'];
$country_id=$_POST['country_id'];
$user_id=$_POST['user_id'];
//echo $user_id;exit;
$sql=mysqli_query($conn,"select * from mobusers where id='".$user_id."'");
if(mysqli_num_rows($sql)){
    $row=mysqli_fetch_array($sql);
    $ent_id=$row['entity_id'];//customer's id
    $customerData = [
        'customer' => [
            'addresses' => [
                [
                    "id"=>$ent_id,
                    "firstname"=> $first_name,
                    "lastname"=> $last_name,
                    "company"=> $company,
                    "street"=> [
                    $street
                    ],
                    "city"=> $city,
                    "postcode"=> $pin,
                    "country_id"=> $country_id,
                    "telephone"=> $mobile
                ]
            ]
        ]
    ];
    $requestUrl='http://magento-148486-425775.cloudwaysapps.com/index.php/rest/V1/customers/'.$ent_id;
    //echo $requestUrl;exit;
    $ch = curl_init();
    $ch = curl_init($requestUrl); 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    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);
    print_r($result);exit;
    if(!isset($result['message']) && $result > 0){
        $response['status']="Success";
        $response['msg']="Your addresse has been updated successfully.";
    }else{
        $response['status']="Error";
        $response['msg']="Failed!";
    }
    echo json_encode($response);
}
?>

Here I have already one existing customer and while registered he does not add any address. I have already the customer's id(i.e-$ent_id(entity_id of customer entity table)) which will be the parent ID of the address table. Using this ID I want to add this new address to address table. But in my case I am getting the following error.

Array
(
    [message] => Request does not match any route.
    [trace] => 
)

Here I need to add this address using this customer id.Please help me.

2
  • In Customer module this by this url of api is used for PUT method can you change your method from POST to PUT and then try once. Aug 27, 2018 at 6:41
  • @CharulTyagi : I have already applied PUT method but its not updating anything.
    – satya
    Aug 27, 2018 at 6:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.