PHP SOAP Object Reference Not Set to Reference of Object - php

I'm attempting to communicate with a SOAP Service to post some data to the database.
Here's the WSDL.
As you can see, the required data is quite cumbersome so apologies for the length of the code, you will need to scroll through to see everything. Here is how I am attempting to communicate with the service, however I'm getting the error System.NullReferenceException: Object reference not set to an instance of an object
PHP:
$soapClient = new soapClient("https://secure.quickflora.com/Admin/scripts/ws/QuickfloraOrders.asmx?WSDL");
$orderArray = array(
"Token" => $token,
"OrderErrorEmail" => "edwardsalexk#gmail.com",
"Billing" => array("CustomerID" => "1",
"FullName" => "Jack Nichols",
"FirstName" => "Jack",
"LastName" => "Nichols",
"Address1" => "9725 E Windrose Drive",
"Address2" => "",
"Address3" => "",
"City" => "Scottsdale",
"State" => "Arizona",
"Zip" => "85260",
"Phone"=> "4807218374",
"PhoneExt" => "123",
"Fax" => "1234",
"Email"=> "edwardsalexk#gmail.com",
"Cell" =>"480 721 8374",
),
"Shipping" => array("Attention "=> "Alexander",
"Salutation" => "Alexander",
"FirstName" => "Alexander",
"LastName" => "Edwards",
"Address1"=> "9725 E Windrose Drive",
"Address2" => "",
"Address3" => "",
"City"=>"Scottsdale",
"State" =>"Arizona",
"Zip" => "85260",
"Country" => "United States",
"Phone" => "480 721 8374",
"PhoneExt" => "123",
"Fax" => "123",
"Email" => "edwardsalexk#gmail.com",
"Cell" => "480 721 8374",
"CardMessage" => "I hope these flowers find you well!",
"DestinationType" => "House",
"DriverRouteInfo" => "None",
"Priority" => "Highest",
"Order Ship Date" => "4/20/2017",
),
"Payment" => array("MethodID" => "1",
"Check" =>array("Number" => "123",
"CheckID" => "1",
"ChkDate" => "4/20/2017,"),
"CreditCard" => array("TypeID" => "1",
"Name" => "Alexander Edwards",
"Account Number" => "12345678",
"Expiration Date" => "11/14/1993",
"SecurityCode" => "123",
"ApprovalCode" => "123")),
"WireServiceInfo" => array("WireService" => "Alexander Service",
"WireServiceCode" => "123",
"ReferenceID" => "123",
"TransmitMethod" => "123",
"RepresentativeTalkedTo" => "Alexander",
"FloristName" => "Alexander",
"FloristPhone" => "480 721 8734",
"FloristCity" => "Scottsdale",
"FloristState" => "Arizona"),
"Orderdetails" => array("OccasionCodeID" => "123",
"SourceCodeID" => "123",
"Comments" => "Hello",
"TaxGroupID" => "123",
"Currency" => array("ID" => "123",
"ExchangeRate" => 1.2),
"DeliveryCharge" => 12.5,
"ServiceCharge" => 12.5,
"Discounts" => array("Percentage" => 12.5,
"Amount" => 12.5),
"Tax" => array("Percentage" => 12.5,
"Amount" => 12.5),
"GSTTax" => array("Percentage" => 12.5,
"Amount" => 12.5),
"PSTTax" => array("Percentage" => 12.5,
"Amount" => 12.5),
"Total" => 123.34
),
"OrderItemsdetails" => array(
"Itemno" => array("String", "String"),
"Itemid" => array("String", "String"),
"UpcCode" => array("String", "String"),
"ItemName" => array("String", "String"),
"Description" => array("String", "String"),
"Quantity" => array("String", "String"),
"UnitOfMeasurement" => array("String", "String"),
"DiscountPercentage" => array("String", "String"),
"UnitPrice" => array("String", "String")
)
);
$response = $soapClient->PostOrder($orderArray);
echo var_dump($response);

Related

Unexpected token while deserializing object: when using Click and Drop API - Royal Mail

I am making a POST request to create an order for Royal Mail Click and Drop:
$response = Http::withHeaders([
'Content-Type' => 'application/json',
'Authorization' => 'Bearer secret-123'
])->post('https://api.parcel.royalmail.com/api/v1/orders/', [
'items' => [
'recipient' => [
'address' => [
"fullName" => 'Tom',
"companyName" => "Test",
"addressLine1" => "150",
"addressLine2" => "Valley Close",
"addressLine3" => "Elmdom",
"city" => "Birmingham",
"county" => "West Midlands",
"postcode" => "B12 2YT",
"countryCode" => "GB"
],
"emailAddress" => "test#test.com"
],
"billing" => [
"address" => [
"fullName" => 'Tom',
"companyName" => "Test",
"addressLine1" => "150",
"addressLine2" => "Valley Close",
"addressLine3" => "Elmdom",
"city" => "Birmingham",
"county" => "West Midlands",
"postcode" => "B12 2YT",
"countryCode" => "GB"
],
"phoneNumber" => "42425 5252552",
"emailAddress" => "test#test.com"
],
"orderDate" => "2021-05-18T16:39:01Z",
"subtotal" => 0,
"shippingCostCharged" => 0,
"total" => 0,
]
])->json();
dd($response);
but keep getting
'Unexpected token while deserializing object: PropertyName. Path 'items.recipient', line 1, position 22. Failed to deserialize following order request'
I keep getting the same error for all required fields...
API docs do not provide much details https://api.parcel.royalmail.com/. The same payload works in Insomnia. I am using Laravel Http client.
The API shows that items is an array of objects.
items": [
{
"orderReference": "string",
"recipient": {},
"sender": {},
"billing": {},
"packages": [],
"orderDate": "2019-08-24T14:15:22Z",
"plannedDespatchDate": "2019-08-24T14:15:22Z",
"specialInstructions": "string",
"subtotal": 0,
"shippingCostCharged": 0,
"otherCosts": 0,
"customsDutyCosts": 0,
"total": 0,
"currencyCode": "str",
"postageDetails": {},
"tags": [],
"label": {}
}
]
So you just need to wrap everything in items in another set of brackets.
$response = Http::withHeaders([
'Content-Type' => 'application/json',
'Authorization' => 'Bearer secret-123'
])->post('https://api.parcel.royalmail.com/api/v1/orders/', [
'items' => [[
'recipient' => [
'address' => [
"fullName" => 'Tom',
"companyName" => "Test",
"addressLine1" => "150",
"addressLine2" => "Valley Close",
"addressLine3" => "Elmdom",
"city" => "Birmingham",
"county" => "West Midlands",
"postcode" => "B12 2YT",
"countryCode" => "GB"
],
"emailAddress" => "test#test.com"
],
"billing" => [
"address" => [
"fullName" => 'Tom',
"companyName" => "Test",
"addressLine1" => "150",
"addressLine2" => "Valley Close",
"addressLine3" => "Elmdom",
"city" => "Birmingham",
"county" => "West Midlands",
"postcode" => "B12 2YT",
"countryCode" => "GB"
],
"phoneNumber" => "42425 5252552",
"emailAddress" => "test#test.com"
],
"orderDate" => "2021-05-18T16:39:01Z",
"subtotal" => 0,
"shippingCostCharged" => 0,
"total" => 0,
]]
])->json();

Payment api multidimensional array issue in php

My payment provider issued the following example code for their API:
$order = $mollie->orders->create([
"amount" => [
"value" => "1027.99",
"currency" => "EUR"
],
"billingAddress" => [
"streetAndNumber" => "Keizersgracht 313",
"postalCode" => "1016 EE",
"city" => "Amsterdam",
"country" => "nl",
"givenName" => "Luke",
"familyName" => "Skywalker",
"email" => "luke#skywalker.com",
],
"shippingAddress" => [
"streetAndNumber" => "Keizersgracht 313",
"postalCode" => "1016 EE",
"city" => "Amsterdam",
"country" => "nl",
"givenName" => "Luke",
"familyName" => "Skywalker",
"email" => "luke#skywalker.com",
],
"metadata" => [
"order_id" => $orderId
],
"consumerDateOfBirth" => "1958-01-31",
"locale" => "en_US",
"orderNumber" => strval($orderId),
"redirectUrl" => "{$protocol}://{$hostname}{$path}/orders/return.php?order_id={$orderId}",
"webhookUrl" => "{$protocol}://{$hostname}{$path}/orders/webhook.php",
"method" => "ideal",
"lines" => [
[
"sku" => "5702016116977",
"name" => "LEGO 42083 Bugatti Chiron",
"productUrl" => "https://shop.lego.com/nl-NL/Bugatti-Chiron-42083",
"imageUrl" => 'https://sh-s7-live-s.legocdn.com/is/image//LEGO/42083_alt1?$main$',
"quantity" => 2,
"vatRate" => "21.00",
"unitPrice" => [
"currency" => "EUR",
"value" => "399.00"
],
"totalAmount" => [
"currency" => "EUR",
"value" => "698.00"
],
"discountAmount" => [
"currency" => "EUR",
"value" => "100.00"
],
"vatAmount" => [
"currency" => "EUR",
"value" => "121.14"
]
],
[
"type" => "digital",
"sku" => "5702015594028",
"name" => "LEGO 42056 Porsche 911 GT3 RS",
"productUrl" => "https://shop.lego.com/nl-NL/Porsche-911-GT3-RS-42056",
"imageUrl" => 'https://sh-s7-live-s.legocdn.com/is/image/LEGO/42056?$PDPDefault$',
"quantity" => 1,
"vatRate" => "21.00",
"unitPrice" => [
"currency" => "EUR",
"value" => "329.99"
],
"totalAmount" => [
"currency" => "EUR",
"value" => "329.99"
],
"vatAmount" => [
"currency" => "EUR",
"value" => "57.27"
]
]
]
]);
What I'd like to do is to replace the hard coded second dimension "lines" with data from these session vars.
foreach ($_SESSION['cart_products'] as $cart_itm) {
$name = $cart_itm['cat_item_titel_' . $lang . ''];
$unitPrice = $cart_itm['cat_item_prijs'];
$sku = $cart_itm['cat_item_code'];
}
And this is the solution I came up with but this just doesn't work. The reason, I assume, is that the data in the $orders array is echoed as a string and not functional code as i should be. But I haven't got a clue how to aproach this issue.
$i = 0;
$lngth = count($_SESSION['cart_products']);
foreach ($_SESSION['cart_products'] as $cart_itm) {
$orders[] = '[';
$orders[] .= '"sku" => ' . $cart_itm['cat_item_code'] . ',';
$orders[] .= '"name" => ' . $cart_itm['cat_item_titel_en'] . ',';
$orders[] .= '"quantity" => 1,';
$orders[] .= '"vatRate" => "0.00",';
$orders[] .= '"unitPrice" => [
"currency" => "EUR",
"value" => "15.50"],';
$orders[] .= '"totalAmount" => [
"currency" => "EUR",
"value" => "15.50"],';
$orders[] .= '"vatAmount" => [
"currency" => "EUR",
"value" => "0.00"]';
if ($i == $lngth - 1) {
$orders[] .= ']';
}
else {
$orders[] .= '],';
}
$i++;
}
$echoOrders = implode($orders);
$order = $mollie->orders->create([
"amount" => [
"value" => "15.50",
"currency" => "EUR"
],
"billingAddress" => [
"streetAndNumber" => $address,
"postalCode" => $postalcode,
"city" => $city,
"country" => $country,
"givenName" => $firstname,
"familyName" => $name,
"email" => $email,
],
"shippingAddress" => [
"streetAndNumber" => $addressa,
"postalCode" => $postalcodea,
"city" => $citya,
"country" => $countrya,
"givenName" => $firstnamea,
"familyName" => $namea,
"email" => $email,
],
"metadata" => [
"order_id" => $orderId
],
"locale" => "nl_BE",
"orderNumber" => strval($orderId),
"redirectUrl" => "{$protocol}://{$hostname}{$path}/thankyou.php?order_id={$orderId}",
"webhookUrl" => "https://www.cluster-park.com/dev/includes/webhook.php",
"lines" => [ $echoOrders ]
]);
All suggestions are more than welcome.
Thanks!
You need to build an associative array, at the moment you are building a string. This should give you a starting point...
$lines = [];
foreach ($_SESSION['cart_products'] as $cart_itm) {
$lines[] = [ "sku" => $cart_itm['cat_item_code'],
"name" => $cart_itm['cat_item_titel_en'],
"quantity" => 1,
"vatRate" => "0.00",
"unitPrice" => [
"currency" => "EUR",
"value" => "15.50"]
],
// Add all of the other data you have
];
}
then later in your code, you add them in using...
"lines" => $lines

Add a dynamic key => value options in 3 level array option

I have an array as main options in my code.
In case, I want to add dynamic key => values into specific options array key.
This is my main options array:
$configarray1 = array(
"name" => "Addon",
"description" => "module for whmcs",
"version" => "1.1",
"author" => "Me",
"language" => "english",
"fields" => array(
"sender" => array (
"FriendlyName" => "Sender",
"Type" => "dropdown",
"Options" => strtolower($GatewaysIM),
"Description" => $getBalance,
"Default" => $Defaultsender,
),
"validateday" => array (
"FriendlyName" => "Days for Re-validation",
"Type" => "text",
"Size" => "25",
"Description" => "",
"Default" => "90",
),
)
);
I want to add this sender array options in configarray1 fields key:
if($sender == 'sender1'){
$configarray2['fields'] = array(
"username" => array (
"FriendlyName" => "username",
"Type" => "text",
"Size" => "25",
"Description" => "",
"Default" => "",
),
"password" => array (
"FriendlyName" => "password",
"Type" => "password",
"Size" => "25",
"Description" => "",
"Default" => "",
)
);
} elseif($sender == 'sender2'){
$configarray2['fields'] = array(
"line" => array (
"FriendlyName" => "line",
"Type" => "text",
"Size" => "25",
"Description" => "",
"Default" => "",
)
);
}
Output array must be like this below when sender is sender1:
$configarray = array(
"name" => "Addon",
"description" => "module for whmcs",
"version" => "1.1",
"author" => "Me",
"language" => "english",
"fields" => array(
"sender" => array (
"FriendlyName" => "Sender",
"Type" => "dropdown",
"Options" => strtolower($GatewaysIM),
"Description" => $getBalance,
"Default" => $Defaultsender,
),
"username" => array (
"FriendlyName" => "username",
"Type" => "text",
"Size" => "25",
"Description" => "",
"Default" => "",
),
"password" => array (
"FriendlyName" => "password",
"Type" => "password",
"Size" => "25",
"Description" => "",
"Default" => "",
),
"validateday" => array (
"FriendlyName" => "Days for Re-validation",
"Type" => "text",
"Size" => "25",
"Description" => "",
"Default" => "90",
),
)
);
I tested array push but this adds a key in arrays first place and not in 'fields' key, my code was this $configarray = array_push($configarray1,$configarray2); but this not works !
I also tested sum of two arrays ($configarray = $configarray1 + $configarray2) but this is the same as array_push and returns wrong output for me.
How can i resolve this problem ?!
You should just declare it. You don't need a new variable.
Assuming you have the original array named as $configarray1, just:
if($sender == 'sender1'){
$configarray1['fields']['username'] = array(
"FriendlyName" => "username",
"Type" => "text",
"Size" => "25",
"Description" => "",
"Default" => "",
);
$configarray1['fields']['password'] => array(
"FriendlyName" => "password",
"Type" => "password",
"Size" => "25",
"Description" => "",
"Default" => "",
);
} elseif($sender == 'sender2'){
$configarray1['fields']['line'] = array (
"FriendlyName" => "line",
"Type" => "text",
"Size" => "25",
"Description" => "",
"Default" => "",
);
}

Array multisort not working for an array

$artists = [
0 => [
"id" => "3",
"plan_id" => "1",
"name" => "Artist-A",
"views" => "1189189",
"soundcloud" => "42",
"facebook" => "59881948",
"twitter" => "21760757",
"youtube" => 0,
"instagram" => "3429017"
],
1 => [
"id" => "10",
"plan_id" => "1",
"name" => "Artist-B",
"views" => "1",
"soundcloud" => 0,
"facebook" => 0,
"twitter" => 0,
"youtube" => 0,
"instagram" => 0
],
2 => [
"id" => "2",
"plan_id" => "1",
"name" => "Artist-C",
"views" => "1629",
"soundcloud" => "20",
"facebook" => "5025158",
"twitter" => "582899",
"youtube" => 0,
"instagram" => "112127"
],
3 => [
"id" => "4",
"plan_id" => "2",
"name" => "Artist-D",
"views" => "484353",
"soundcloud" => "7",
"facebook" => "104449606",
"twitter" => "36820201",
"youtube" => 0,
"instagram" => "16483226"
],
4 => [
"id" => "5",
"plan_id" => "2",
"name" => "Artist-E",
"views" => "98765432",
"soundcloud" => "13",
"facebook" => "59551072",
"twitter" => "38995648",
"youtube" => 0,
"instagram" => "64997436"
]
]
foreach ($remaining_artists as $key => $value) {
$soundcloud[$key] = $value['soundcloud'];
}
array_multisort($soundcloud, SORT_ASC, $artists);
I use array_multisort to sort array. It's working perfectly fine. But there is an error 'array_multisort(): Array sizes are inconsistent' for the above array. I really can't figure out what's the problem here and its solution.
Your array_multisort() parameters were out of order.
http://php.net/manual/en/function.array-multisort.php
Also, syntax problems.
This works
<?php
$artists = [
0 => [
"id" => "3",
"plan_id" => "1",
"name" => "Artist-A",
"views" => "1189189",
"soundcloud" => "42",
"facebook" => "59881948",
"twitter" => "21760757",
"youtube" => 0,
"instagram" => "3429017"
],
1 => [
"id" => "10",
"plan_id" => "1",
"name" => "Artist-B",
"views" => "1",
"soundcloud" => 0,
"facebook" => 0,
"twitter" => 0,
"youtube" => 0,
"instagram" => 0
],
2 => [
"id" => "2",
"plan_id" => "1",
"name" => "Artist-C",
"views" => "1629",
"soundcloud" => "20",
"facebook" => "5025158",
"twitter" => "582899",
"youtube" => 0,
"instagram" => "112127"
],
3 => [
"id" => "4",
"plan_id" => "2",
"name" => "Artist-D",
"views" => "484353",
"soundcloud" => "7",
"facebook" => "104449606",
"twitter" => "36820201",
"youtube" => 0,
"instagram" => "16483226"
],
4 => [
"id" => "5",
"plan_id" => "2",
"name" => "Artist-E",
"views" => "98765432",
"soundcloud" => "13",
"facebook" => "59551072",
"twitter" => "38995648",
"youtube" => 0,
"instagram" => "64997436"
]
];
$soundcloud = [];
foreach ($artists as $key => $value) {
$soundcloud[$key] = $value['soundcloud'];
}
array_multisort($soundcloud, $artists, SORT_ASC);
print_r($artists);

Dolibarr soap web service usage with php

I am trying to use a Dolibarr soap web service with php
Dolibarr web service
this is what i got for the moment:
$url = "http://localhost/seko/dollibar/dolibarr-3.7.1/htdocs/webservices/server_invoice.php?wsdl";
$client = new SoapClient($url);
$authentication = array(
"dolibarrkey" => "xxxxx",
"sourceapplication" => "",
"login" => "xxxx",
"password" => "xxxxxx",
"entity" => "1"
);
$line = array(
"id" => "57",
"type" => 0,
"desc" => "SEKO",
"vat_rate" => 16.000,
"qty" => 03,
"unitprice" => 10500.00000000,
"total_net" => 10500.0000000,
"total_vat" => 1680.00000000,
"total" => 12180.0000000,
"date_start" => "",
"date_end" => "",
"payment_mode_id" => "efectivo",
"product_id" => 1,
"product_ref" => "",
"product_label" => "",
"product_desc" => ""
);
$invoice = array(
"id" => "57",
"ref" => "0007",
"ref_ext" => "test",
"thirdparty_id" => 3,
"fk_user_author" => "1",
"fk_user_valid" => "1",
"date" => date("Y-m-d"),
"date_due" => date("Y-m-d"),
"date_creation" => date("Y-m-d h:i:sa"),
"date_validation" => date("Y-m-d h:i:sa"),
"date_modification" => "",
"type" => 0,
"total_net" => 10500.00000000,
"total_vat" => 1680.00000000,
"total" => 12180.0000000,
"note_private" => "",
"note_public" => "",
"status" => 2,
"close_code" => "",
"close_note" => "",
"project_id" => "",
"lines" => $lines
);
$res = $client->createInvoice($authentication, $invoice);
var_dump($res);
I get the following error:
An uncaught Exception was encountered
Type: SoapFault
Message: looks like we got no XML document
When I use the getInvoice method of the service it works fine. But not the creatInvoice method. I am sure the problem is with my $line array but I do not know how to fix it.
According to XML documentation (open this http://localhost/seko/dollibar/dolibarr-3.7.1/htdocs/webservices/server_invoice.php?wsdl in your webbrowser)
please note that you need to remove the "payment_mode_id" field from the $line array and move it to your $invoice array
Like this :
$line = array(
"id" => "57",
"type" => 0,
"desc" => "SEKO",
"vat_rate" => 16.000,
"qty" => 03,
"unitprice" => 10500.00000000,
"total_net" => 10500.0000000,
"total_vat" => 1680.00000000,
"total" => 12180.0000000,
"date_start" => "",
"date_end" => "",
"product_id" => 1,
"product_ref" => "",
"product_label" => "",
"product_desc" => ""
);
$invoice = array(
"id" => "57",
"ref" => "0007",
"ref_ext" => "test",
"thirdparty_id" => 3,
"fk_user_author" => "1",
"fk_user_valid" => "1",
"date" => date("Y-m-d"),
"date_due" => date("Y-m-d"),
"date_creation" => date("Y-m-d h:i:sa"),
"date_validation" => date("Y-m-d h:i:sa"),
"date_modification" => "",
"payment_mode_id" => "efectivo",
"type" => 0,
"total_net" => 10500.00000000,
"total_vat" => 1680.00000000,
"total" => 12180.0000000,
"note_private" => "",
"note_public" => "",
"status" => 2,
"close_code" => "",
"close_note" => "",
"project_id" => "",
"lines" => $lines
);
Hoping help
Regards

Categories