I have a model in codeigniter which contains a function that pings servers and stores data in an external server. I am trying to use my main server to call the function on the slave server. Here is the function:
public function check_online($ip, $port){
if(!$socket = stream_socket_client('tcp://'.$ip.':'.$port, $this->timeout)) {
return FALSE;
} else {
return TRUE;
}
}
Help would be very greatly appreciated.
What you're describing seems to be a remote invocation of a function on a different machine.
Usually, this requires you to set up a function stub at your client end and set up a channel. But since you're using PHP, you can use a simple POST request to accomplish the same.
Here is an example: PHP: Remote Function Call and returning the result?
However, if the remote function only "stores" the data in (say) MySQL, then I would suggest you to re-design your application so that you don't need a separate function on a different server to do the store operation.
Related
I'm using the Laravel Swoole Coroutine go function to do a HTTP request in order to achieve better performance. When I get the response from the external service, a new entry is created in the database with the data from the external service.
I want to be able to return a response from the controller with that new DB record. However, it appears that anything outside the go does not have access to anything that got assigned in the go function. I understand that that happens in a separate thread, but is there a way to implement this so that I can have access to the results inside the go function?
Please note that I have coroutine enabled globally and I only to use function as shown below:
public function store(User $user, Request $request) {
go(function () {
// get data from external API using Laravel HTTP Client
...
$user = User:create($data);
return response($user, 201)->send();
});
}
I have tried using the WaitGroup(), but it complains that the event loop has already been started if I wrap it with the Co\run function.
I have a Power Delivery Unit device connected to my local network, that is controlled by a simple web page and I´m trying to use Laravel Schedule to turn the device on and off at specific times of the day. So I´ve created a PDUController with the following methods: (both approaches work fine)
public function on()
{
return redirect()->away(
"http://192.168.0.100/control_outlet.htm?outlet6=1&outlet7=1&op=0&submit=Apply"
);
}
public function off()
{
return Redirect::to(
"http://192.168.0.100/control_outlet.htm?outlet6=1&outlet7=1&op=1&submit=Apply"
);
}
If you visit the routes:
Route::get('/pdu/on', [PDUController::class, 'on'])->name('pdu.on');
Route::get('/pdu/off', [PDUController::class, 'off'])->name('pdu.off');
Everything works as expected and you can turn the device on or off accordingly. Now when I use the Scheduler on Kernel.php like:
protected function schedule(Schedule $schedule)
{
$schedule->call('App\Http\Controllers\PDUController#on')->everyMinute();
}
It doesn´t work.
I know the flow is right, because I can Log::debug() every step of the process correctly, but the device is not receiveing the signal and thus is not turning on.
Could you help, please? Thanks!
The Redirect functions return a redirect response to the browser. Since the scheduler runs through Artisan, there is no browser to redirect, so no connection is made. Instead, use the HTTP Client to create a connection to the page.
I have some PHP script where I invoke method from the external class. I want to run this method asynchronously. I don't want to block rest of the program. This method does some work in the background and return nothing so there is no need to wait while it finished. Is there a way to do this in PHP?
# get post data from user
$postData = $this->f3->get('POST');
# start of asynchronous part
$obj = new asyncClass();
$obj->init($postData);
# end of asynchronous part
# do some work with post data
$soc = new someOtherClass($postData);
$result = $soc->send();
# assign result of work to variable
$this->f3->set('var', $result);
# show it to user
$this->f3->set('view', 'view.html');
If this can help, I'm using Fat Free Framework and PHP 5.6 Non-Thread Safe
You can use $f3->abort() to send the output/response to the browser and process your other blocking function afterwards. That's not a real asynchron solution but would work. You could also use something like php-icicle to add threads support, but that maybe requires some other php modules being installed.
Use threading.
class PostDataHandlerAsync extends Thread
{
private $postData
public function __construct($postData)
{
$this->postData = $postData;
}
public function run()
{
/*
Your code goes here
*/
}
}
$postData = $this->f3->get('POST');
$obj = new PostDataHandlerAsync($postData);
$obj->run();
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'm currently working on a small application that works like this:
When the user clicks a link, an Ajax GET request is fired.
The request hits a server-side PHP script.
The script, which requests information for another domain, retrieves a JSON feed.
The feed is then echoed back to the client for parsing.
I'm not really a PHP developer, so I am looking for some best practices with respect to cross-domain requests. I'm currently using file_get_contents() to retrieve the JSON feed and, although it's functional, it seems like a weak solution.
Does the PHP script do anything other than simply call the other server? Do you have control over what the other server returns? If the answers are No and Yes, you could look into JSONP.
You might want to abstract the retrieval process in PHP with an interface so you can swap out implementations if you need too. Here is a naive example:
interface CrossSiteLoader
{
public function loadURL($url);
}
class SimpleLoader implements CrossSiteLoader
{
public function loadURL($url)
{
return file_get_contents($url);
}
}
Comes in handy if you need to test locally with your own data because you can use a test implementation:
public ArrayLoader implements CrossSiteLoader
{
public function loadURL($url)
{
return json_encode(array('var1' => 'value1', 'var2' => 'value2'));
}
}
or if you just want to change from file_get_contents to something like curl