I'm trying to make a soap call to a server with as little code as possible. But I'm alreaty having issues authenticating myself (No headers neccessary for that call). This is the code I have:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
header('Content-Type: text/html; charset=utf-8');
$data = array('Username' => 'someusername', 'Userpass' => 'somepass');
try
{
$client = new SoapClient(null, array(
"location" => "http://relaxdays.plentymarkets-x1.com/plenty/api/soap/version115/?xml",
"uri" => "http://relaxdays.plentymarkets-x1.com/plenty/api/soap/version115/",
"trace" => true,
"soap_version" => SOAP_1_1,
"exceptions" => false
));
$response = $client->__soapCall("PlentySoapRequest_GetAuthentificationToken", $data);
}
catch (Exception $e)
{
echo "Error!";
echo $e -> getMessage();
echo '<br/><br/>Last response:<button type="button" onclick="if(document.getElementById(\'spoiler1\').style.display==\'none\') {document.getElementById(\'spoiler1\') .style.display=\'\'}else{document.getElementById(\'spoiler1\') .style.display=\'none\'}">Show/Hide</button><div id="spoiler1" style="display:none; font-family:monospace;">'. htmlspecialchars($client->__getLastResponse()).'</div>';
}
echo '<br/><br/>Last request:<button type="button" onclick="if(document.getElementById(\'spoiler2\').style.display==\'none\') {document.getElementById(\'spoiler2\') .style.display=\'\'}else{document.getElementById(\'spoiler2\') .style.display=\'none\'}">Show/Hide</button><div id="spoiler2" style="display:none; font-family:monospace;">'. htmlspecialchars($client->__getLastRequest()).'</div>';
echo '<br/><br/>Response:<button type="button" onclick="if(document.getElementById(\'spoiler3\').style.display==\'none\') {document.getElementById(\'spoiler3\') .style.display=\'\'}else{document.getElementById(\'spoiler3\') .style.display=\'none\'}">Show/Hide</button><div id="spoiler3" style="display:none; font-family:monospace;">'. htmlspecialchars($response).'</div>';
?>
However, regardless of what I try, it doesn't seem to work. The furthest I've come is this error:
SoapFault exception: [VersionMismatch] Wrong Version in /var/www/htdocs/plentyimport/tester.php:18
Stack trace: #0 /var/www/htdocs/plentyimport/tester.php(18): SoapClient->__soapCall('PlentySoapReque...', Array) #1 {main}
I've already tried using "soap_version" => SOAP_1_2, or switching the Soap-URL to different versions (eg. /version112/) but the error just does not want to go away.
Any help would be appreciated.
Thank you very much
virhonestum
You have to pass the definition of SOAP as first parameter, like this:
$client = new SoapClient('http://relaxdays.plentymarkets-x1.com/plenty/api/soap/version115/?xml');
I have stripped other arguments as those are not required for making this work.
The call should be made to operation, in this case like this:
$response = $client->__soapCall("GetAuthentificationToken", $data);
An finally, the data should contain structure PlentySoapRequest_GetAuthentificationToken, like this:
$data = [
'PlentySoapRequest_GetAuthentificationToken' => [
'Username' => 'someusername',
'Userpass' => 'somepassword'
]
];
Note: I'm not posting this as complete answer. I don't know SOAP and can't explain what was wrong and how I fixed it, only posting this to help author get out of this phase.
Related
I'm new to Laravel framework so I'm having a hard time to do something very trivial. The main idea is to contact an API and get its response. Below is my function where I'm having error,
public function verification($id=null){
try{
$res = $client->createRequest('POST','http://35.161.181.102/api/socialverify/linkedin',['headers' => $headers , 'body' => $urlclean]);
$res= $client->send($res);
}catch(\GuzzleHttp\Exception\RequestException $e) {
\Log::info($e->getMessage());
\Log::info($e->getCode());
\Log::info($e->getResponse()->getBody()->getContents());
}
}
When I run the above function I'm getting the error shown below,
Illegal string offset 'id'
Any pointers on what I'm doing wrong and how can I solve it.
Any help is appreciated. Thank in advance.
What do you see in your /storage/logs/laravel.log?
I assume Client is Guzzle Client and by default Guzzle throws RequestException whenever there is a request issue. See Documentation. So why not try to do this and see what's the error responded from Guzzle:
try {
$response = $client->post('http://link-to-my-api', array(
'headers' => array('Content-type' => 'application/json'),
'body' => $data
));
$response->send();
}catch(\GuzzleHttp\Exception\RequestException $e) {
\Log::info($e->getMessage());
\Log::info($e->getCode());
\Log::info($e->getResponse()->getBody()->getContents());
}
And check your /storage/logs/laravel.log to see the logs being printed.
you can try this way:
use Illuminate\Support\Facades\Http;
$response = Http::withHeaders($header)
->post($url, [
$data
]);
I am trying to implement a soaprequest and making the call does seem to work. The only problem is: I don't know how to receive the response data. My code looks like this:
$auth = array(
'UsernameToken' => array(
'Username' => 'xxx',
'Password' => 'yyyy'
)
);
$header = new SoapHeader('xs','Security',$auth, 0);
$client->__setSoapHeaders($header);
$client->__setLocation('http://example.com/test.php');
$params = array(
...
'trace' => 1,
'cache_wsdl' => 0
);
try {
$response = $client->getSomeData($params);
}catch(Exception $e){
echo "Exception: ".$e->getMessage();
}
print_r($response);
This results in an empty page, because $response is empty. But the test.php file is called (I tried with a simple mail() command and it sends the mail every time I call the page with the soapclient).
So I guess the soap response is somehow sent to the test.php file - right? How do I get it? If I do not set the location, I get a nullpointerexception, so I have to do that. I tried
$client->__getLastResponse()
that's empty too.
What can I do, how do I get the soap response data? Any hints would be appreciated. Thank you!
I wrote some PHP Unit Tests, that need User Authentication for a Request.
For that i added some parameterse to createClient:
$this->client = static::createClient(array(), array(
'PHP_AUTH_USER' => TEST_USER_NAME,
'PHP_AUTH_PW' => TEST_USER_PASS,
));
TEST_USER_NAME and TEST_USER_PASS containing the Login Credentials.
If I do a request like that
$parameters = array(
"object" => self::TEST_OBJECT_ID,
);
$headers = array(
'HTTP_API_AUTHORIZATION' => 'API_AUTH_KEY',
);
$this->client->request('POST', '/api/v4/object/get', $parameters, array(), $headers);
$response = $this->client->getResponse();
$this->assertEquals(200, $response->getStatusCode(), $response->getContent());
The test says OK, but after that this Message appears:
THE ERROR HANDLER HAS CHANGED!
If i change the Credentials to something wrong, the message does not appear.
Any suggestions how i can prevent that or remove this message?
Found my mistake - i browsed all tests and regarding code and it was was the error said, i changed the error handler.
set_error_handler(array(&$this, 'handleGeoError'));
I get the error that is the title, and this is the line of code he is complaining about;
$client = new Client($passwordResetUrl, array('timeout' => 30));
$client->setParameterPost(array("username" => $username,
"application" => "pw_solcon",
"requester" => $requester,
"method" => $method));
$response = $client->request(Client::POST);
if ($response->getStatus() == 200) {
//do something
}
It complains specifically about $response = $client->request(Client::POST);
I looked at the documentation here, but I don't get what I am doing wrong.
Can somebody please help me?
Thank you
You are using Zend framework 2 and using documentation for Zend Framework 1
use this as your reference.
ZF2 uses $response = $client->send();
I connected to a SOAP server from a client and am trying to send form information back.
The connection works, but I have no idea how to send data back. I have received documentation ( -> http://s000.tinyupload.com/?file_id=89258359616332514672) and am stuck at the function AddApplication
This is the PHP code I've written so far. There is no form integration yet, only dummy data.
<?
$client = new SoapClient(
'https://wstest.hrweb.be/TvBastards/TvBastards/Job.svc?singleWsdl',
array(
'soap_version' => SOAP_1_1
)
);
$params = array(
'username' => 'XXX',
'password' => 'XXX',
'environmentKey' => 'XXX',
);
//Open session
try{
$token = $client->OpenSession($params);
}catch(SoapFault $ex){
echo "<pre>";
print_r($ex->detail->ExceptionDetail);
echo "</pre>";
}
//Add Application
try{
$resp = $client->AddApplication($params, ___THE_XML_SHOULD_BE_HERE___); // I have no idea how I can implement a XML file over here, and make this part work
}catch(SoapFault $ex){
echo "<pre>";
print_r($ex->detail->ExceptionDetail);
echo "</pre>";
}
//Close session
try{
$app = $client->CloseSession($token);
}catch(SoapFault $ex){
echo "<pre>";
print_r($ex);
echo "</pre>";
}`
The error I receive now is the following:
End element 'Body' from namespace 'http://schemas.xmlsoap.org/soap/envelope/' expected. Found element 'param1' from namespace ''. Line 2, position 156.
Which is understandable as I don't provide any XML.
I receive my token so the OpenSession works perfectly. As said, I'm completely stuck at the AddApplication function. This is my first encounter with a SOAP service, so every possible bit of explanation is highly appreciated.
Fixed it, and hopefully it can help out some others. I'll try and put it into steps.
define('SIM_LOGIN', 'LOGIN NAME HERE');
define('SIM_PASSWORD', 'LOGIN PASSWORD HERE');
define('ENV_KEY', 'ENVIRONMENT KEY HERE');
/*** login parameters ***/
$params = array(
'username' => SIM_LOGIN,
'password' => SIM_PASSWORD,
'environmentKey' => ENV_KEY,
);
/*** Set up client ***/
$client = new SoapClient(
__SOAP URL HERE__,
array(
'soap_version' => SOAP_1_1
)
);
After setting up the parameters and connecting to the client, we can start calling functions within the SOAP service. Every SOAP service will be different, so function names and parameters can be different. In below example I need to open a session to retrieve a token. This token is used in all the other functions, so this function is necessary. If something fails I call the "abort()" function.
try{
$token = $client->OpenSession($params);
}catch(SoapFault $ex){
abort();
}
If the token is received I call upon the function AddApplication. This expects the token parameter and an "object" (which is basically an STDClass).
I create an stdClass with all my data:
/*** Create stdClass with requested data ***/
$std = new stdClass();
$std->Firstname = $firstname;
$std->Lastname = $lastname;
$std->Birthdate = $birthdate;
$std->Phone = $phone;
$std->Email = $email;
Be sure to check camelcasing of names or capitals, as this makes all the difference.
Now we call upon the AddApplication function with parameters "token(string)" and "application(object)".
/*** AddApplication ***/
try{
$result = $client->AddApplication(array("token" => $token, "application" => $std));
}catch(SoapFault $ex){
abort();
}
If all goes well the data is stored on the external server and you receive a "success" message. It's possible that you receive a "fail" even without going into the SoapFault. Be sure to log both "$result" and "$ex", as a SOAP service can return a "Fail" but the try-catch sees this as a well formed result.
Last thing to do is close the session (and destroy the token)
/*** CloseSession ***/
try{
$app = $client->CloseSession($token);
}catch(SoapFault $ex){
abort();
}
If any questions, don't hesitate to ask them here, I'll be glad to help as I had such problems figuring this out.