Post byte array from PHP to .NET WCF Service - c#

I got a WCF service with a method to receive files, looking something like this
public bool UploadFile(string fileName, byte[] data)
{
//...
}
What I'd like to do is to post the data to this method in the WCF service from PHP, but are unaware if it's even possible to post byte arrays from PHP to a .NET method hosted by a WCF service.
So I was thinking of something like this
$file = file_get_contents($_FILES['Filedata']['tmp_name']); // get the file content
$client = new SoapClient('http://localhost:8000/service?wsdl');
$params = array(
'fileName' => 'whatever',
'data' => $file
);
$client->UploadFile($params);
Would this be possible or are there any general recommendations out there I should know about?

Figured it out.
The official php documentation tells that the file_get_contents returns the entire file as a string (http://php.net/manual/en/function.file-get-contents.php). What noone tells is that this string is compatible with the .NET bytearray when posted to a WCF service.
See example below.
$filename = $_FILES["file"]["name"];
$byteArr = file_get_contents($_FILES['file']['tmp_name']);
try {
$wsdloptions = array(
'soap_version' => constant('WSDL_SOAP_VERSION'),
'exceptions' => constant('WSDL_EXCEPTIONS'),
'trace' => constant('WSDL_TRACE')
);
$client = new SoapClient(constant('DEFAULT_WSDL'), $wsdloptions);
$args = array(
'file' => $filename,
'data' => $byteArr
);
$uploadFile = $client->UploadFile($args)->UploadFileResult;
if($uploadFile == 1)
{
echo "<h3>Success!</h3>";
echo "<p>SharePoint received your file!</p>";
}
else
{
echo "<h3>Darn!</h3>";
echo "<p>SharePoint could not receive your file.</p>";
}
} catch (Exception $exc) {
echo "<h3>Oh darn, something failed!</h3>";
echo "<p>$exc->getTraceAsString()</p>";
}
Cheers!

Related

Flutter VideoPlayerController network request from web api

I am making a flutter app and using the VideoPlayerController library package and requesting video content via network:
VideoPlayerController newController = VideoPlayerController.network(
"http://192.168.1.1:9999/S3/get-object/name-of-video.mp4");
My Web API Backend is .NET Core 3 and the controller endpoint is this:
[AllowAnonymous]
[HttpGet("get-object/{url}")]
public async Task<FileStreamResult> GetObject(string url)
{
// Seperate out only the filename
string[] res = url.Split(new string[] { "%2F" }, StringSplitOptions.None);
string fileName = res.LastOrDefault();
Stream imageStream = await S3Helper.ReadObjectData(_appSettings, fileName);
Response.Headers.Add("Content-Disposition", new ContentDisposition
{
FileName = fileName,
Inline = true // false = prompt the user for downloading; true = browser to try to show the file inline
}.ToString());
if (fileName.Contains(".jpg") || fileName.Contains(".jpeg"))
{
return File(imageStream, "image/jpeg");
}
else if (fileName.Contains(".png"))
{
return File(imageStream, "image/png");
}
else if (fileName.Contains(".mp4"))
{
return File(imageStream, new MediaTypeHeaderValue("video/mp4").MediaType, true);
}
else
{
return null;
}
}
However, when I create a widget that uses a Network image, it actually works. I'm not sure what the difference is.
CachedNetworkImage(
imageUrl: "http://192.168.1.1:9999/S3/get-object/name-of-image.jpg",
placeholder: (context, url) =>
CircularProgressIndicator(),
errorWidget: (context, url, error) =>
Icon(Icons.error),
fit: BoxFit.contain,
),
The .Net Core Backend has the video coming via an http get request as an inline video, similar to this one:
https://flutter.github.io/assets-for-api-docs/assets/videos/butterfly.mp4
The error I get from my flutter app shows up like this:
Source error. E/ExoPlayerImplInternal(24687): com.google.android.exoplayer2.upstream.HttpDataSource$HttpDataSourceException: Unable to connect to http://192.168.1.1:9999/S3/get-object/name-of-video.mp4
I don't know about ios. But android doesn't allow http://. You need to provide a link starting https://.
To allow the http:// or others
add this line on androids AndroidManifest.xml.
Location android\app\src\main\AndroidManifest.xml
android:usesCleartextTraffic="true"
This should look like this:

Codeigniter:Soap Client call works well in PHP not in C#

I created a SOAP server in codeigniter and here is my controller
class Sample extends CI_Controller {
function __construct()
{
parent::__construct();
$ns = base_url();
$this->load->library("Nusoap_library");
$this->load->library("Master");
$this->server = new soap_server(); // create soap server object
$this->server->configureWSDL("SOAP", $ns); // wsdl
$this->server->wsdl->schemaTargetNamespace = $ns; // server namespace
}
public function index()
{
$ns = base_url();
$input_array = array ('type' => "xsd:string"); // "addnumbers" method parameters
$return_array = array ("fruit" => "xsd:string");
$this->server->register('Master.fruits', $input_array, $return_array, "urn:SOAPServerWSDL", "urn:".$ns."/addnumbers", "rpc", "encoded", "Fruit Types");
$this->server->service(file_get_contents("php://input")); // read raw data from request body
}
public function client(){
$this->client = new nusoap_client(base_url()."index.php?wsdl", true);
$this->load->view("client");
}
}
the server is works very well, and i created a Mater class as library
Master.php [library]
class Master {
public function fruits($type)
{
switch($type)
{
case 'red':
return "Apple";
break;a
case 'yellow':
return "banana";
break;
}
}
}
As you can see the controller, i also created a client function to test whether the client working fine
here is my client View code
$error = $this->client->getError();
if ($error) {
echo "<h2>Constructor error</h2><pre>" . $error . "</pre>";
}
$result = $this->client->call("Master.fruits", array("type" => "red"));
if ($this->client->fault) {
echo "<h2>Fault</h2><pre>";
print_r($result);
echo "</pre>";
}
else {
$error = $this->client->getError();
if ($error) {
echo "<h2>Error</h2><pre>" . $error . "</pre>";
}
else {
echo "<h2>Fruits</h2><pre>";
echo $result;
echo "</pre>";
}
}
i am getting result perfectly in php while calling the SOAP server but if the same SOAP server is called via C# by creating service
i am getting errors something like it should not be like Master.fruits it should be like masterfruits and also i am getting errors in content Type

How to use cookies with php soap client?

i am connecting to webservice using php.
try {
$client = new SoapClient("https://.....",array(
'exceptions' => true
));
} catch ( SoapFault $e ) {
echo 'sorry... our service is down';
}
in c# it is working, while in binding for that is set allowCookies="true".
how and where should i add this allowCookies="true", if i want to use php soap client?
thank you.
You should use SoapClient::__setCookie()

C# SOAP object to PHP

I am trying to use WDSL SOAP in PHP. The initial connection seems to work fine but I am struggling to 'convert' some C# to PHP, in particular headers.
AreaSearchRequest request = new AreaSearchRequest();
request.GUID = "1234";
request.Location = "UK";
// Create AreaSearchHeader, assign AreaSearchRequest
AreaSearchHeader header = new AreaSearchHeader();
header.Request = request;
header.Validate = false;
// SOAP connection
soap.Open();
// Call the AreaSearch method response object
AreaSearchResponse response = soap.AreaSearch(header);
//Close API connection
soap.Close();
And here is my rough translation into PHP.
$wsdl = "https://whatever/";
$options = array(
'trace' => 1,
);
$client = new SoapClient($wsdl, $options);
$request = array(
'GUID' => '1234',
'Location' => 'UK',
);
$client->__soapCall('AreaSearch', $request);
What is really throwing me off is the header stuff to make a valid request! Thanks (sorry, I have no experience of C# whatsoever).
try using nusoap https://sourceforge.net/projects/nusoap/ to do the heavy lifting. I dont use PHP myself but a have a load of c# soap services that people I work with use nusoap to consume and they haven't had any issues

Php, Soap & ASMX WebService

I'm trying to call an AspNet WebService (C# 3.5) from PHP (5.3.5) using NuSoap.
<?php
require_once('lib/nusoap.php');
$proxyhost = isset($_POST['proxyhost']) ? $_POST['proxyhost'] : '';
$proxyport = isset($_POST['proxyport']) ? $_POST['proxyport'] : '';
$proxyusername = isset($_POST['proxyusername']) ? $_POST['proxyusername'] : '';
$proxypassword = isset($_POST['proxypassword']) ? $_POST['proxypassword'] : '';
$client = new nusoap_client("http://localhost:53096/MyWebService.asmx?wsdl", 'wsdl', $proxyhost, $proxyport, $proxyusername, $proxypassword);
$callParams= array(
'token' => "Token"
, 'ppp' => array(1,2,3)
);
$result = $client->call("MyWebMethod", $callParams);
?>
My WebMethod is very simple:
[WebMethod()]
public int[] MyWebMethod(string token, int[] ppp)
{
return ppp;
}
When I set a breakpoint in VisualStudio 2008, token is OK but ppp = empty array of int (not null).
Any idea of what is wrong with my code?
i don't know about nusoap, but the native PHP SoapClient class has a method to dump out the last XML request sent to the web service, this is probably a good place to start
If the second parameter name is callParams, then you should use the same name while calling method from php (not 'ppp').

Categories

Resources