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
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"
}
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,
I'm Having issues grabbing values in a multi level array.
This is my JSON I am grabbing with CURL and putting into a variable.
{
"id": 454626,
"results": [
{
"iso_3166_1": "SK",
"release_dates": [
{
"certification": "U",
"iso_639_1": "sk",
"note": "",
"release_date": "2020-02-20T00:00:00.000Z",
"type": 3
}
]
},
{
"iso_3166_1": "DE",
"release_dates": [
{
"certification": "6",
"iso_639_1": "",
"note": "",
"release_date": "2020-02-13T00:00:00.000Z",
"type": 3
}
]
},
{
"iso_3166_1": "TW",
"release_dates": [
{
"certification": "G",
"iso_639_1": "",
"note": "",
"release_date": "2020-02-21T00:00:00.000Z",
"type": 3
}
]
}
]
}
This is my PHP code that I'm having issues with.
$id_tmdb = $row[id_tmdb];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.themoviedb.org/3/movie/$id_tmdb/release_dates?api_key=API-KEY",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET"
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
$result = json_decode($response, true);
foreach($result[results][release_dates] as $key=>$val){
echo "$val[release_date]";
}
I'm trying to get the release_date but it just shows blank.
I have another page that is formatted the same way so I am unsure why it isn't working.... Thank you for all your help.
Your $result['results'] value is also an array and you need to iterate over that as well as the release_dates array:
foreach($result['results'] as $results) {
foreach ($results['release_dates'] as $val){
echo "$val[release_date]\n";
}
}
Output (for the sample JSON provided):
2020-02-20T00:00:00.000Z
2020-02-13T00:00:00.000Z
2020-02-21T00:00:00.000Z
Demo on 3v4l.org
This should get you there:
foreach($result['results'] as $res){
echo $res['release_dates'][0]['release_date']."\n";
}
3v4l link
On a side-note, I put the array keys in quotes to prevent php warnings.
I have a curl page that output in json format but I can't get json reading from android correctly. I want Json format with just an array only not object and array together. Check the output at the bottom for example. I want to write a php code that encode json that matches below. How would i do that? Thanks.
PHP FILE:
<?php
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/vendor/mashape/unirest-php/src/Unirest.php';
//$response = Unirest\Request::get("",
// array(
// "host" => "",
// "key" => "
// )
//);
//
//
//echo stripslashes(json_encode($response));
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "json",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_HTTPHEADER, array('Accept: application/json'),
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"host: ",
"KEY: "
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
$json = json_encode($response, JSON_UNESCAPED_SLASHES);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo stripslashes($response);
}
$fp = fopen('results.json', 'w');
fwrite($fp, stripslashes($response));
fclose($fp);
ORIGINAL OUTPUT FROM CURL:
{
"quoteResponse":
{
"result": [{
"language": "en-US",
"region": "US",
"quoteType": "EQUITY",
"quoteSourceName": "Nasdaq Real Time Price",
"exchangeDataDelayedBy": 0,
"regularMarketPrice": 30.69,
"regularMarketTime": 1568404801,
"regularMarketChange": 0.48000145,
"regularMarketVolume": 45547913,
"shortName": "Advanced Micro Devices, Inc.",
"priceHint": 2,
"postMarketChangePercent": 0.0325846,
"postMarketTime": 1568419190,
"postMarketPrice": 30.7,
"postMarketChange": 0.0100002,
"regularMarketChangePercent": 1.5888827,
"regularMarketPreviousClose": 30.21,
"fullExchangeName": "NasdaqGS",
"longName": "Advanced Micro Devices, Inc.",
"market": "us_market",
"pageViews": {
"shortTermTrend": "UP",
"midTermTrend": "UP",
"longTermTrend": "UP"
},
"marketState": "CLOSED",
"exchange": "NMS",
"sourceInterval": 15,
"exchangeTimezoneName": "America/New_York",
"exchangeTimezoneShortName": "EDT",
"gmtOffSetMilliseconds": -14400000,
"esgPopulated": false,
"tradeable": true,
"triggerable": true,
"symbol": "AMD"
}],
"error": null
}
}
THE OUTPUT I WANT IS THIS WITHOUT quoteResponse LIKE ABOVE OUTPUT. CHECK BELOW :
{
"result": [{
"language": "en-US",
"region": "US",
"quoteType": "EQUITY",
"quoteSourceName": "Nasdaq Real Time Price",
"regularMarketPrice": 30.69,
"regularMarketTime": 1568404801,
"regularMarketChange": 0.48000145,
"regularMarketVolume": 45547913,
"sourceInterval": 15,
"exchangeTimezoneName": "America/New_York",
"exchangeTimezoneShortName": "EDT",
"pageViews": {
"shortTermTrend": "UP",
"midTermTrend": "UP",
"longTermTrend": "UP"
},
"gmtOffSetMilliseconds": -14400000,
"market": "us_market",
"postMarketChangePercent": 0.0325846,
"postMarketTime": 1568419190,
"postMarketPrice": 30.7,
"postMarketChange": 0.0100002,
"regularMarketChangePercent": 1.5888827,
"priceHint": 2,
"shortName": "Advanced Micro Devices, Inc.",
"regularMarketPreviousClose": 30.21,
"fullExchangeName": "NasdaqGS",
"longName": "Advanced Micro Devices, Inc.",
"exchange": "NMS",
"exchangeDataDelayedBy": 0,
"esgPopulated": false,
"tradeable": true,
"triggerable": true,
"marketState": "CLOSED",
"symbol": "AMD"
}],
"error": null
}
Decode your JSON, get quoteResponse from it and encode it again.
$json = json_encode(json_decode($json)->quoteResponse);