404 bad request in php curl - php

I have a method for to send the data to API. I test data in postman. It is working ok. But in the project it is not working. But I copied the method another project it is working here. How can I solve the problem?
function send_candidates_f2()
{
$my_json = '
{
"typeObj": "1",
"tin": "305050590",
"pinfl": "asas",
"tinName": "IQTIDOR ZIYOSI НТМ",
"ns1Code": "14",
"ns3Code": "4",
"ns41Code": "33",
"ns42Code": "1",
"ns10Code": "14",
"ns11Code": "9",
"ns10Code_1": "14",
"ns11Code_1": "9",
"f2Num": 1,
"f2Date": "23.01.2023",
"rukName": "ERGASHEV JAVOHIR ALISHEROVICH",
"buhName": "НЕ ПPЕДУСМОТPЕН",
"nprk": "",
"dateprk": "",
"prvdbeg": "23.01.2023",
"prvdend": "23.01.2023",
"otdel": "1",
"fabula": "sdsdf",
"violationCode1": "5",
"tin2": "420540289",
"year": "2023",
"pkey": "F2ECC0F41C123142E053DE3A18ACF835",
"na3Code": "D0515486CD830030E043C0A8502B15AA",
"files": [
"xs_buyruq_ilovasi.pdf"
],
"fileAct": "F2ECC0F41C133142E053DE3A18ACF835",
"confirmedBy": "NARKULOVA MARXAMAT YUSUPOVNA",
"enteredBy": "NARKULOVA MARXAMAT YUSUPOVNA",
"objectId": "1",
"appType": "Сайёр солик текширувда ноконуний тадбиркорлик(рейд)"
}';
// var_dump($my_json); die();
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://dev-tax-offenses-api.soliq.local/tax-offence-api/apray/set-apray-candidate",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 86400,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $my_json,
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/json"
),
));
$response = curl_exec($curl);
curl_close($curl);
var_dump($response); die();
}

Related

Getting 401 unauthorzed error while creating order with Shipstation REST API and PHP

I am trying to connect Shipstation with my website using REST API and creating order which can be added to manual store inside Shipstation by default. However, I am getting error 401 unauthorised continuously, even I copied API key and API secret from Shipstation account and passed its base64 form to Authorization header.
Here is my Simple Post Request :
$api_key = 'SS_KEY';
$api_secret = 'SS_SECRET';
$token = base64_encode($api_key.':'.$api_secret);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://ssapi.shipstation.com/orders/createorder/',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"orderNumber": "TEST-ORDER-API-DOCS",
"orderStatus": "awaiting_shipment",
"customerUsername": "abc",
"customerEmail": "headhoncho#whitehouse.gov",
"billTo": {
"name": "Inam Ali",
"company": "abc",
"street1": "1",
"street2": null,
"street3": null,
"city": "Pesh",
"state": "KPK",
"postalCode": 25000,
"country": "Pakistan",
"phone": "0315333333",
"residential": true
},
"shipTo": {
"name": "Inam Ali",
"company": "US Govt",
"street1": "1600 Pennsylvania Ave",
"street2": "Oval Office",
"street3": null,
"city": "Washington",
"state": "DC",
"postalCode": "20500",
"country": "US",
"phone": "555-555-5555",
"residential": true
},
"items": [
{
"lineItemKey": "vd08-MSLbtx",
"name": "Test item #1",
"imageUrl": null,
"quantity": 2,
"unitPrice": 99.99,
"taxAmount": 2.5,
}
],
"amountPaid": 218.73,
"taxAmount": 5,
"tagIds": [
53974
]
}',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'host: ssapi.shipstation.com',
'Authorization: Basic '.$token
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;

flipkart shipment API /v3/shipments/filter/ Not getting valid response

I am developing code to generate labels for Flipkart orders which orders I am getting from Flipkart orders API. while calling this URL https://api.flipkart.net/sellers/v3/shipments/labels, I am getting a response invalid JSON. Below code that I tried. Anyone who developed Flipkart API and face this issue before kindly help.
I refer Flipkart API Docs https://seller.flipkart.com/api-docs/fmsapi_index.html
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.flipkart.net/sellers/v3/shipments/labels',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"shipments": [
{
"shipmentId": "8f68421b-37cc-4862-aeda-52aeefe121811a6",
"locationId": "",
"taxItems": [
{
"orderItemId": "12406434662285585103",
"taxRate": 5.0,
"quantity": 1
}
],"serialNumbers": [
{
"orderItemId": "12406434662285585103",
"serialNumbers": [
[]
]
}
],
"invoices": [
{
"shipmentId": "8f68421b-37cc-4862-aeda-52aeefe121811a6",
"invoiceDate": "2022-02-02",
"invoiceNumber": "",
"orderItems": [
{
"orderItemId": "12406434662285585103",
"invoiceAmount": 939.00,
"taxRate": 5.0,
"serialNumbers": [
[]
],
"taxDetails": {
"cgstRate": 0.0,
"sgstRate": 0.0,
"igstRate": 5.0
}
}
]
}
],
"subShipments": [
{
"subShipmentId": "SS-1",
"dimensions": {
"length": 25,
"breadth": 20,
"height": 5,
"weight": 0.3
}
}
]
}
]
}',
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer 89',
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
**Response**
{
"code": 400,
"message": "Unable to process JSON"
}

POST JSON Body with CURL in PHP

I have running code like the following
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://chat-service.com/api/direct',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_POSTFIELDS =>'{
"to_number": "62111111111",
"to_name": "Mr X",
"message_template_id": "abc123abc123",
"channel_integration_id": "123abc123abc",
"language": {
"code": "id"
},
"parameters": {
"body": [
{
"key": "1",
"value": "name",
"value_text": "Mr X"
},
{
"key": "2",
"value": "vehiclereg",
"value_text": "L0001X"
},
{
"key": "3",
"value": "date",
"value_text": "29 Feb 2022"
}
]
}
}
',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Authorization: Bearer 1122334455',
'target_channel: trial'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
?>
now i'd like to store CURLOPT_POSTFIELDS value as variable
and this is my code
but stil not running
<?php
$curl = curl_init();
$data = array
(
"to_number"=>'62111111111',
"to_name"=>'Mr X',
"message_template_id"=>'abc123abc123',
"channel_integration_id"=>'123abc123abc',
"language"=>
array
(
"code"=>"id"
),
"parameters"=>
array
(
"body"=>array
(
"key"=>"1",
"value"=>"name",
"value_text"=>"Mr X"
),
"body"=>array
(
"key"=>"2",
"value"=>"vehiclereg",
"value_text"=>"L0001X"
),
"body"=>array
(
"key"=>"3",
"value"=>"date",
"value_text"=>"29 Feb 2022"
)
)
);
curl_setopt_array($curl, array(
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Authorization: Bearer 1122334455',
'target_channel: trial'
),
CURLOPT_URL => 'https://chat-service.com/api/direct',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_POSTFIELDS => http_build_query($data);
));
$response = curl_exec($curl);
curl_close($curl);
echo "<pre>";
print_r($response)
?>
In my opininion this line is the suspect why the code not working but still don't know how to fix it
Running one
"parameters": {
"body": [
{
"key": "1",
"value": "name",
"value_text": "Mr X"
},
{
"key": "2",
"value": "vehiclereg",
"value_text": "L0001X"
},
{
"key": "3",
"value": "date",
"value_text": "29 Feb 2022"
}
]
}
Not Running
"parameters"=>
array
(
"body"=>array
(
"key"=>"1",
"value"=>"name",
"value_text"=>"Mr X"
),
"body"=>array
(
"key"=>"2",
"value"=>"vehiclereg",
"value_text"=>"L0001X"
),
"body"=>array
(
"key"=>"3",
"value"=>"date",
"value_text"=>"29 Feb 2022"
)
)
I tried googling it everywhere
find some similar case like on this one
send post json with php (curl)
but still can fix it
i did encode like this
<?php
$data = array
(
"to_number"=>'62111111111',
"to_name"=>'Mr X',
"message_template_id"=>'abc123abc123',
"channel_integration_id"=>'123abc123abc',
"language"=>
array
(
"code"=>"id"
),
"parameters"=>
array
(
"body"=>array
(
"key"=>"1",
"value"=>"name",
"value_text"=>"Mr X"
),
"body"=>array
(
"key"=>"2",
"value"=>"vehiclereg",
"value_text"=>"L0001X"
),
"body"=>array
(
"key"=>"3",
"value"=>"date",
"value_text"=>"29 Feb 2022"
)
)
);
echo json_encode($data);
?>
and this is the result
{
"to_number": "62111111111",
"to_name": "Mr X",
"message_template_id": "abc123abc123",
"channel_integration_id": "123abc123abc",
"language": {
"code": "id"
},
"parameters": {
"body": {
"key": "3",
"value": "date",
"value_text": "29 Feb 2022"
}
}
}
Try use
CURLOPT_POSTFIELDS => json_encode($data);
Reference
How to POST JSON Data With PHP cURL?
Finally found the solution based on trial error and googling everywhere
<?php
$curl = curl_init();
$data =
[
"to_number"=>'62111111111',
"to_name"=>'Mr X',
"message_template_id"=>'abc123abc123',
"channel_integration_id"=>'123abc123abc',
"language"=>
[
"code"=>"id"
],
"parameters"=>
[
"body" =>
[
[
"key"=>"1",
"value"=>"name",
"value_text"=>"Mr X"
],
[
"key"=>"2",
"value"=>"vehiclereg",
"value_text"=>"L0001X"
],
[
"key"=>"3",
"value"=>"date",
"value_text"=>"29 Februari 2022"
]
]
]
];
curl_setopt_array($curl, array(
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Authorization: Bearer 1122334455',
'target_channel: tria;'
),
CURLOPT_URL => 'https://chat-service.com/api/direct',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_POSTFIELDS => json_encode($data)
));
$response = curl_exec($curl);
curl_close($curl);
echo "<pre>";
print_r($response);
?>
thank you stack overflow for inspiring

Random Characters PHP Decode Nested JSON

Really struggling with this one. I have connected to the API fine and can get the raw JSON Data.
But when I am trying to decode it I just get a load of random characters.
The JSON path should be response->data->fieldData->Case_ID
There are other fields there also but I have hidden them to make it easier to read.
This is my JSON
{
"response": {
"dataInfo": {
"database": "MicroLog",
"layout": "External_API_Cases",
"table": "Cases",
"totalRecordCount": 143691,
"foundCount": 23,
"returnedCount": 2
},
"data": [
{
"fieldData": {
"Case_ID": 139220,
},
"portalData": {},
"recordId": "139261",
"modId": "138"
},
{
"fieldData": {
"Case_ID": 143421,
},
"portalData": {},
"recordId": "143462",
"modId": "25"
}
]
},
"messages": [
{
"code": "0",
"message": "OK"
}
]
}
The code I have been using.
<?php
include('login.php');
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'URL_HERE',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"query": [
{
"Closed": "1",
"Closed_TimeStamp": "06/24/2021"
}
],
"limit": 2
}',
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer '.$token.'',
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
$decode = json_decode($response,true);
foreach ($decode['response']['data'] as $dec) {
foreach ($dec['fieldData'] as $cd) {
echo $cd['Case_ID'];
}
echo "<br/>";
}
?>
This is what it returns
188B7162M111662J>41ISS1AM1200000000000J5D
118A7162I11162J�62ISS1AM120000000000J5D

$body->append(); from POSTMAN API, how to replace it with variables?

Given this generated code from postman, $body->append(); from POSTMAN API, how to replace it with variables?
how to replace the CURLOPT_POSTFIELDS with php variables? so it can be filled with dynamic variable
what is the best practices to do it?
thank you
<?php
$json = '{
"RateRequest": {
"ClientDetails" : null,
"RequestedShipment": {
"DropOffType": "REQUEST_COURIER",
"ShipTimestamp": "2020-08-25T11:00:00GMT+02:00",
"UnitOfMeasurement": "SI",
"Content": "NON_DOCUMENTS",
"PaymentInfo": "DAP",
"NextBusinessDay": "Y",
"Account": "54xxxxxx",
"Ship": {
"Shipper": {
"City": "city",
"PostalCode": 123456,
"CountryCode": "ID"
},
"Recipient" : {
"City" : "canberra",
"PostalCode" : 2601,
"CountryCode" : "AU"
}
},
"Packages": {
"RequestedPackages": {
"#number": "1",
"Weight": {
"Value": 8.82
},
"Dimensions": {
"Length": 4.33,
"Width": 4.33,
"Height": 4.33
}
}
}
}
}
}';
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://wsbexpress.dhl.com/rest/sndpt/RateRequest",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $json,
CURLOPT_HTTPHEADER => array(
"Authorization: Basic YmF0dWxheWFuZ3NJRDpKXjBlUEA2dkQhM2Y=",
"Content-Type: application/json",
"Cookie: BIGipServer~WSB~pl_wsb-express-cbj.dhl.com_443=293349575.64288.0000"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
json_decode the string into an array
modify the values as needed
set that data for CURLOPT_POSTFIELDS
Something like this
$json = '{
"RateRequest": {
"ClientDetails" : null,
"RequestedShipment": {
"DropOffType": "REQUEST_COURIER",
"ShipTimestamp": "2020-08-25T11:00:00GMT+02:00",
"UnitOfMeasurement": "SI",
"Content": "NON_DOCUMENTS",
"PaymentInfo": "DAP",
"NextBusinessDay": "Y",
"Account": "54xxxxxx",
"Ship": {
"Shipper": {
"City": "city",
"PostalCode": 123456,
"CountryCode": "ID"
},
"Recipient" : {
"City" : "canberra",
"PostalCode" : 2601,
"CountryCode" : "AU"
}
},
"Packages": {
"RequestedPackages": {
"#number": "1",
"Weight": {
"Value": 8.82
},
"Dimensions": {
"Length": 4.33,
"Width": 4.33,
"Height": 4.33
}
}
}
}
}
}';
$data = json_decode($json, true);
if (isset($data['RateRequest']['RequestedShipment']['DropOffType'])) {
$data['RateRequest']['RequestedShipment']['DropOffType'] = 'some new value';
}
//... later in your curl call...
CURLOPT_POSTFIELDS => $data,

Categories