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
Related
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();
}
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"
}
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
I must send a post request with the following json data structure using PHP and cURL:
{
"assistanceRequest":
{
"type": "TECHNICAL_DATA",
"deliveryMode": "NORMAL",
"creationDate": "2020-04-09T10:09:00+02:00",
"source": "ATD",
"language": "FR",
"country": "FR"
},
"customer":
{
"code": 123456,
"login": "client#company.com",
"companyName": "Workshop Harris",
"phoneNumber": "+44 123456789",
"email": "workshop#company.com"
},
}
However I don't understand how to create a similar request. I tried with the following code but I suppose it's not correct because I should group the data in two blocks:
$post = [
'type' => 'TECHNICAL_DATA',
'deliveryMode' => 'NORMAL',
'creationDate' => '2020-04-16T12:46:33+02:00',
'source' => 'ATD',
'language' => 'FR',
'country' => 'CH',
'code' => '123456',
'login' => 'FR1234561A',
'companyName' => 'Workshop Harris Love',
'phoneNumber' => '+390432561543',
'email' => 'client#client.com', ];
$post_encoded = json_encode($post);
$ch = curl_init('http://222.333.333.444/opafffws/public/api/req');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_encoded);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($post_encoded))
);
$response = curl_exec($ch);
echo "The response 1: <br>" . $response;
echo "<br>";
curl_close($ch);
Can help?
<?php
$data = [
'assistanceRequest' => [
'type' => 'TECHNICAL_DATA',
'deliveryMode' => 'NORMAL',
"creationDate" => "2020-04-09T10:09:00+02:00",
"source" => "ATD",
"language" => "FR",
"country" => "FR"
],
'customer' => [
"code" => 123456,
"login" => "client#company.com",
"companyName" => "Workshop Harris",
"phoneNumber" => "+44 123456789",
"email" => "workshop#company.com"
],
];
echo "<pre>"; print_r(json_encode($data));
This is an example for your structure. Tweak it as you need.
The output of the above code will be:
{
"assistanceRequest": {
"type": "TECHNICAL_DATA",
"deliveryMode": "NORMAL",
"creationDate": "2020-04-09T10:09:00+02:00",
"source": "ATD",
"language": "FR",
"country": "FR"
},
"customer": {
"code": 123456,
"login": "client#company.com",
"companyName": "Workshop Harris",
"phoneNumber": "+44 123456789",
"email": "workshop#company.com"
}
}
world! I'm having some troubles with my API script with OLX. Can someone help me, please?
I'm making a JSON that returns this:
{
"access_token": "7b1cb35b4eca28be89174d75baf66e00d2728bf6",
"ad_list": [
{
"id": "31dasa1d23",
"operation": "insert",
"category": 1040,
"subject": "Título teste - Anúncio OLX - Marília/SP",
"body": "Imóvel com Sala, Copa, Cozinha planejada e Gabinete, 3 Dormitórios sendo 1 suíte, Lavanderia com Área de Serviço Coberta, Espaço Gourmet com Churrasqueira e Chuveirão para Ducha, Garagem para 2 Carros, Cerca Elétrica e Interfone. Confira mais sobre o imóvel: http://www.silvioimoveis.com.br/imovel-residencial-professor-antonio-da-silva-penteado-marilia-sp-venda/principal/imovel/principal/5394",
"phone": 1433333333,
"type": "s",
"price": 260000,
"zipcode": "17504000",
"params":{
"rooms": "3",
"garage_spaces": "2"
},
"images": [
"http://homologacao.silvioimoveis.com.br/fotos/imoveis_internos/5394/2175ea284f81f03.jpg",
"http://homologacao.silvioimoveis.com.br/fotos/imoveis_internos/5394/bb0ff30ac2bcbae.jpg"
]
}
]
}
And my code to create this JSON is:
$json = '{
"access_token": "'.$data['access_token'].'",
"ad_list": [
{
"id": "'.$jsonGerado[0]['id'].'",
"operation": "'.$jsonGerado[0]['operation'].'",
"category": '.$jsonGerado[0]['category'].',
"subject": "'.$jsonGerado[0]['subject'].'",
"body": "'.$jsonGerado[0]['body'].'",
"phone": '.$jsonGerado[0]['phone'].',
"type": "'.$jsonGerado[0]['type'].'",
"price": '.$jsonGerado[0]['price'].',
"zipcode": "'.$jsonGerado[0]['zipcode'].'",
"params": {
"rooms": "'.$jsonGerado[0]['params']['rooms'].'",
"garage_spaces": "'.$jsonGerado[0]['params']['garage_spaces'].'"
},
"images": [
'.$jsonGerado[0]['images']['linkFotoPrincipal'].',
'.$jsonGerado[0]['images']['linkDemaisFotos'][0].',
'.$jsonGerado[0]['images']['linkDemaisFotos'][1].',
'.$jsonGerado[0]['images']['linkDemaisFotos'][2].',
'.$jsonGerado[0]['images']['linkDemaisFotos'][3].',
'.$jsonGerado[0]['images']['linkDemaisFotos'][4].',
'.$jsonGerado[0]['images']['linkDemaisFotos'][5].',
'.$jsonGerado[0]['images']['linkDemaisFotos'][6].',
'.$jsonGerado[0]['images']['linkDemaisFotos'][7].',
'.$jsonGerado[0]['images']['linkDemaisFotos'][8].',
'.$jsonGerado[0]['images']['linkDemaisFotos'][9].'
]
}
]
}';
The CURL code is:
curl_setopt_array($curl, array(
CURLOPT_URL => "https://apps.olx.com.br/autoupload/import",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => $json
));
And finally, the error is:
When I don't use json_encode() in $json variable, I got "NULL value found..." instead of "string value found"
As RiggsFolly mentioned, use json_encode on a PHP array or object rather than building the JSON yourself. Here's an example of how you might do that with the example code you provided:
$json_array = array(
"access_token" => $data['access_token'],
"ad_list" => array(
array(
"id" => $jsonGerado[0]['id'],
"operation" => $jsonGerado[0]['operation'],
"category" => $jsonGerado[0]['category'],
"subject" => $jsonGerado[0]['subject'],
"body" => $jsonGerado[0]['body'],
"phone" => $jsonGerado[0]['phone'],
"type" => $jsonGerado[0]['type'],
"price" => $jsonGerado[0]['price'],
"zipcode" => $jsonGerado[0]['zipcode'],
"params" => array(
"rooms" => $jsonGerado[0]['params']['rooms'],
"garage_spaces" => $jsonGerado[0]['params']['garage_spaces']
),
"images" => array(
$jsonGerado[0]['images']['linkFotoPrincipal'],
$jsonGerado[0]['images']['linkDemaisFotos'][0],
$jsonGerado[0]['images']['linkDemaisFotos'][1],
$jsonGerado[0]['images']['linkDemaisFotos'][2],
$jsonGerado[0]['images']['linkDemaisFotos'][3],
$jsonGerado[0]['images']['linkDemaisFotos'][4],
$jsonGerado[0]['images']['linkDemaisFotos'][5],
$jsonGerado[0]['images']['linkDemaisFotos'][6],
$jsonGerado[0]['images']['linkDemaisFotos'][7],
$jsonGerado[0]['images']['linkDemaisFotos'][8],
$jsonGerado[0]['images']['linkDemaisFotos'][9]
)
)
)
);
You can then do $json = json_encode($json_array);. I hope that helps!