Fedex MPS integration in php - php

$client = new SoapClient($path_to_wsdl, ['trace' => 1]);
$this->packageLineItem = [['SequenceNumber'=>1,
'GroupPackageCount' => 1,
'MasterTrackingId' => '123123123123',
'TotalShipmentWeight' =>['Value' => '256', 'Units' => 'LB'],
'Weight' => ['Value' => 1, 'Units' => 'LB'],
],
['SequenceNumber'=>2,
'GroupPackageCount' => 1,
'MasterTrackingId' => '123123123123',
'TotalShipmentWeight' =>['Value' => '256', 'Units' => 'LB'],
'Weight' => ['Value' => 1, 'Units' => 'LB'],
],
['SequenceNumber'=>3,
'GroupPackageCount' => 1,
'MasterTrackingId' => '123123123123',
'TotalShipmentWeight' =>['Value' => '256', 'Units' => 'LB'],
'Weight' => ['Value' => 1, 'Units' => 'LB'],
],
['SequenceNumber'=>4,
'GroupPackageCount' => 1,
'MasterTrackingId' => '123123123123',
'TotalShipmentWeight' =>['Value' => '256', 'Units' => 'LB'],
'Weight' => ['Value' => 1, 'Units' => 'LB'],
]
];
$i = 0;
foreach ($this->packageLineItem as $key => $value)
{
$request = [
'WebAuthenticationDetail' => $this->WebAuthentication,
'ClientDetail' => $this->ClientDetail,
'TransactionDetail' => $this->TransactionDetail,
'Version' => $this->Version,
'RequestedShipment' => [
'ShipTimestamp' => date('c'),
'DropoffType' => 'REGULAR_PICKUP',
'ServiceType' => $ServiceType,
'PackagingType' => 'YOUR_PACKAGING',
'Shipper' => $this->Shipper,
'Recipient' => $this->Recipient,
'ShippingChargesPayment' => $this->ShippingChargesPayment,
'LabelSpecification' => $this->LabelSpecification,
'PackageCount' => 10,
'MasterTrackingId' => '123123123123',
'PackageDetail' => 'INDIVIDUAL_PACKAGES',
'RequestedPackageLineItems' => $value
]
];
print_r($request);
$response = $client->processShipment($request);
print_r($response);
$i++;
}
This code only works for the first piece of the shipment. It prints one label but after that it gives me error "Master Tracking ID invalid". I need to print label for each piece of that shipment. Does any one has any idea about that?

Related

Filter and re-format a given multi-dimensional array (php)

I have been given a dataset by my tutor and asked to carry out the following:
// make a function
// get skus of which the figure of the average monthly sales unit is equal or greater than 350
// array in the following format:
[
'id' => 11102,
'sku' => 'TEST_3',
'alternates' => [
'asin' => [
'code' => '50',
'value' => 'JL1235',
],
'barcode' => [
'code' => '10',
'value' => 'JS160694',
],
],
'commodityCode' => '9989898889',
'price' => [
'value' => 145.99,
],
'instructions' => [
'picking' => [
'code' => 'PICK',
'value' => 'I\'VE JUST HAD AN UNHAPPY LOVE AFFAIR, SO I DON\'T SEE WHY ANYBODY ELSE SHOULD HAVE A GOOD TIME.',
],
],
'quantity' => [
'inner' => 100,
'masterCarton' => 200,
'pallet' => 300,
],
'averageMonthlyUnitSales' => 100,
'created_at' => '2022-03-13 04:14:12'
],
Example dataset is here:
$data = [
[
'id' => '9947',
'sku' => 'test_1',
'asin_code' => '50',
'asin_value' => '',
'barcode' => '10',
'barcode_value' => 'js160694',
'commodityCode' => '9989898889',
'price' => '120.50',
'picking_instruction_code' => 'PICK',
'picking_instruction_value' => '',
'qty_inner' => '120',
'qty_masterCarton' => '200',
'qty_pallet' => '1200',
'averageMonthlyUnitSales' => '750',
'created_at' => '2019-02-23T01:54:14.957299+00:00'
],
[
'id' => '10921',
'sku' => 'test_2',
'asin_code' => '50',
'asin_value' => 'bx12345',
'barcode' => '10',
'barcode_value' => 'jb170931',
'commodityCode' => '9989898889',
'price' => '20.59',
'picking_instruction_code' => 'PICK',
'picking_instruction_value' => 'It\'s only half completed, I\'m afraid',
'qty_inner' => '70',
'qty_masterCarton' => '250',
'qty_pallet' => '270',
'averageMonthlyUnitSales' => '120',
'created_at' => '2021-12-23T11:41:31.193982+00:00'
],
[
'id' => '11102',
'sku' => 'test_3',
'asin_code' => '50',
'asin_value' => 'jl1235',
'barcode' => '10',
'barcode_value' => 'js160694',
'commodityCode' => '9989898889',
'price' => '145.99',
'picking_instruction_code' => 'PICK',
'picking_instruction_value' => 'I\'ve just had an unhappy love affair, so I don\'t see why anybody else should have a good time.',
'qty_inner' => '100',
'qty_masterCarton' => '200',
'qty_pallet' => '300',
'averageMonthlyUnitSales' => '100',
'created_at' => '2022-03-13T04:14:12.11.093745 +00:00'
],
];
I can perform the first part of the assignment (filter for averageMonthlyUnitSales>350) by carrying out something like:
$filtered_array= array_filter($data, function($item){
return ($item['averageMonthlyUnitSales']>350);
});
But I am not too sure how I can go about getting a new array in the required format?
The only way I can see to simply do both is as follows.
$data = [
[
'id' => '9947',
'sku' => 'test_1',
'asin_code' => '50',
'asin_value' => '',
'barcode' => '10',
'barcode_value' => 'js160694',
'commodityCode' => '9989898889',
'price' => '120.50',
'picking_instruction_code' => 'PICK',
'picking_instruction_value' => '',
'qty_inner' => '120',
'qty_masterCarton' => '200',
'qty_pallet' => '1200',
'averageMonthlyUnitSales' => '750',
'created_at' => '2019-02-23T01:54:14.957299+00:00'
],
[
'id' => '10921',
'sku' => 'test_2',
'asin_code' => '50',
'asin_value' => 'bx12345',
'barcode' => '10',
'barcode_value' => 'jb170931',
'commodityCode' => '9989898889',
'price' => '20.59',
'picking_instruction_code' => 'PICK',
'picking_instruction_value' => 'It\'s only half completed, I\'m afraid',
'qty_inner' => '70',
'qty_masterCarton' => '250',
'qty_pallet' => '270',
'averageMonthlyUnitSales' => '120',
'created_at' => '2021-12-23T11:41:31.193982+00:00'
],
[
'id' => '11102',
'sku' => 'test_3',
'asin_code' => '50',
'asin_value' => 'jl1235',
'barcode' => '10',
'barcode_value' => 'js160694',
'commodityCode' => '9989898889',
'price' => '145.99',
'picking_instruction_code' => 'PICK',
'picking_instruction_value' => 'I\'ve just had an unhappy love affair, so I don\'t see why anybody else should have a good time.',
'qty_inner' => '100',
'qty_masterCarton' => '200',
'qty_pallet' => '300',
'averageMonthlyUnitSales' => '100',
'created_at' => '2022-03-13T04:14:12.11.093745 +00:00'
],
];
$new = [];
foreach ($data as $line){
if ( $line['averageMonthlyUnitSales'] > 350 ){
// reformat the array
$new = [
'id' => $line['id'],
'sku' => $line['sku'],
'alternates' => ['asin' => ['code'=>$line['asin_code'], 'value'=>$line['asin_value'] ],
'barcode' => ['code'=> $line['barcode'], 'value' => $line['barcode_value']]
],
'commodityCode' => $line['commodityCode'],
'price' => [ 'value' => $line['price'] ],
'instructions' => ['picking' => ['code' => $line['picking_instruction_code'], 'value'=> $line['picking_instruction_value']]
],
'quantity' =>['inner' =>$line['qty_inner'], 'masterCarton' =>$line['qty_masterCarton'], 'pallet'=>$line['qty_pallet']],
'averageMonthlyUnitSales'=>$line['averageMonthlyUnitSales'],
'created_at'=>$line['averageMonthlyUnitSales']
];
}
}
print_r($new);
RESULT
PHP 8.1.3
Array
(
[id] => 9947
[sku] => test_1
[alternates] => Array
(
[asin] => Array
(
[code] => 50
[value] =>
)
[barcode] => Array
(
[code] => 10
[value] => js160694
)
)
[commodityCode] => 9989898889
[price] => Array
(
[value] => 120.50
)
[instructions] => Array
(
[picking] => Array
(
[code] => PICK
[value] =>
)
)
[quantity] => Array
(
[inner] => 120
[masterCarton] => 200
[pallet] => 1200
)
[averageMonthlyUnitSales] => 750
[created_at] => 750
)

Insert new object into objects tree. How?

I have just finished creating this beautiful php object tree. How would I go about inserting new objects using a loop into the items node array? Basically how do I insert new anonymous item objects into the items array in this object tree?
Take a look
<?php
$phpObjectTree =
(object)[
'apiVersion' => '1.0',
'data' => (object)[
'requested' => '2020-01-01T23:59:59.001Z',
'status' => 'OK',
'estimate' => (object)[
'id' => '1001',
'type' => 'Commercial',
'client' => (object)[
'name' => 'ABC Construction, Inc.',
'address1' => '123 Main St',
'city' => 'Salt Lake City',
'state' => 'UT',
'postalCode' => '84101'
],
'summary' => (object)[
'generalRequirements' => (object)[
'default' => 0
],
'salesTax' => (object)[
'material' => 4.85,
'labor' => 4.85,
'equipment' => 4.85
],
'overheadProfit' => (object)[
'material' => 10,
'labor' => 10,
'equipment' => 10
],
'contingency' => (object)[
'default' => 3
],
'performanceBond' => (object)[
'default' => 1.3
],
'laborPlus' => (object)[
'dailyRate' => 280,
'crewCount' => 0,
'dayCount' => 0
],
'locationAdjustment' => (object)[
'factor' => 90.1
],
]
],
'items' => [
(object)[
'id' => '2001',
'number' => '09 91 23.00 0000',
'description' => 'Line Item 1',
'quantity' => 0,
'unit' => 'sf',
'material' => (object)[
'cost' => 0,
'costEach' => 9.99
],
'labor' => (object)[
'cost' => 0,
'costEach' => 9.99
],
'equipment' => (object)[
'cost' => 0,
'costEach' => 9.99
],
'lineTotal' => 0
],
(object)[
'id' => '2002',
'number' => '09 91 23.00 0000',
'description' => 'Line Item 2',
'quantity' => 0,
'unit' => 'sf',
'material' => (object)[
'cost' => 0,
'costEach' => 9.99
],
'labor' => (object)[
'cost' => 0,
'costEach' => 9.99
],
'equipment' => (object)[
'cost' => 0,
'costEach' => 9.99
],
'lineTotal' => 0
]
]
]
];
// SET JSON HEADER...
header('Content-type:application/json;charset=utf-8');
print_r(json_encode($phpObjectTree));
?>
Same way you'd modify any array in php:
https://www.php.net/manual/en/language.types.array.php#language.types.array.syntax.modifying
$phpObjectTree->data->items[] = (object)[
'id' => '2003',
'number' => '09 91 23.00 0000',
'description' => 'Line Item 3',
'quantity' => 0,
'unit' => 'sf',
'material' => (object)[
'cost' => 0,
'costEach' => 9.99
],
'labor' => (object)[
'cost' => 0,
'costEach' => 9.99
],
'equipment' => (object)[
'cost' => 0,
'costEach' => 9.99
],
'lineTotal' => 0
];
Or you can use stdClass to create new object and add properties.
$obj = new \stdClass;
$obj->newProperty = 'value';

php Two-dimensional arrays are sorted and grouped according to the value of the key

I have a two-dimensional array that needs to be grouped according to assemble_id ,I can't get the result I want by traversing. Below is the original array:
$list = [
0 =>[
'nickname' => 'Bob',
'phone' => 15295892895,
'is_group' => 1,
'created_at' => 1544944181,
'assemble_id' => 'c1d0zUbmP',
'status' => 1
],
1 =>[
'nickname' => 'Jack',
'phone' => 15295892895,
'is_group' => 1,
'created_at' => 1544944181,
'assemble_id' => 'ED6OJX4VV',
'status' => 1
],
2 =>[
'nickname' => 'Grace',
'phone' => 15295892895,
'is_group' => 0,
'created_at' => 1544944181,
'assemble_id' => 'c1d0zUbmP',
'status' => 1
],
3 =>[
'nickname' => 'Jelly',
'phone' => 15295892895,
'is_group' => 0,
'created_at' => 1544944181,
'assemble_id' => 'ED6OJX4VV',
'status' => 1
]
];
What i need is:
[
[
'assemble_id' => 'c1d0zUbmP',
'assemble_group' => [
[
'nickname' => 'Bob',
'phone' => 15295892895,
'is_group' => 1,
'created_at' => 1544944181,
'assemble_id' => 'c1d0zUbmP',
'status' => 1
],
[
'nickname' => 'Grace',
'phone' => 15295892895,
'is_group' => 0,
'created_at' => 1544944181,
'assemble_id' => 'c1d0zUbmP',
'status' => 1
]
]
],
[
'assemble_id' => 'ED6OJX4VV',
'assemble_group' => [
[
'nickname' => 'Jack',
'phone' => 15295892895,
'is_group' => 1,
'created_at' => 1544944181,
'assemble_id' => 'ED6OJX4VV',
'status' => 1
],
[
'nickname' => 'Jack',
'phone' => 15295892895,
'is_group' => 0,
'created_at' => 1544944181,
'assemble_id' => 'ED6OJX4VV',
'status' => 1
]
]
]
];
I need to group according to the same assemble_id and put is_group=1 in the first of each group.
hery is my code:
function formatData($list)
{
$data = [];
foreach ($list as $k=>$v) {
$list[$k]['created_at'] = formatDate($v['created_at'],'Y-m-d H:i');
$list[$k]['phone'] = setMobile($v['phone']);
$data[$k]['assemble_id'] = $v['assemble_id'];
$data[$v['assemble_id']][] = $list[$k];
}
return $data;
}
This did not get the results I wanted.
Assign temporary associative keys, then remove them when finished looping.
Code: (Demo)
foreach ($list as $row) {
$result[$row['assemble_id']]['assemble_id'] = $row['assemble_id'];
$result[$row['assemble_id']]['assemble_group'][] = [
'nickname' => $row['nickname'],
'phone' => $row['phone'], // add your function here
'is_group' => $row['is_group'],
'created_at' => $row['created_at'], // add your function here
'assemble_id' => $row['assemble_id'],
'status' => $row['status']
];
}
var_export(array_values($result));
Output:
array (
0 =>
array (
'assemble_id' => 'c1d0zUbmP',
'assemble_group' =>
array (
0 =>
array (
'nickname' => 'Bob',
'phone' => 15295892895,
'is_group' => 1,
'created_at' => 1544944181,
'assemble_id' => 'c1d0zUbmP',
'status' => 1,
),
1 =>
array (
'nickname' => 'Grace',
'phone' => 15295892895,
'is_group' => 0,
'created_at' => 1544944181,
'assemble_id' => 'c1d0zUbmP',
'status' => 1,
),
),
),
1 =>
array (
'assemble_id' => 'ED6OJX4VV',
'assemble_group' =>
array (
0 =>
array (
'nickname' => 'Jack',
'phone' => 15295892895,
'is_group' => 1,
'created_at' => 1544944181,
'assemble_id' => 'ED6OJX4VV',
'status' => 1,
),
1 =>
array (
'nickname' => 'Jelly',
'phone' => 15295892895,
'is_group' => 0,
'created_at' => 1544944181,
'assemble_id' => 'ED6OJX4VV',
'status' => 1,
),
),
),
)

Recursion PHP function returns 500 error

Hi I'm have recursion function that makes binary tree array, so from that
array(
array(
'name' => 'John',
'id' => 1,
'mother_id' => 2,
'father_id' => 3
),
array(
'name' => 'Lucy',
'id' => 2,
'mother_id' => 5,
'father_id' => 4
),
array(
'name' => 'Jim',
'id' => 3,
'mother_id' => 7,
'father_id' => 9
),
array(
'name' => 'Paul',
'id' => 4,
'mother_id' => 534,
'father_id' => 54
),
array(
'name' => 'Laura',
'id' => 5,
'mother_id' => 554,
'father_id' => 51
),
array(
'name' => 'Vanessa',
'id' => 7,
'mother_id' => 5354,
'father_id' => 514
),
array(
'name' => 'Adam',
'id' => 9,
'mother_id' => 245354,
'father_id' => 514234
),
);
I'm getting that:
array(
array(
'person' => array(
'name' => 'John',
'id' => 1,
'mother_id' => 2,
'father_id' => 3
),
'parents' => array(
'mother' => array(
'person' => array(
'name' => 'Lucy',
'id' => 2,
'mother_id' => 5,
'father_id' => 4
),
'parents' => array(
'mother' => array(
'person' => array(
'name' => 'Laura',
'id' => 5,
'mother_id' => 554,
'father_id' => 51
),
'parents' => array(...)
),
'father' => array(
'person' => array(
'name' => 'Paul',
'id' => 4,
'mother_id' => 534,
'father_id' => 54
),
'parents' => array(...)
),
)
),
'father' => ...
)
and it works completely fine until it gets array with more than -+100 values, and when it gets this array, it returns 500 error, that's seems strange for me, cos obviously I don't have infinite loop or something like that, cos smallest arrays parses fine but I can't figure out what's reason for this kind of behavior.
function parseTree(&$tree, $root = null)
{
$return = null;
foreach ($tree as $key => $item) {
if ($root == null || $item['id'] == $root) {
$return = [
'person' => $item,
'parents' => [
'father' => parseTree($tree, $item['father_id']),
'mother' => parseTree($tree, $item['mother_id'])
]
];
unset ($tree[$key]);
}
}
return $return;
}

How can I make a binary tree array from the array?

I have the array of persons:
array(
array(
'name' => 'John',
'id' => 1,
'mother_id' => 2,
'father_id' => 3
),
array(
'name' => 'Lucy',
'id' => 2,
'mother_id' => 5,
'father_id' => 4
),
array(
'name' => 'Jim',
'id' => 3,
'mother_id' => 7,
'father_id' => 9
),
array(
'name' => 'Paul',
'id' => 4,
'mother_id' => 534,
'father_id' => 54
),
array(
'name' => 'Laura',
'id' => 5,
'mother_id' => 554,
'father_id' => 51
),
array(
'name' => 'Vanessa',
'id' => 7,
'mother_id' => 5354,
'father_id' => 514
),
array(
'name' => 'Adam',
'id' => 9,
'mother_id' => 245354,
'father_id' => 514234
),
);
And I want to get this:
array(
array(
'person' => array(
'name' => 'John',
'id' => 1,
'mother_id' => 2,
'father_id' => 3
),
'parents' => array(
'mother' => array(
'person' => array(
'name' => 'Lucy',
'id' => 2,
'mother_id' => 5,
'father_id' => 4
),
'parents' => array(
'mother' => array(
'person' => array(
'name' => 'Laura',
'id' => 5,
'mother_id' => 554,
'father_id' => 51
),
'parents' => array(...)
),
'father' => array(
'person' => array(
'name' => 'Paul',
'id' => 4,
'mother_id' => 534,
'father_id' => 54
),
'parents' => array(...)
),
)
),
'father' => ...
)
So John is a child that has mother and father, father has mother and father, mother has mother and father and etc, and write a script that do something but not what I want exactly
function parseTree(& $tree, $root = null) {
$return = null;
foreach ($tree as $key=> $item){
if ($item['id'] == $root){
$return = [
'person' => $item,
'parents' => [
'father' => parseTree($tree, $item['father_id'])
]
];
unset ($tree[$key]);
}elseif($item['id'] == $root){
$return = [
'person' => $item,
'parents' => [
'father' => parseTree($tree, $item['mother_id'])
]
];
unset ($tree[$key]);
}
elseif ($root == null) {
$return = [
'person' => $item,
'parents' => [
'father' => parseTree($tree, $item['father_id']),
'mother' => parseTree($tree, $item['mother_id'])
]
];
unset ($tree[$key]);
}
}
return $return;
}
How can I make it work? Or maybe there are some proper libraries for that?
You almost had it. I would do it in this manner:
function parseTree(&$tree, $root = null)
{
$return = null;
foreach ($tree as $key => $item) {
if ($root == null || $item['id'] == $root) {
$return = [
'person' => $item,
'parents' => [
'father' => parseTree($tree, $item['father_id']),
'mother' => parseTree($tree, $item['mother_id'])
]
];
unset ($tree[$key]);
}
}
return $return;
}

Categories