Passing Variables from php to a WCF Web service, variables are blank - php

I have a WCF Service, the interfaces work fine when connecting with a c# application but when I connect using a PHP application all variables passed to the service are null.
This is the PHP code used to connect to the service and send the data.
$SelectedFolder = $_REQUEST['folder'];
var_dump($SelectedFolder);
try
{
$client = new SoapClient('http://localhost:8663/Service.svc?wsdl');
$Files = $client->GetAllLatestVersionsString($SelectedFolder);
}
The var dump displays the following
string 'Pictures/Sample/' (length=16)
This is the service code
[OperationContract]
List<VersionedFileDataModel> GetAllLatestVersionsString(string partUri);
I've tried passing a static value instead of a variable and both times the value received by the service is null.
Thanks in Advance for any help,
Matt

Fixed it, I was required to pass the variables through using an array of params, for the example I posted I had to change the code to this.
try
{
$client = new SoapClient('http://localhost:8663/Service.svc?wsdl');
$params->partUri = $SelectedFolder;
$Files = $client->GetAllLatestVersionsString($params);
}

Related

Q&A: How to get POST variables with PHP on Alibaba Cloud Function Compute service

I played around with the PHP 7.2 runtime and HTTP trigger on Alibaba Cloud Function Compute. The basic example in the documentation is the following:
<? php
use RingCentral\Psr7\Response;
function handler($request, $context): Response{
/*
$body = $request->getBody()->getContents();
$queries = $request->getQueryParams();
$method = $request->getMethod();
$headers = $request->getHeaders();
$path = $request->getAttribute("path");
$requestURI = $request->getAttribute("requestURI");
$clientIP = $request->getAttribute("clientIP");
*/
return new Response(
200,
array(
"custom_header1" => "v1"
),
"hello world"
);
}
This works quite well. It's easy to get the query parameters from an URL. But the body content is only available in a whole string with
$request->getBody()->getContents();
Although the documentation says that the $request parameter follows the PSR-7 HTTP Message standard, it is not possible to use $request->getParsedBody() to deliver the values submitted by POST method. It didn't work as expected - the result remains empty.
The reason is the underlying technology. Alibaba Cloud Function Compute makes use of the event-driven React PHP library to handle the requests (you can check this by analyzing the $request object). So the $_POST array is empty and there is no "easy way to get POST data".
Luckily, Alibaba's Function Compute handler provides the body content by $request->getBody()->getContents(); as a string like
"bar=lala&foo=bar"
So a solution seems easiser than thought at the beginning, you can e.g. use PHP's own parse_str() function:
$data = [];
$body = $request->getBody()->getContents();
parse_str($body,$data);
If you place this snippet in the handler function, the POST variables are stored in the $data array and ready for further processing.
Hope that this helps somebody who asked the same questions than I. :-)
Kind regards,
Ralf
As you can see in the documentation you need to add a RequestBodyParserMiddleware as middleware to get a parsed PSR-7 request. It seems you didn't do that.
Also keep in mind that only the Content-Types: application/x-www-form-urlencoded and multipart/form-data are supported here. So make sure the client need to send these headers so the request can be parsed. If it's another Content-Type you need to use another middleware.
See: https://github.com/reactphp/http#requestbodyparsermiddleware for more information.
I hope this helps!
#legionth: I apologize that I didn't use the comment feature here, but my answer is too long. :-)
Thanks a lot for your comments - the usage of RequestBodyParserMiddleware is a great solution if you can control the server code. But in the context of Alibaba Cloud Function Compute service this seems not possible. I tried to find out more information about the invocation process - here are my results:
Function Compute makes use of the Docker image defined in https://github.com/aliyun/fc-docker/blob/master/php7.2/run/Dockerfile .
In the build process they download a PHP runtime environment from https://my-fc-testt.oss-cn-shanghai.aliyuncs.com/php7.2.tgz . (I didn't find this on GitHub, but the code is public downloadable.)
A shell script start_server.sh starts a PHP-CGI binary and runs a PHP script server.php.
In server.php a React\Http\Server is started by:
$server = new Server(function (ServerRequestInterface $request) {
[...]
});
[...]
$socket = new \React\Socket\Server(sprintf('0.0.0.0:%s', $port), $loop);
$server->listen($socket);
$loop->run();
As seen in the Function Compute documentation (& example of FC console), I can only use two functions:
/*
if you open the initializer feature, please implement the initializer function, as below:
*/
function initializer($context) {
}
and the handler function you can find in my first post.
Maybe Alibaba will extend the PHP runtime in future to make it possible to use a custom middleware, but currently I didn't find a way to do this.
Thanks again & kind regards,
Ralf

How do you get the HTTP host with Laravel 5

I'm trying to get the hostname from an HTTP request using Laravel 5, including the subdomain (e.g., dev.site.com). I can't find anything about this in the docs, but I would think that should be pretty simple. Anyone know how to do this?
Good news! It turns out this is actually pretty easy, although Laravel's Request documentation is a bit lacking (the method I wanted is inherited from Symfony's Request class). If you're in a controller method, you can inject the request object, which has a getHttpHost method. This provides exactly what I was looking for:
public function anyMyRoute(Request $request) {
$host = $request->getHttpHost(); // returns dev.site.com
}
From anywhere else in your code, you can still access the request object using the request helper function, so this would look like:
$host = request()->getHttpHost(); // returns dev.site.com
If you want to include the http/https part of the URL, you can just use the getSchemeAndHttpHost method instead:
$host = $request->getSchemeAndHttpHost(); // returns https://dev.site.com
There two ways, so be careful:
<?php
$host = request()->getHttpHost(); // With port if there is. Eg: mydomain.com:81
$host = request()->getHost(); // Only hostname Eg: mydomain.com
laravel 5.6 and above
request()->getSchemeAndHttpHost()
Example of use in blade :
{{ request()->getSchemeAndHttpHost() }}
You can use request()->url();
Also you can dump the complete request()->headers();
And see if that data is useful for you.

Issue with String response in Retrofit

I am using Retrofit in my android app to communicate with my server. In one of my server calls I am expecting a String response from server. So, I declare a callback which expects a string value. Callback<String>. In the php, I echo a string. Say echo "test"; When I hit the url in browser, the echo works as expected test. But in my android app, failure callback is called.
I tried changing the php to echo "\"test\"";
On browser : "test"
On android : success callback is called.
I solved it by declaring a variable.
Php:
$result = "test";
echo $result;
Browser: test
Android : success callback is called.
My question is, is this how Retrofit works? Or am I doing anything wrong? Also, to solve this is there any way other than declaring a variable?
Callback<String> does not make a lot of sense in the context of retrofit. By default retrofit operates using GSON.
What you are actually waiting for from the server is json deserialized into a POJO (simple java object).
Lets say you have a data model (POJO) like:
public class User {
public final String name;
}
Then you'd use a callback like this Callback<User>. And from the server you should do: echo '{ "name" : "Simon" }';
In your success callback you will have an instance of User class with the name field set to 'Simon'.
More on this here: http://square.github.io/retrofit/

PHP: SOAP webservice client to ASP.NET webservice server

I was trying to connect to asp.net webservice from PHP,
I dont want to use nuSOAP
I have created SOAP client using default SoapClient()
$options = array('style'=>SOAP_DOCUMENT,
'use'=>SOAP_LITERAL,
'soap_version'=>SOAP_1_1,
'exceptions'=>1,
'trace'=>1
);
$clnt = new SoapClient('webserviceURL?wsdl', $options);
$clnt ->__Call('method', array('param'=>'val'));
Now, Webservice server is not recogising my Parameter that I am passing to the webservice method.
Can Anyone help me ?
If the webservice expects document/literal wrapped calling convention then you should put method parameters inside additional array:
$clnt ->__Call('method', array(array('param'=>'val')));
Yes, I got the Answer
$params = array('param'=>'val');
$resp = $clnt->method(array('param'=>$params));
'method' is webservice method you want to call
Method mentioned by Furgas will also work

Troubles with Zend Rest Client when trying to call a Web Service developed in ASP and sending a parameter as array

I have this call to a Web Service that is developed with ASP:
$endpoint = Zend_Registry::get('config')->endpoint->services->myService;
$client = new Zend_Rest_Client($endpoint);
$client->userId($adminUserId);
$client->otherIds($otherIds);
$result = $client->get();
But when I try to call the service the parameter 'otherIds' is not been taken by the WS.
That is because the first called function apparently (as I grok from the source code) is chosen for the "method" parameter. The Zend REST server seems to take this format. I suggest for other servers to feed a dummy method to the client, so the first call should be
$client->Dummy();
After that, the arguments are set.

Categories