I have a php class (server class) responsible for communication between arduino and server. Class has a looping method listening to a server socket, and calling method for handling incoming messages and sending the response to arduino. It also has an information of all connected devices and their addresses.
Now I would like to make API in php that would communicate with mobile app and call server class method that would forward the message to the arduino. API part is not the problem, but the way how to trigger and send info from API to class with a looping method.
Is there a way to access class instance from Server class inside API file so I could call it's methods and use info about connected devices?
EDIT
Here is a simplyfied code example. Main file that is creating class instance:
<?php
require_once("SocketServer.class.php");
$server = new SocketServer("X.Y.Z.Y",PORT);
$server->max_clients = 10;
$server->hook("CONNECT","handle_connect");
$server->hook("INPUT","handle_input");
$server->infinite_loop();
....
and the file from which I would like to access instance $server instantiated in the previous file
<?php
include ("phpscript.php");
$server->test();
?>
Then, I start the first script and when HTTP request is sent to the second one (.htaccess is forwarding reqests to this file) using hurl.it I get the server error message, which means that there is a error in the code.
I'm expecting that looping script outputs echo from the test method of SocketServer class, but that doesn't happen.
Related
I am trying to send a SOAP request to a client's API endpoint. I am not at all familiar with SOAP, so having quite a difficult time getting this to work.
From the client's documentation
The requested ticket can be used to call all the API web methods subsequently.
public string RequestTicket(
string username,
string password
);
URL
https://www.clientsurl.net/api/v01_00/APIService.asmx?wsdl
Parameters
string username
string password
I am able to create the WSDL
$client = new Client('https://www.clientsurl.ca/api/v01_00/APIService.asmx?wsdl', ['soap_version' => SOAP_1_1]);
but not sure how to send the parameters through
$params = [
'username' => 'myusername'
'password' => 'mypassword'
];
I am also not sure what the relevance of RequestTicket is. Am I supposed to add it to the url?
The answer is probably very simple, but after tons of searching I couldn't find anything. Please help.
I have write a method to send a request
protected function soapRequest(string $method, array $arguments)
{
try {
$client = new \Zend\Soap\Client($this->getWsdl(),
[
'soap_version' => SOAP_1_1,
'cache_wsdl' => WSDL_CACHE_NONE
]);
$result = $client->{$method}($arguments);
return $result->return;
} catch (\SoapFault $s) {
...
} catch (\Exception $e) {
...
}
}
You must have a Soap method to send yours parameters.
If you don't know the method name, I advise you to run SoapUI application, very useful for debugging soap requests.
A SOAP service has a set of operations that you can call over the network. These operations can also have parameters. Basically, it's just like calling a method with parameters in code just that the invocation happens over the network with the method name and parameters being marshaled into an XML that respects the rules of the SOAP protocol.
To call the SOAP service, you can either make a HTTP request of type POST to the service's endpoint (i.e. https://www.clientsurl.ca/api/v01_00/APIService.asmx) or you can use a SOAP client. A SOAP client is some code that you can generate from the WSDL of the SOAP web service, or is some code that can dynamically read the WSDL and provide you some ways to invoke the operations described there. As opposed to making a POST HTTP request, the client takes care of these details for you and allows you to make the call over the network just like you call a local method in your code.
To call an operation of the SOAP service in your client code you have to invoke a method with parameters. The name of the method and its parameters (what names and what types) are described by the WSDL of the service.
With that being said, I'll add some details about what you posted in your question.
The requested ticket can be used to call all the API web methods subsequently.
Some service operations can require authentication in order to be be allowed to invoke them. Just like you need a username and password to access protected sections of a website for example. For a SOAP web service, his can happen in a few ways, the most common two being:
you send the username and password with each call to the web service (somehow; can be as SOAP headers, as HTTP headers with BASIC Authentication, etc).
the service exposes a method that you have to call with username and password just like point 1), but then returns an access token of some sort that you then need to provide to the rest of the web service's operations. This is just like a Login page on a website where you authenticate with username and password and then you get back a SessionID that you can use on all other requests until you decide to log out.
It seems that your service uses the second approach, and RequestTicket seems to be the operation that you need to call in order to be able to call the rest of the operations after that.
I am able to create the WSDL
You do not create the WSDL, the WSDL already exists for the web service. Also make sure you do not make a confusion between the SOAP web service and its WSDL. The code you show just creates a SOAP client from the WSDL (what I described above) to allow you to invoke operations on it.
I am also not sure what the relevance of RequestTicket is. Am I supposed to add it to the url?
Most likely RequestTicket is an operation of the web service. You should look inside the WSDL to see if it's described there. The WSDL is a little tough to swallow if you are not familiar with how it works, so your best bet is to use a tool like SoapUI to feed it the web service WSDL and have SoapUI generate sample requests for the web service. You can then also use SoapUI to test the web service to make sure you understand how it works before you try to replicate the same calls with your PHP code.
Here is my Symfony3 command that I'm using for websocket server purpose
public function __construct(ChatFlowProcessor $chatManager, int $webSocketPort)
{
$this->chatManager = $chatManager;
$this->webSocketPort = $webSocketPort;
parent::__construct();
}
$server = IoServer::factory(
new HttpServer(
new WsServer(
$this->chatManager
)
),
$this->webSocketPort
);
$server->run();
As you see, I've got a chatManager simply using Symfony3 autowiring. The service implements Ratchet MessageComponentInterface.
Now, I want to get access to the server from outside of the connection. I mean, send a message to websocket client using my chatManager, apparently I need to get access to chatManager instance, that stored in WsServer and keep information about all active connections.
Is it possible? Thanks.
You dont need an access to chatManager just to send messages. It's a chat server which only purpose IS to transfer messages from/to all its clients.
Simply create a websocket client, connect it to your server (together other clients) and start sending (and receiving) messages. Any special functionality (e.g. send message to one client only or get list of all clients etc.) must be implemented in chatManager (Ratchets MessageComponentInterface).
Perhaps I misunderstood the question, sorry then.
I am currently busy with a PSR-7 project with responses and requests.
Currently we are setting up an application in our index.php by doing something like:
$app = new Application();
$app->loadConfiguration(
'../config/global.yml',
);
// Should return the response?
$app->run((new ServerRequestFactory())->createServerRequestFromGlobals());
Here the run method also calls an emit method that is responsible for sending the headers and printing the body of the response.
The request and respons are now linked together in one call which makes it hard to test since you don't want to send the response with the headers straight to PHPUnit.
I have removed the emit call in the chain of the run method and added this to the index after the run method call:
// Send the response.
$app->send();
This way they are decoupled but the downside is I now have to hold a instance of my response in a response property inside my Application.php($app) class.
I want to move the response instance to the response class itself but my co-workers thinks a class should never hold an instance of itself. Yet when I look at frameworks this happens quite a lot. Is he right about this?
What arguments can I make to decouple my request and response besides easier testing?
I am pretty new to unit testing, one of the arguments I have already heard is that I should not test the full application anyways but rather separate components and therefore should not be worried about de-coupling the request and response.
I am new to code with NuSOAP lib. Currently, I've just accomplished connected client with server and get service.
Right now, my service or function is on the same php file as soap server. I register my function that on the same page using this code :
$server->register("myFunction");
how can I register a function if I have external php file that consist my function that I needed ?
first, you need to include your php file in your code, and before, register the method or function.
Be sure if your function need to parameters or has a return. In that case, you must inscribe your parametes and return statements using the method register of the webservice.
See here http://www.wackylabs.net/2004/07/creating-a-web-service-and-wsdl-using-nusoap/
In that example, when you see > replace by >
I have a generic HTTP file access API which I use for the system I'm working on. To make it as flexible as possible, it returns request and response data in the form of HTTP strings.
I'm currently implementing a version which interacts with the S3, via the AWS SDK for PHP 2.
Is there an easy way to quickly get the Request and Response HTTP requests which the S3Client makes when performing operations? If not, is there a more piecemeal way which I can use to not have to fake it?
Basically, I'd like the full-text of both the Request and Response on demand, or at least access to relevant data (headers, response codes, URLs, etc) so I can properly populate the return data for my framework.
Thanks.
You can get either the request or response object from a command object. Assuming $s3 holds an instance of Aws\S3\S3Client, you could do something like this:
$command = $s3->getCommand('ListObjects', array('Bucket' => '<bucket-name>'));
$request = $command->getRequest();
$response = $command->getResponse();
Those objects have methods for viewing the body, headers, status codes, etc. and you can cast them to string to see the string form.
If you want to quickly see the request and response as you are executing commands, you can attach the wire logger, and see what comes out on STDOUT (or STDERR)
$s3->addSubscriber(\Guzzle\Plugin\Log\LogPlugin::getDebugPlugin());
$s3->listObjects(array('Bucket' => '<bucket-name>'));
You will need to look into the Guzzle\Http\Client class, which is an ancestor class to S3Client, to have a look at the methods that it makes available. You can always override some of these methods in your own child of S3Client to make accessing this information easier for you.
Ultimately the data you are looking for resides in an object of class Guzzle\Http\Message\Response, which I believe is returned from Guzzle\Http\Client::send().
So perhaps in your own implementation of S3Client you can override the send() method to send the HTTP requests, then process the response data as needed.