NuSOAP using laravel 4 - php

I want to use nusoap library in applications using laravel 4.
I have an update on the composer with NoiselabsNuSOAP :
NuSOAP Library with composer
And I have followed the user manual
My code :
public function CallSOAP()
{
$client = new \nusoap_client('http://mywebsite/services/VcareServices.php', true);
$response = $client->call('validateLogin', array('EMAIL'=>'bertho_joris#yahoo.co.id', 'PASSWORD'=>'3b774f5aae6b97a060864f8310hg6785'));
return $response;
}
But I get an error message :
UnexpectedValueException The Response content must be a string or
object implementing __toString(), "boolean" given.
My question:
Is this library I can use for my laravel application?
How do I use to use it?

it's true maybe your SOAP response it's not the right one, but first of all try adding:
$client->soap_defencoding = 'UTF-8';
$client->decode_utf8 = false;
and check what is your service returning, maybe you have to specify the element of the response object like:
return $response['MyValue'];
or
return $response['key']['value'];

It looks to me like the nusoap library is being loaded correctly, otherwise you would get an error about \nusoap_client not being a recognised function.
It is more likely that the endpoint you are working with (http://mywebsite/services/VcareServices.php) is not providing a valid SOAP response.
Check that your endpoint complies with the SOAP protocol, or test your client against an existing SOAP server that you know works. That should help you diagnose the problem.

Related

Return file in PHP Slim REST API from a Guzzle call to another REST API

Update
This seems to relate in some way to the reading of the stream when outputting. The function used by Slim to output the body looks like this, where $body implements StreamInterface and $this->responseChunkSize is 4096:
$amountToRead = $body->getSize();
while ($amountToRead > 0 && !$body->eof()) {
$length = min($this->responseChunkSize, $amountToRead);
$data = $body->read($length);
echo $data;
$amountToRead -= strlen($data);
if (connection_status() !== CONNECTION_NORMAL) {
break;
}
}
It appears the $body->eof() call (which is just a wrapper for PHP's feof() function) is returning true even though the full file has not been read. Not sure why that would be though. I also verified that this does not occur if I just do an fopen() on the file and create a Stream from it, then run the same code. It only happens when the stream is the product of the external REST API call via Guzzle.
Original Post
I have a service built using Slim (v4.4) that calls an external REST API using Guzzle (v6.5.3) that returns a file. This is running in Windows, web server is IIS/FastCGI (I know, unusual). PHP version is 7.3.10. The call from Slim to the external REST API retrieves the file just fine, but when my app calls the service, some files get corrupted, seems some data gets lost based on what I see in the file size. The call from the service to the external REST API is fairly simple:
$file_response = $guzzleClient->request('GET', "{$base_url}/docs/{$file_id}", [
'headers' => [
'Authorization' => "token {$token}"
]
]);
The above call works fine and returns the file correctly, I can either display it to screen or use the 'sink' option in Guzzle to save to a file, it works fine. But when I try to call the service that wraps that call, it fails. I tried a couple things. Firstly, I was just returning the response as is since it conforms to the interface required anyway. My Slim route looks like this:
$app->group('/files', function (Group $group) {
$group->get('/{file_id}', GetFileAction::class);
});
The GetFileAction class has a method like this:
public function __invoke(Request $request, Response $response, $args): Response {
...Guzzle request returning $file_response here...
return $file_response;
}
My app is also using Guzzle to call the service, the call looks like this:
$guzzleClient->request(
'GET',
"{$base_url}/files/{$file_id}",
[
'auth' => [$username, $password],
'sink' => $file_path
]
);
I wondered if returning the Guzzle response in Slim might be causing some unexpected result, so I tried returning this in the service instead:
return $response->withBody(new \Slim\Psr7\Stream($file_response->getBody()->detach()));
Same result. Obviously if somebody who has run into this exact same problem can help out it would be great, but if not some pointers on how I could try to debug the handling of the streams would likely be helpful.
I've confirmed this is linked to a weird issue with the feof() function returning true even though it hasn't read the full file. The solution I came up with involved creating a different Response Emitter than the default Slim 4 one (mostly the same) and overwrite the emitBody function so it does not rely on feof(). I did so like this:
$length = min($this->responseChunkSizeCopy, $amountToRead);
while ($amountToRead > 0 && ($data = $body->read($length)) !== false) {
echo $data;
$amountToRead -= $length;
$length = min($this->responseChunkSizeCopy, $amountToRead);
if (connection_status() !== CONNECTION_NORMAL) {
break;
}
}
So far this has worked well based on my testing. I have no idea why feof() is not working as expected and didn't really find anything that seemed to specifically address it. Maybe it's a Windows specific thing, and since PHP is less common on Windows it's not a common occurrence. But leaving this solution here in case it can help someone.
I'm trying to achieve a similar goal—using Slim to proxy and forward incoming requests to another service via a Guzzle client—and encountered a similar problem when returning the Guzzle response.
In my case the problem was that the other service was incorrectly returning a Transfer-Encoding: chunked header in the response.
Your mileage may vary, but the solution was to replace this with a correct Content-Length header in the returned response:
return $response
->withoutHeader('Transfer-Encoding')
->withHeader('Content-Length', $response->getBody()->getSize());

Using goutte to read from a file / string

I'm using Goutte to make a webscraper.
For development, I've saved a .html document I'd like to traverse (so i'm not constantly making requests to the website). Here's what I have so far:
use Goutte\Client;
$client = new Client();
$html=file_get_contents('test.html');
$crawler = $client->request(null,null,[],[],[],$html);
Which based of what I know should call request in Symfony\Component\BrowserKit, and pass in the raw body data. Here's the error message I'm getting:
PHP Fatal error: Uncaught exception 'GuzzleHttp\Exception\ConnectException' with message 'cURL error 7: Failed to connect to localhost port 80: Connection refused (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)' in C:\Users\Ally\Sites\scrape\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.
If I were to just use DomCrawler, it's non-trivial to create a crawler using a string. (see: http://symfony.com/doc/current/components/dom_crawler.html). I'm just unsure about how to do the equivalent with Goutte.
Thanks in advance.
Tools you decided to use make real http connections and are not suitable for what you want to do. At least out of the box.
Option 1: Implement your own BrowserKit Client
All goutte does is it extends BrowserKit's Client. It implements http requests with Guzzle.
All you need to do to implement your own client, is to extend the Symfony\Component\BrowserKit\Client and provide the doRequest() method:
use Symfony\Component\BrowserKit\Client;
use Symfony\Component\BrowserKit\Request;
use Symfony\Component\BrowserKit\Response;
class FilesystemClient extends Client
{
/**
* #param object $request An origin request instance
*
* #return object An origin response instance
*/
protected function doRequest($request)
{
$file = $this->getFilePath($request->getUri());
if (!file_exists($file)) {
return new Response('Page not found', 404, []);
}
$content = file_get_contents($file);
return new Response($content, 200, []);
}
private function getFilePath($uri)
{
// convert an uri to a file path to your saved response
// could be something like this:
return preg_replace('#[^a-zA-Z_\-\.]#', '_', $uri).'.html';
}
}
$client = new FilesystemClient();
$client->request('GET', '/test');
Client's request() needs to accept real URIs, therefore you need to implement your own logic to convert it to a filesystem location.
Have a look at Goutte's Client for insipration.
Option 2: Implement a custom Guzzle handler
Since Goutte uses Guzzle, you could provide your own Guzzle handler that would load responses from files, instead of making real http requests. Have a look at the handlers and middleware doc.
If you're just after caching responses so you make less http requests, Guzzle provides support for this already.
Option 3: Use DomCrawler directly
new Crawler(file_get_contents('test.html'))
The only drawback is you'll loose some of convenience methods of the BrowserKit client, like click() or selectLink().

Call SOAP API using php

I have one WSDL file. file url is given below
http://home.nutselect.nl/services/DossierService.svc?wsdl
I need to get the order details from this wsdl file. but when call this soap WSDL file i got the empty window only.
Any one please help me. How to get the Order details from this soap WSDL file.
try {
$client = new SoapClient('http://home.nutselect.nl/services/DossierService.svc?wsdl');
$response = $client->Request();
print_r($response);
Saravana all you need to do is generate a php class form the wsdl. and when you inherit the class you can probably get the public methods.
Check this link for the php class generator
https://github.com/wsdl2phpgenerator/wsdl2phpgenerator

PHP error when try execute .net webservice method

I need communicate with web-service construct in .net(i think), but when i try call a method, return this error:
The SOAP action specified on the message, '', does not match the HTTP SOAP Action, 'http://tempuri.org/TripointWebservicesVersionedGeneral/Ping'.
Code to connect to web-service and execute method:
$client = new SoapClient(
'...?wsdl',
array(
'trace' => 1,
'soap_version' => SOAP_1_2
)
);
//$test = $client->__soapCall('Ping',array(''));
$test = $client->Ping();
What is the reason of this error? or what i need to do to call a method?
I have already read this PHP Fatal error: "The SOAP action specified on the message, '', does not match the HTTP SOAP Action", and put in option of soap connection this "'action' => '.../Ping'" but don't help me.
i've found the reason why i can't call the method's, its because of this:
One of the problems:
The PHP returns "Caught exception: Cannot process the message because the content type ‘text/xml; charset=utf-8′ was not the expected type ‘application/soap+xml; charset=utf-8′."
The reason:
WCF is running a new version of web services than expected by the PHP.
The solution:
Change the binding type of the WCF service from binding="wsHttpBinding" to binding="basicHttpBinding".
This insures that your .NET web service would support clients and other services that conform to the WS-I standards.
url source:
http://spacebug.com/php-calling-wcf-part-1-html
Not a full answer, but I just wanted to highlight that you're probably facing this problem because you're using SoapClient to try and consume a web service that uses WS-Addressing. You should try to track down what others do in this situation:
http://www.cdatazone.org/index.php?/archives/15-WS-Addressing-for-extsoap.html
https://github.com/wso2/wsf

using functions from internal SOAP interface via php

I received a manual to internal SOAP interface of my partner. It says:
MyPARTNER web services are provided in the form of a SOAP interface. The service is available in this URL: https://justsomeurl.com:435/soap
then some bla bla about authorization etc. and then a part about Accessible Methods:
pull()
The PULL method is used for pulling data from the database. The method
receives a unique data based parameter under an internal name
requestXML. This parameter contains data in a structured XML format.
String pull(String requestXML)
The XML contains data required to make the request, and the response
data is sent back.
then some other methods, error codes, it's not important here...
The problem is that I'm totally unexperienced in SOAP so I don't know how to use this interface via PHP. I've tried to find some examples, tutorials and I am now little bit more informed about SOAP and its functionality but still haven't found any advice about how to use interface like this...
thanx for any help
Php comes with PHP SOAP libraries, that usually are included and enabled after a common php installation.
Yuo are asked to biuld the client part of the webservice pattern. Your partner should provide you the .wsdl of the web service. The wsdl describes the avialble method, the parameters they need and what they return.
Tipically parameters and return values are array structures
This could be a skeleton for your code:
//build a client for the service
$client = new SoapClient("partner.wsdl");
//$client is now a sort of object where you can call functions
//prepare the xml parameter
$requestXML = array("parameter" => "<xml>Hello</xml>");
//call the pull function this is like
$result = $client->__soapCall("pull", $requestXML );
//print the value returned by the web service
print_r($result);
Here follows a non-wsdl example
First the location paramater is the address the SOAP request will be sent to.
The uri parameter is the target namespace of the SOAP service. This is related to xml namespaces.
A sample code for you could be:
//for URI specification you should watch your partners documentation. maybe also a fake uri (like mine) could work
//build a client for the service
$client = new SoapClient(null, array(
'location' =>
"https://justsomeurl.com:435/soap",
'uri' => "urn:WebServices",
'trace' => 1 ));
// Once built a non-wsdl web service works as a wsdl one
//$client is now a sort of object where you can call functions
//prepare the xml parameter
$requestXML = array("parameter" => "<xml>Hello</xml>");
//call the pull function this is like
$result = $client->__soapCall("pull", $requestXML );
//print the value returned by the web service
print_r($result);
Here a useful link: http://www.herongyang.com/PHP/SOAP-Use-SOAP-Extension-in-non-WSDL-Mode.html

Categories