Connecting a real wsdl service and using its methods with PHP - php

I have an ecommerce website which is under development right now. I bought a web software. I want to connect to the webservice and add some brands.
This is my webservice WSDL url: http://www.cantabu.com/Servis/UrunServis.svc?wsdl
The method for adding a new brand is "SaveMarka".
This is my initial code:
ini_set("soap.wsdl_cache_enabled", "0");
$wsdl = "http://www.cantabu.com/Servis/UrunServis.svc?wsdl";
$authCode = "MY_WEBSERVICE_AUTHENTICATION_CODE";
$client = new SoapClient($wsdl);
When i print_r $client->__getFunctions() The result is (Related to SaveMarka method):
Array
(
[4] => SaveMarkaResponse SaveMarka(SaveMarka $parameters)
)
And when i print_r $client->__getTypes(), the result is (Up to 5th index of the result array, because others are unrelated to SaveMarka):
Array
(
[0] => struct ArrayOfint {
int int;
}
[1] => struct ArrayOfstring {
string string;
}
[2] => struct ArrayOfKategori {
Kategori Kategori;
}
[3] => struct Kategori {
boolean Aktif;
int ID;
string Icerik;
string Kod;
int PID;
string SeoAnahtarKelime;
string SeoSayfaAciklama;
string SeoSayfaBaslik;
int Sira;
string Tanim;
}
[4] => struct ArrayOfMarka {
Marka Marka;
}
[5] => struct Marka {
boolean Aktif;
int ID;
string SeoAnahtarKelime;
string SeoSayfaAciklama;
string SeoSayfaBaslik;
string Tanim;
}
In their web service document, they say; if you want to add a new brand, the ID should be 0 (zero). Otherwise, whe webservice will update the brand related to that ID.
I have tried different variations of SaveMarka method. But they all failed with varied results.
First of all, i have created a Marka class.
class Marka{
var $ID;
var $SeoAnahtarKelime;
var $SeoSayfaBaslik;
var $SeoSayfaAciklama;
var $Tanim;
var $Aktif;
function __construct($id, $seo, $seoAciklama, $seoBaslik, $ad){
$this->ID = $id;
$this->SeoAnahtarKelime = $seo;
$this->SeoSayfaBaslik = $seoBaslik;
$this->SeoSayfaAciklama = $seoAciklama;
$this->Tanim = $ad;
$this->Aktif = true;
}
}
Then i have tried to do this:
$marka = new Marka(0, "pg", "P&G", "P&G", "P&G");
$newMarka = array(
"UyeKodu" => $authCode",
"Marka" => $marka
);
$client->__soapCall("SaveMarka", $newMarka);
The result is:
Type: SoapFault
Message: The formatter threw an exception while trying to deserialize
the message: Error in deserializing body of request message for
operation 'SaveMarka'. End element 'Body' from namespace
'http://schemas.xmlsoap.org/soap/envelope/' expected. Found element
'param1' from namespace ''. Line 2, position 150.
I also tried (I don't know why):
$marka = new Marka(0, "pg", "P&G", "P&G", "P&G");
$newMarka = array(array(
"UyeKodu" => $authCode",
"Marka" => $marka
));
$client->__soapCall("SaveMarka", $newMarka);
And the result is:
Type: SoapFault
Message: Object reference not set to an instance of an object.
Then i get rid of the Marka class and tried to do it simpler without using a class:
$newMarka = array(
"UyeKodu" => $authCode,
"ID" => 0,
"Tanim" => "P&G",
"Aktif" => true,
"SeoAnahtarKelime" => "pg",
"SeoSayfaBaslik" => "P&G",
"SeoSayfaAciklama" => "P&G"
);
$client->__soapCall("SaveMarka", $newMarka);
Result is:
Type: SoapFault
Message: The formatter threw an exception while trying to deserialize
the message: Error in deserializing body of request message for
operation 'SaveMarka'. End element 'Body' from namespace
'http://schemas.xmlsoap.org/soap/envelope/' expected. Found element
'param1' from namespace ''. Line 2, position 150.
And also (again, i don't know why)
$newMarka = array(array(
"UyeKodu" => $authCode,
"ID" => 0,
"Tanim" => "P&G",
"Aktif" => true,
"SeoAnahtarKelime" => "pg",
"SeoSayfaBaslik" => "P&G",
"SeoSayfaAciklama" => "P&G"
));
$client->__soapCall("SaveMarka", $newMarka);
And the result is:
Type: SoapFault
Message: Object reference not set to an instance of an object.
What am i doing wrong?
Can anyone please help me with adding a new brand? I have supplied the functions, types, wsdl link and all my tries.
Please, help.

Related

PHP Soapclient returns object has no property

When sending a request from my PHP soapclient as described below, I get:
[getInterestAndExchangeRates failed: Please check inparameters in Xxx].
I suspect that it is the [$response] line that has wrong syntax. More specific the [$searchRequestParameters]. I have checked that all the content in the array is in the correct order.
Question:
Is there something wrong with below code syntax?
I have followed following instructions:
https://swea.riksbank.se/sweaWS/docs/api/call/getInterestAndExchangeRates.htm
<?php
/**
* A soapclient request to Swedish Central bank, API.
*/
$client = new SoapClient("https://swea.riksbank.se/sweaWS/wsdl/sweaWS_ssl.wsdl");
$searchGroupSeries = array(
"groupid" => 2,
"seriesid" => "SECBREPOEFF"
);
$searchRequestParameters = array (
"aggregateMethod" => "W",
"avg" => true,
"datefrom" => "2018-01-01",
"dateto" => "2019-01-01",
"languageid" => "en",
"max" => true,
"min" => false,
"searchGroupSeries" => $searchGroupSeries,
"ultimo" => false
);
// Test 1 (pointing out above array, does not work):
$response = $client->__soapCall("getInterestAndExchangeRates", $searchRequestParameters);
// Test 2 (wrap in above array inside another array, does not work):
// $response = $client->__soapCall('getInterestAndExchangeRates', array('searchRequestParameters' => $searchRequestParameters));
var_dump($response);
Error:
PHP Fatal error: Uncaught SoapFault exception: [soap:Server] getInterestAndExchangeRates failed: Please check inparameters in [path]_(does_not_work).php:22
Stack trace:
#0 [path]_(does_not_work).php(22): SoapClient->__soapCall('getInterestAndE...', Array)
#1 {main}
thrown in [path]_(does_not_work).php on line 22

Object of class could not be converted to int

I want to write phpunit test for save method at my repository. My repo code is:
public function saveCustomer(Custom $custom)
{
try
{
$custom->save();
return array(
'status' => true,
'customerId' => $custom->getId()
);
}
catch(\Exception $e)
{
return array(
'status' => false,
'customerId' => 0
);
}
}
I wrote this test:
public function testSaveNewUye()
{
$request = array(
'email' => 'www#www.com',
'phone' => '555 555 555',
'password' => '34636'
);
$repo = new CustomerRepository();
$result_actual = $this->$repo->saveCustomer($request);
$result_expected = array(
'status' => true,
'customerId' => \DB::table('custom')->select('id')->orderBy('id', 'DESC')->first() + 1
);
self::assertEquals($result_expected, $result_actual);
}
I got the error given below:
ErrorException: Object of class App\CustomerRepository could not be converted to int
Can you help me?
Problem is here:
$repo = new CustomerRepository();
$result_actual = $this->$repo->saveCustomer($request);
You are assigning and using variables not the same.
Try like this instead:
$this->repo = new CustomerRepository();
// ^------- assign to `$this`
$result_actual = $this->repo->saveCustomer($request);
// ^------- remove `$`
When doing $this->$repo-> PHP tries to convert the (object) $repo to a string $this->(object)-> which does not work.
Then you have a second error here:
\DB::table('custom')->select('id')->orderBy('id', 'DESC')->first() + 1
From the database you get an object (instanceof stdClass) which you cannot simply + 1.
The whole thing is probably something like
\DB::table('custom')->select('id')->orderBy('id', 'DESC')->first()->id + 1
(From the returned object, you want the property id.)

Google cloud -speech api return null result

I work with Google cloud speech API. When I run my script there is a call to the API and a response. The operation info returns data, but the result is empty.
Here is my code (where file url, file name, key url, project name and bucket name I deleted the real data):
function __construct(){
$file_url='file path.mp3';
$filename='file name.mp3';
/** Create google client **/
$client = new Google_Client();
$key='path to google key';
putenv($key);
$client->useApplicationDefaultCredentials();
/** Create storage **/
$str_config = array(
'projectId' => 'project id'
);
$storage = new StorageClient($str_config);
$bucket_name='bucket name';
$bucket=$storage->bucket($bucket_name);
$object = $bucket->object($filename);
/** Create Speech **/
$config = array(
'projectId' => 'project id',
'languageCode' => 'en-US'
);
$options = array(
"encoding"=>'LINEAR16',
"languageCode"=>"en-US",
'sampleRateHertz' => 16000
)
;
$speech = new Google\Cloud\Speech\SpeechClient($config);
$operation = $speech->beginRecognizeOperation(
$object,
$options
);
$backoff = new ExponentialBackoff(100);
$backoff->execute(function () use ($operation) {
print('Waiting for operation to complete' . PHP_EOL);
$operation->reload();
if (!$operation->isComplete()) {
throw new Exception('Job has not yet completed', 500);
}
});
if ($operation->isComplete()) {
if (empty($results = $operation->results())) {
$results = $operation->info();
}
var_dump($results, $operatimon->results());
}
}
The result i get call:
Array
(
[0] => Array
(
[name] => some name
[metadata] => Array
(
[#type]=> type.googleapis.com/google.cloud.speech.v1.LongRunningRecognizeMetadata
[progressPercent] => 100
[startTime] => 2017-07-16T19:15:58.768490Z
[lastUpdateTime] => 2017-07-16T19:15:59.999625Z
)
[done] => 1
[response] => Array
(
[#type]=> type.googleapis.com/google.cloud.speech.v1.LongRunningRecognizeResponse
[totalBilledTime] => 15s
)
)
[1] => Array
(
)
)
I tried several file type whit several encodings, can't find the right combination. Or maybe there is another problem. Pleas Help.
Solved it by using the ffmpeg library to encode the audio to flac whit mono channel.
For anyone else encountering this problem, the issue could lie in your audio file not matching the encoding you've entered in your option array.
Check this resource:
https://cloud.google.com/speech-to-text/docs/reference/rest/v1beta1/RecognitionConfig#AudioEncoding
Just like the accepted answer, by changing from "LINEAR16" to "FLAC" and converting my audio file to FLAC, it worked for me.

PHP SOAP Request via SOAPClient to SAP - params missing error

I am doing a PHP SOAP Request as follows (my code, location removed as I'm not allow to publish)
try {
$location = "http://myUrltoWSDLhere";
$client = new SoapClient($location,
array(
'soap_version' => SOAP_1_1,
'login' => 'myuser',
'password' => 'mypass'
));
$params = array(array('BUKRS'=> 1));
$x = $client->ZIbFtiWsCredPosToWeb( $params );
print_r($x);
} catch (Exception $e) {
echo $e->getMessage();
}
I've been trying countless combinations how to pass the params to my method and I am doing a print_r($x) and gives me the below output.
stdClass Object ( [EBkpf] => stdClass Object ( ) [EMsgnr] => 6 [EMsgtxt] => BUKRS is missing [EMsgtyp] => E [ESstMon] => 00000000000000000000 )
Important: SoapClient getTypes returns the following for this method and the IBukrs is the field I am trying to pass data to. In SAP the Company Code is "BUKRS" so I had tried BUKRS and IBukrs and also Bukrs.
Any help would be greatly appreciated!!
[43] => struct ZIbFtiWsCredPosToWeb {
int IAnz;
char1 IAp;
char4 IBukrs;
ZibSstGjahr IGjahr;
ZibSstCredTt IKreditor;
char1 IOp;
numeric20 ISstMon;
}
WSDL Content: http://pastebin.com/fU5PhD9B

SOAP PHP Value cannot be null. Parameter name: source

I'm trying to call a function from this webservice:
http://www.zulutrade.com/WebServices/Performance.asmx?WSDL
I'm sending all the requested params but I'm getting this error: Value cannot be null. Parameter name: source
I think it's a server issue, but maybe I need to change something in my code:
$client = new SoapClient('http://www.zulutrade.com/WebServices/Performance.asmx?WSDL',
array('location' => "http://www.zulutrade.com/WebServices/Performance.asmx",
'trace'=>1,
"cache_wsdl" => 0));
$params = array
(
'providerId' => 109206,
'fromDateStr' => "1985-12-19",
'toDateStr' => "2013-05-06",
'validTrades' => true,
'lotSize' => "Mini",
'start' => 0,
'length' => 20,
'sortBy' => "buy",
'sortAscending' => true
);
try
{
$result = $client->GetProviderTrades($params);
}
catch (SoapFault $fault)
{
print_r($fault);
}
Any ideas?
thanks
I tried with nusoap class and I get this error
HTTP Error: Couldn't open socket connection to server http://www.zulutrade.com:81/WebServices/Performance.asmx, Error (10060): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
So maybe it's a bug on their part
You set all vars except one. ArrayOfInt currencyIds; is not set.
struct GetProviderTrades {
int providerId;
ArrayOfInt currencyIds;
string fromDateStr;
string toDateStr;
boolean validTrades;
LotSize lotSize;
int start;
int length;
string sortBy;
boolean sortAscending;
}

Categories