1

I need to call webservice api url & fetch result....

JSON payload provided by API team :

{
"request_id": "R09",
"client_name": "CLIENT",
"services": [
{
"service_code": "REGULAR",
"service_details": [
{
"service_leg": "FORWARD",
"service_data": {
"vendor_name":"Ekart",
"amount_to_collect": 1000,
"dispatch_date": "",
"customer_promise_date": "",
"delivery_type": "SMALL",
"source": {
"address": {
"first_name": "AVThamizhmahan",
"address_line1": "3/1675 ES Garden Vazhudaretty Post Villupuram",
"address_line2": "Tamil Nadu",
"pincode": "400066",
"city": "Villupuram",
"state": "Tamil Nadu",
"primary_contact_number": "88888888"
}
},
"destination": {
"address": {
"first_name": "AVThamizhmahan",
"address_line1": "3/1675 ES Garden Vazhudaretty Post Villupuram",
"address_line2": "Tamil Nadu",
"pincode": "400066",
"city": "Villupuram",
"state": "Tamil Nadu",
"primary_contact_number": "88888888"
}
},
"return_location": {
"address": {
"first_name": "AVThamizhmahan",
"address_line1": "3/1675 ES Garden Vazhudaretty Post Villupuram",
"address_line2": "Tamil Nadu",
"pincode": "400066",
"city": "Villupuram",
"state": "Tamil Nadu",
"primary_contact_number": "88888888"
}
}
},
"shipment": {
"client_reference_id": "ABCD",
"tracking_id": "CLTC0000000001",
"shipment_value": 2000,
"shipment_dimensions": {
"length": {
"value": 1
},
"breadth": {
"value": 1
},
"height": {
"value": 1
},
"weight": {
"value": 1
}
},
"return_label_desc_1": "",
"return_label_desc_2": "",
"shipment_items": [
{
"product_id": "134883075",
"item_content":{},
"category": "Apparel",
"product_title": "Mast&HarbourBlackHandbag: Handbags",
"quantity": 1,
"cost": {
"total_sale_value": 500,
"total_tax_value": 100 ,
“tax_breakup”:{
“cgst”:0.0,
“sgst”:0.0,
“igst”:0.0
}
},
"seller_details": {
"seller_reg_name": "Seller Registered Legal Name",
"vat_id": "",
"cst_id": "",
“tin_id”:””,
“gstin_id”:””
},
“hsn” :””,
“ern”:””,
“discount”:0.0,
"legal_entity": "",
"billable_entity": "",
"cod_remittance_entity": "",
"item_attributes": [
{
"name": "order_id",
"value": "ABCD"
},
{
"name": "invoice_id",
"value": "INV1234"
},
{
"name": "item_dimensions",
"value": "l:b:h:w"
},
{
"name": "brand_name",
"value": "Peter England"
}
],
"handling_attributes": [
{
"name": "isFragile",
"value": "false"
},
{
"name": "isDangerous",
"value": "false"
}
]
}
]
}
}
]
}
]
}

I am calling JSon as array & passing url :

<?php

$url = "https://staging.logistics.com/v2/shipments/create";

$data = array
( 
"request_id"=> "R09",
"client_name"=> "DOC",
"services"=> [
array (
"service_code"=> "REGULAR",
"service_details"=> [
array (
"service_leg"=> "FORWARD",
"service_data"=> array (
"vendor_name"=>"Ekart",
"amount_to_collect"=> 1000,
"dispatch_date"=> "",
"customer_promise_date"=> "",
"delivery_type"=> "SMALL",
"source"=> array (
"address"=> array (
"first_name"=> "AVThamizhmahan",
"address_line1"=> "31675 ES Garden Vazhudaretty Post Villupuram",
"address_line2"=> "Tamil Nadu",
"pincode"=> "400066",
"city"=> "Villupuram",
"state"=> "Tamil Nadu",
"primary_contact_number"=> "88888888"
)
),
"destination"=> array (
"address"=> array (
"first_name"=> "AVThamizhmahan",
"address_line1"=> "31675 ES Garden Vazhudaretty Post Villupuram",
"address_line2"=> "Tamil Nadu",
"pincode"=> "400066",
"city"=> "Villupuram",
"state"=> "Tamil Nadu",
"primary_contact_number"=> "88888888"
)
),
"return_location"=> array (
"address"=> array (
"first_name"=> "AVThamizhmahan",
"address_line1"=> "31675 ES Garden Vazhudaretty Post Villupuram",
"address_line2"=> "Tamil Nadu",
"pincode"=> "400066",
"city"=> "Villupuram",
"state"=> "Tamil Nadu",
"primary_contact_number"=> "88888888"
)
)
),
"shipment"=> array (
"client_reference_id"=> "ABCD",
"tracking_id"=> "DOCC0000000001",
"shipment_value"=> 2000,
"shipment_dimensions"=> array (
"length"=> array (
"value"=> 1
),
"breadth"=> array (
"value"=> 1
),
"height"=> array (
"value"=> 1
),
"weight"=> array (
"value"=> 1
)
),
"return_label_desc_1"=> "",
"return_label_desc_2"=> "",
"shipment_items"=> [
array (
"product_id"=> "134883075",
"item_content"=>array (),
"category"=> "Apparel",
"product_title"=> "Mast&HarbourBlackHandbag=> Handbags",
"quantity"=> 1,
"cost"=> array (
"total_sale_value"=> 500,
"total_tax_value"=> 100 ,
"tax_breakup"=>array (
"cgst"=>0.0,
"sgst"=>0.0,
"igst"=>0.0
)
),
"seller_details"=> array (
"seller_reg_name"=> "Seller Registered Legal Name",
"vat_id"=> "",
"cst_id"=> "",
"tin_id"=>"",
"gstin_id"=>""
),
"hsn" =>"",
"ern"=>"",
"discount"=>0.0,
"legal_entity"=> "",
"billable_entity"=> "",
"cod_remittance_entity"=> "",
"item_attributes"=> [
array (
"name"=> "order_id",
"value"=> "ABCD"
),
array (
"name"=> "invoice_id",
"value"=> "INV1234"
),
array (
"name"=> "item_dimensions",
"value"=> "l=>b=>h=>w"
),
array (
"name"=> "brand_name",
"value"=> "Peter England"
)
],
"handling_attributes"=> [
array (
"name"=> "isFragile",
"value"=> "false"
),
array (
"name"=> "isDangerous",
"value"=> "false"
)
]
)
]
)
)
]
)
]
);
echo $data=json_encode($data);
$data_string = json_encode(array(
    "Payload" => $data
));

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Content-Type : application/json"
));

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
     "Authorization : Basic ZGFpbHlvcmRlcnM6ZHVtbf4fsww="
));

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);

curl_setopt($ch, CURLOPT_POST, 1); 
curl_close($ch);

echo "$result";

?>

Response :

400 Bad request
Your browser sent an invalid request.

Am i sending in proper format ?

Here is APi Document : https://drive.google.com/open?id=13AstBwus-uErvElg2-FN0knIy1m9lMHa

Please let me know if you need any other information....

I am using ekart api.....

Thanks in Advance....

Update

Below is POSTMAN result :

enter image description here

Update 2 :

Error :

{"response":[{"status":"REQUEST_REJECTED","message":["X_Merchant_Code is null "]}],"request_id":"2aa4cf98-0b59-4e2d-ba10-79c39663d6b6"}

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ALL & ~MYSQLI_REPORT_INDEX);

$url = "https://staging.ekartlogistics.com/v2/shipments/create";

$data1 = array
( 
"request_id"=> "835",
"client_name"=> "DOC",
"services"=> [
array (
"service_code"=> "REGULAR",
"service_details"=> [
array (
"service_leg"=> "FORWARD",
"service_data"=> array (
"vendor_name"=>"Ekart",
"amount_to_collect"=> 1000,
"dispatch_date"=> "",
"customer_promise_date"=> "",
"delivery_type"=> "SMALL",
"source"=> array (
"address"=> array (
"first_name"=> "AVThamizhmahan",
"address_line1"=> "31675 ES Garden Vazhudaretty Post Villupuram",
"address_line2"=> "Tamil Nadu",
"pincode"=> "560034",
"city"=> "Villupuram",
"state"=> "Tamil Nadu",
"primary_contact_number"=> "88888888"
)
),
"destination"=> array (
"address"=> array (
"first_name"=> "AVThamizhmahan",
"address_line1"=> "31675 ES Garden Vazhudaretty Post Villupuram",
"address_line2"=> "Tamil Nadu",
"pincode"=> "110037",
"city"=> "Villupuram",
"state"=> "Tamil Nadu",
"primary_contact_number"=> "88888888"
)
),
"return_location"=> array (
"address"=> array (
"first_name"=> "AVThamizhmahan",
"address_line1"=> "31675 ES Garden Vazhudaretty Post Villupuram",
"address_line2"=> "Tamil Nadu",
"pincode"=> "110037",
"city"=> "Villupuram",
"state"=> "Tamil Nadu",
"primary_contact_number"=> "88888888"
)
)
),
"shipment"=> array (
"client_reference_id"=> "ABCD",
"tracking_id"=> "DOCC0000000001",
"shipment_value"=> 2000,
"shipment_dimensions"=> array (
"length"=> array (
"value"=> 1
),
"breadth"=> array (
"value"=> 1
),
"height"=> array (
"value"=> 1
),
"weight"=> array (
"value"=> 1
)
),
"return_label_desc_1"=> "",
"return_label_desc_2"=> "",
"shipment_items"=> [
array (
"product_id"=> "134883075",
"item_content"=>array (),
"category"=> "Apparel",
"product_title"=> "Mast&HarbourBlackHandbag=> Handbags",
"quantity"=> 1,
"cost"=> array (
"total_sale_value"=> 500,
"total_tax_value"=> 100 ,
"tax_breakup"=>array (
"cgst"=>0.0,
"sgst"=>0.0,
"igst"=>0.0
)
),
"seller_details"=> array (
"seller_reg_name"=> "Seller Registered Legal Name",
"vat_id"=> "",
"cst_id"=> "",
"tin_id"=>"",
"gstin_id"=>""
),
"hsn" =>"",
"ern"=>"",
"discount"=>0.0,
"legal_entity"=> "",
"billable_entity"=> "",
"cod_remittance_entity"=> "",
"item_attributes"=> [
array (
"name"=> "order_id",
"value"=> "ABCD"
),
array (
"name"=> "invoice_id",
"value"=> "INV1234"
),
array (
"name"=> "item_dimensions",
"value"=> "l=>b=>h=>w"
),
array (
"name"=> "brand_name",
"value"=> "Peter England"
)
],
"handling_attributes"=> [
array (
"name"=> "isFragile",
"value"=> "false"
),
array (
"name"=> "isDangerous",
"value"=> "false"
)
]
)
]
)
)
]
)
]
);
echo $data=json_encode(array("payload"=>$data1));
echo "<br/><br/>";//die;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

curl_setopt($curl, CURLOPT_HTTPHEADER, Array("Content-Type: application/json", "X_Merchant_Code: DOC"));


curl_setopt($curl, CURLOPT_USERPWD, "dailyorders:dummyKey"); //Your credentials goes here
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //IMP if the url has https and you don't want to verify source certificate

$curl_response = curl_exec($curl);

curl_close($curl);

print_r($curl_response);die;


?>
  • how are you sending the json? posting it in POSTMAN, using an ajax request, something else? How are you reading the posted JSON in your PHP script? – chiliNUT Oct 26 '18 at 06:22
  • @chiliNUT Thanks for reply, i am using curl for this.... –  Oct 26 '18 at 06:24
  • @chiliNUT if i calling in wrong way, please guide me how to call in proper way to get correct response..... –  Oct 26 '18 at 06:31
  • are you missing method PUT on ur CURL – Julius Limson Oct 26 '18 at 06:44
  • @YuukkaLimson i am using `post`, do i still need PUT ? –  Oct 26 '18 at 06:45
  • @chiliNUT do i need to pass in `json format` in php instead of `array` ? –  Oct 26 '18 at 06:53
  • @YuukkaLimson do i need to pass in `json format` in php instead of `array` ? –  Oct 26 '18 at 06:53
  • $result = curl_exec($ch); curl_setopt($ch, CURLOPT_POST, 1); you interchanged these two – Julius Limson Oct 26 '18 at 06:54
  • @YuukkaLimson `interchanged` still same result.... –  Oct 26 '18 at 06:55
  • 1
    try checking your parameters again. you are nesting your json data to PayLoad which the document doesn't require. try only curl_setopt($ch, CURLOPT_POSTFIELDS, $data); – Julius Limson Oct 26 '18 at 07:00
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/182562/discussion-between-vickey-colors-and-yuukka-limson). –  Oct 26 '18 at 07:01
  • Ah, I see now. I think the problem is `data_string`, try it without that, so: `curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));` – chiliNUT Oct 26 '18 at 07:01
  • also I dont think you need to set any other opts after you `curl_exec` – chiliNUT Oct 26 '18 at 07:02
  • @YuukkaLimson tried as you said, still getting same result.... –  Oct 26 '18 at 07:04
  • @chiliNUT updated as you said, still getting same result.... –  Oct 26 '18 at 07:04
  • @YuukkaLimson here is updated code , please check : https://pastebin.com/HCBp7FTU –  Oct 26 '18 at 07:06
  • @chiliNUT here is updated code , please check : https://pastebin.com/HCBp7FTU –  Oct 26 '18 at 07:06
  • @vickeycolors try using postman if your parameters are correct. also your seeing this option. https://stackoverflow.com/questions/12331224/how-to-include-authorization-header-in-curl-post-http-request-in-php – Julius Limson Oct 26 '18 at 07:17
  • @YuukkaLimson i did't used postman until now, i will try now with google help..... –  Oct 26 '18 at 07:21
  • @YuukkaLimson when i added this `curl_setopt($ch, CURLOPT_HEADER, true);` , i got this : `HTTP/1.0 400 Bad request Cache-Control: no-cache Connection: close Content-Type: text/html` –  Oct 26 '18 at 07:23
  • Are you sure the data is supposed to be JSON-encoded _twice_, as you are currently doing with the code shown above ...? – misorude Oct 29 '18 at 13:29
  • And where in the documentation you referred to does it even say that the whole thing should be passed as one single parameter named `Payload`? – misorude Oct 29 '18 at 13:36
  • @misorude please check `update 2` part in question..... –  Oct 30 '18 at 05:19

4 Answers4

1

When setting the headers you have a space between the header name and the separating colon. That results in Curl using two headers, a Content-Type : application/json header and a Content-Type: application/x-www-form-urlencoded header, which is the default when posting data. The server could identify the content type as "x-www-form-urlencoded" using the second header, however the first header would cause a 400 response, because there should be no space between the header and the colon. From rfc7230, section 3.2.4:

No whitespace is allowed between the header field-name and colon. In the past, differences in the handling of such whitespace have led to security vulnerabilities in request routing and response handling. A server MUST reject any received request message that contains whitespace between a header field-name and colon with a response code of 400 (Bad Request).

<!- -!>

Your code should return successful response using the headers below.

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
     "Authorization: Basic ZGFpbHlvcmRlcnM6ZHVtbf4fsww=", 
     "Content-Type: application/json"
));

When debugging such issues, it's best to enable the verbose mode which prints helpful information about the connection.

curl_setopt($ch, CURLOPT_VERBOSE, true);

In your Postman request you're submitting a HTTP_X_MERCHANT_CODE header; you should include that in your Curl request.

$headers = array(
    "Authorization: Basic ZGFpbHlvcmRlcnM6ZHVtbXlLZXk=",
    "Content-Type: application/json", 
    "HTTP_X_MERCHANT_CODE: DOC",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

This would fix the "X_Merchant_Code is null" error, but the server would return a new error message: "Expected BEGIN_OBJECT but was BEGIN_ARRAY", pointing at: ...shipment.shipment_items[0].item_content, because the json data does not have the required format. The item_content key is an empty array, which gets encoded as [], but the server expects a json object ({}). We can fix this issue if we use a null object.

$data['services'][0]['service_details'][0]['shipment']['shipment_items'][0]['item_content'] = (object)null;

The complete code:

<?php

$data = [
    "request_id" => "835",
    "client_name" => "DOC",
    "services" => [
        [
            "service_code" => "REGULAR",
            "service_details" => [
                [
                    "service_leg" => "FORWARD",
                    "service_data" => [
                        "vendor_name" => "Ekart",
                        "amount_to_collect" => 1000,
                        "dispatch_date" => "",
                        "customer_promise_date" => "",
                        "delivery_type" => "SMALL",
                        "source" => [
                            "address" => [
                                "first_name" => "AVThamizhmahan",
                                "address_line1" => "3/1675 ES Garden Vazhudaretty Post Villupuram",
                                "address_line2" => "Tamil Nadu",
                                "pincode" => "400066",
                                "city" => "Villupuram",
                                "state" => "Tamil Nadu",
                                "primary_contact_number" => "88888888"
                            ]
                        ],
                        "destination" => [
                            "address" => [
                                "first_name" => "AVThamizhmahan",
                                "address_line1" => "3/1675 ES Garden Vazhudaretty Post Villupuram",
                                "address_line2" => "Tamil Nadu",
                                "pincode" => "400066",
                                "city" => "Villupuram",
                                "state" => "Tamil Nadu",
                                "primary_contact_number" => "88888888"
                            ]
                        ],
                        "return_location" => [
                            "address" => [
                                "first_name" => "AVThamizhmahan",
                                "address_line1" => "3/1675 ES Garden Vazhudaretty Post Villupuram",
                                "address_line2" => "Tamil Nadu",
                                "pincode" => "400066",
                                "city" => "Villupuram",
                                "state" => "Tamil Nadu",
                                "primary_contact_number" => "88888888"
                            ]
                        ]
                    ],
                    "shipment" => [
                        "client_reference_id" => "ABCD",
                        "tracking_id" => "DOCC0000000001",
                        "shipment_value" => 2000,
                        "shipment_dimensions" => [
                            "length" => [
                                "value" => 1
                            ],
                            "breadth" => [
                                "value" => 1
                            ],
                            "height" => [
                                "value" => 1
                            ],
                            "weight" => [
                                "value" => 1
                            ]
                        ],
                        "return_label_desc_1" => "",
                        "return_label_desc_2" => "",
                        "shipment_items" => [
                            [
                                "product_id" => "134883075",
                                "item_content" => (object)null,
                                "category" => "Apparel",
                                "product_title" => "Mast&HarbourBlackHandbag => Handbags",
                                "quantity" => 1,
                                "cost" => [
                                    "total_sale_value" => 500,
                                    "total_tax_value" => 100
                                ],
                                "seller_details" => [
                                    "seller_reg_name" => "Seller Registered Legal Name",
                                    "vat_id" => "",
                                    "cst_id" => ""
                                ],
                                "legal_entity" => "",
                                "billable_entity" => "",
                                "cod_remittance_entity" => "",
                                "item_attributes" => [
                                    [
                                        "name" => "order_id",
                                        "value" => "ABCD"
                                    ],
                                    [
                                        "name" => "invoice_id",
                                        "value" => "INV1234"
                                    ],
                                    [
                                        "name" => "item_dimensions",
                                        "value" => "l:b:h:w"
                                    ],
                                    [
                                        "name" => "brand_name",
                                        "value" => "Peter England"
                                    ]
                                ],
                                "handling_attributes" => [
                                    [
                                        "name" => "isFragile",
                                        "value" => "false"
                                    ],
                                    [
                                        "name" => "isDangerous",
                                        "value" => "false"
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ]
        ]
    ]
];

$url = "https://staging.ekartlogistics.com/v2/shipments/create";
$data = json_encode($data);

$headers = array(
    "Content-Type: application/json", 
    "HTTP_X_MERCHANT_CODE: DOC",
);

$curl = curl_init($url);

curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "dailyorders:dummyKey"); //Your credentials goes here
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //IMP if the url has https and you don't want to verify source certificate
curl_setopt($curl, CURLOPT_VERBOSE, true);

$curl_response = curl_exec($curl);
curl_close($curl);
echo $curl_response ."\n";

?>
Community
  • 1
  • 1
t.m.adam
  • 15,106
  • 3
  • 32
  • 52
  • Thanks , yes its displayed 401, so i decoded and got username & password , i replaced above code by `curl_setopt($curl, CURLOPT_USERPWD, "dailyorders:dummyKey");` , now i am getting `{"response":[{"status":"REQUEST_REJECTED","message":["X_Merchant_Code is null "]}],"request_id":"b03e36cd-a9d8-4ac8-bb6d-0cc3b211a13d"}` , but i am passing `Merchant code` in parameter...... here is [link](http://sbdev1.kidsdial.com/ecom1/ekart.php) –  Oct 30 '18 at 06:09
  • I can't help much with that as I don't have the crdentials. – t.m.adam Oct 30 '18 at 06:11
  • I posted php code with staging credentials in question..... please try from localhost or in your server only...... –  Oct 30 '18 at 06:12
  • Ok, I'll give it a try, if you trust me, but please make sure to change your credentials as soon as possible. – t.m.adam Oct 30 '18 at 06:15
  • I think I've managed to get a successful response, I'll update my answer tomorrow :) – t.m.adam Oct 30 '18 at 21:10
  • I appreciate your help, thanks for your support, i also found solution :) –  Oct 31 '18 at 13:01
1

Passing Merchant key in proper form solved the issue....

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://staging.ekartlogistics.com/v2/shipments/create",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{\n  \"request_id\": 835,\n  \"client_name\": \"DOC\",\n  \"services\": [\n    {\n      \"service_code\": \"REGULAR\",\n      \"service_details\": [\n        {\n          \"service_leg\": \"FORWARD\",\n          \"service_data\": {\n            \"vendor_name\": \"Ekart\",\n            \"amount_to_collect\": \"5382.00\",\n            \"delivery_type\": \"SMALL\",\n            \"source\": {\n              \"address\": {\n                \"first_name\": \"Ebay\",\n                \"address_line1\": \"sqsq\",\n                \"address_line2\": \"gvhvh\",\n                \"pincode\": \"110037\",\n                \"city\": \"gvhbj\",\n                \"state\": \"Delhi\",\n                \"primary_contact_number\": \"9987654321\"\n              }\n            },\n            \"destination\": {\n              \"address\": {\n                \"first_name\": \"Dgwdh\",\n                \"address_line1\": \"C-238\",\n                \"address_line2\": \"Nagar, Amrut Nagar, Ghatkopar (W)\",\n                \"pincode\": \"560034\",\n                \"city\": \"Delhi\",\n                \"state\": \"South West Delhi\",\n                \"primary_contact_number\": \"9716632816\"\n              }\n            },\n            \"return_location\": {\n              \"address\": {\n                \"first_name\": \"Primasource Products and \",\n                \"address_line1\": \"No 99 4th Cross , Prithvi Layout\",\n                \"address_line2\": \"ECC Raod\",\n                \"pincode\": \"560034\",\n                \"city\": \"Bangalore\",\n                \"state\": \"Karnataka\",\n                \"primary_contact_number\": \"9987654321\"\n              }\n            }\n          },\n          \"shipment\": {\n            \"tracking_id\": \"DOCC0000000837\",\n            \"shipment_value\": 2000,\n            \"shipment_dimensions\": {\n              \"length\": {\n                \"value\": \"12\"\n              },\n              \"breadth\": {\n                \"value\": \"12\"\n              },\n              \"height\": {\n                \"value\": \"12\"\n              },\n              \"weight\": {\n                \"value\": \"1\"\n              }\n            },\n            \"return_label_desc_1\": \"\",\n            \"return_label_desc_2\": \"\",\n            \"shipment_items\": [\n              {\n                \"product_id\": \"895\",\n                \"product_title\": \"dwdwd\",\n                \"quantity\": 23,\n                \"cost\": {\n                  \"total_sale_value\": \"5382.00\",\n                  \"total_tax_value\": 5382,\n                  \"tax_breakup\": {\n                    \"cgst\": 0,\n                    \"sgst\": 0,\n                    \"igst\": 0\n                  }\n                },\n                \"seller_details\": {\n                  \"seller_reg_name\": \"Ebay\",\n                  \"vat_id\": \"\",\n                  \"cst_id\": \"\",\n                  \"tin_id\": \"\",\n                  \"gstin_id\": null\n                },\n                \"hsn\": \"123456\",\n                \"ern\": \"\",\n                \"discount\": \"0.00\",\n                \"item_attributes\": [\n                  {\n                    \"name\": \"order_id\",\n                    \"value\": 835\n                  },\n                  {\n                    \"name\": \"invoice_id\",\n                    \"value\": \"NA\"\n                  },\n                  {\n                    \"name\": \"item_dimensions\",\n                    \"value\": \":\"\n                  },\n                  {\n                    \"name\": \"brand_name\",\n                    \"value\": null\n                  }\n                ]\n              }\n            ]\n          }\n        }\n      ]\n    }\n  ]\n}",
  CURLOPT_HTTPHEADER => array(
    "Content-Type: application/json",
    "HTTP_X_MERCHANT_CODE: DOC",
    "Authorization: Basic ZGFpbHlvcmRlcnM6ZHVtbXlLZXk=",
    "Postman-Token: bf0fd969-0618-432a-a222-d4a67bff6cd2",
    "cache-control: no-cache"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}
0

It looks like in your JSON array you have an error :

“vendor_name”:”Ekart”,

Is not quoted correctly :

"vendor_name":"Ekart",

Should work better.

There are a few more errors like this. You should use a JSON editor to check your JSON array.

Jean-Marc Zimmer
  • 537
  • 6
  • 20
  • sorry, that was typo in question, thats not the problem, i updated question now.... –  Oct 26 '18 at 06:36
  • OK, sorry. I also edited ^^ I'll edit this answer if I find some solution. – Jean-Marc Zimmer Oct 26 '18 at 06:37
  • do i need to pass in `json format` in php instead of `array` ? –  Oct 26 '18 at 06:53
  • thanks for suggestion , please tell me how to use json through http request in my case, what code i need to update ? –  Oct 26 '18 at 07:28
  • The HTTP request here is managed by `curl`. The idea is to `json_encode()` data before sending -what you already do-, then decode it to use it in the request management function. It's easier because if you want, for example, to call the same server function through an AJAX HTTP request, you'll _have_ to use JSON. – Jean-Marc Zimmer Oct 26 '18 at 07:43
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/182565/discussion-between-vickey-colors-and-jean-marc-zimmer). –  Oct 26 '18 at 07:45
0

assuming that your params are correct.

try:

echo $data=json_encode($data);
//$data_string = json_encode(array(
//    "Payload" => $data
//));
   $username = "";
   $password = "";
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);     
    curl_setopt($ch, CURLOPT_HEADER, true); 
    //curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    //    "Authorization : yourtoken",
    //));
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
         "Content-Type : application/json"
    ));
    curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);//since you are using basic auth  
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $return = curl_exec($ch);

    curl_close ($ch);
Julius Limson
  • 526
  • 9
  • 29