Error code 500: Internal server error (ReactPHP) - server

I tried to make a server file for my ReactPHP app following this video but when I started up the server, it ran successfully, but when I made a simple http GET the response was "Error code
500: Internal server error", when in theory it should've returned a JSON {"message": "Hello"}.
Here is the code for the server.php file:
use React\Http\Server;
use React\Http\Response;
use Psr\Http\Message\ServerRequestInterface;
use \React\EventLoop\Factory;
require 'vendor/autoload.php';
$loop = Factory::create();
$server = new Server(function (ServerRequestInterface $request) {
return new Response(
200, ['Content-Type' => 'application/json'], json_encode(['message' => 'Hello'])
);
});
$socket = new \React\Socket\Server('127.0.0.1:8000', $loop);
$server->listen($socket);
echo "Listening on ".str_replace('tcp', 'http', $socket->getAddress()). PHP_EOL;
$loop->run();
request.http file:
GET 127.0.0.1:8000
What the request has returned:
HTTP/1.1 500 Internal Server Error
Content-Type: text/plain
Server: ReactPHP/1
Date: Fri, 20 Aug 2021 09:03:19 GMT
Content-Length: 32
Connection: close
Error 500: Internal Server Error
Can someone say to me what the problem is? I think I miswrote something in the server.php file but I am not the one to tell

Code you use from a video is a bit outdated.
If you need a quickfix - just replace one line and it will work:
--- use React\Http\Response;
+++ use React\Http\Message\Response;
Working code for this example (as for react/http-1.5.0) would be
use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Loop;
use React\Http\HttpServer;
use React\Http\Message\Response;
use React\Socket\SocketServer;
require_once __DIR__ . '/vendor/autoload.php';
$loop = Loop::get();
$server = new HttpServer(function (ServerRequestInterface $request) {
return new Response(
200, ['Content-Type' => 'application/json'], json_encode(['message' => 'Hello'])
);
});
$socket = new SocketServer('127.0.0.1:8000');
$server->listen($socket);
echo 'Listening on ' . str_replace('tcp', 'http', $socket->getAddress()) . PHP_EOL;
$loop->run();
List of changes:
Response class location (actual fix)
loop factory changed, deprecation upFactory::create() -> Loop::get
http-server changed React\Http\Server -> React\Http\HttpServer
socket-server changed React\Socket\Server -> React\Socket\SocketServer

The answer provided by Ilya Urvachev is the right one, basically react evolved and the Response class is not located in the same location anymore.
Overall after instantiating your server I recommend you to use this line of code so you get error messages in your command line interface:
$server->on('error', function (Exception $exception) {
echo $exception->getMessage() . PHP_EOL;
});
All in all, It's been very difficult for me to move from regular PHP to reactphp, which completely destroy the verbosity of error messages. Ie. You've got exception telling you that the format of the answer is not the expected one, while, what really fucks up your code is that have an error in the syntax of one of your MySQL queries, which sends back a message that is not respecting the expecting answer's format. If someone wants to provide additional way to improve the reactphp verbosity, feel free :)

Related

Laravel Mail::Send works on local apache but complains about mime on nginx

I have installed a laravel package for user verification
https://github.com/jrean/laravel-user-verification
Register Controller needs to be changed to work like below.
public function register(Request $request)
{
$this->validator($request->all())->validate();
$user = $this->create($request->all());
$this->guard()->login($user);
UserVerification::generate($user);
UserVerification::send($user, 'Verification Mail from example.com');
return redirect()->intended('/home');
}
Used mailgun to send mails.It worked fine on local apache. sent mails successfully to gmail and others.
Installed on nginx production(ubuntu 16.04 VPS ). It throws following error.
FatalThrowableError in SimpleMessage.php line 33:
Type error: Argument 1 passed to Swift_Mime_SimpleMessage::__construct() must
be an instance of Swift_Mime_HeaderSet, none given, called in /var/www/example.com/
html/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Message.php on line 48
To check if it is a package problem I have also done the following which works on local apache but throws the same error on nginx.
Route::get('/mailsend', function() {
$data = [
'title' => 'hello user',
'content' => 'Thanks for joining us'
];
Mail::send('mails.test', $data , function($message) {
$message->to('example#gmail.com','example')->subject('Testing from example');
});
});
The code as shown in error /Swift/Message.php in line 48 as follows
call_user_func_array(
array($this, 'Swift_Mime_SimpleMessage::__construct'),
Swift_DependencyContainer::getInstance()
->createDependenciesFor('mime.message')
);
Could not figure out the problem with nginx. Any help would be greatly appreciated.
Try uncomment line: extension=php_openssl.dll on php.ini
and check this https://laravel.io/forum/05-13-2014-smtp-mail-not-working-in-live-server

Getting HTTP response from balanced

If I am using PHP code such as
$card = Balanced\Card::get("/v1/marketplaces/TEST-MP4K6K0PWGyPtXL4LZ42sQSb/cards/CC5N3HHUDrAyvhNwQOoUd3UX");
$card->unstore();
or
$customer->addCard($card)
how do I read the HTTP response from balanced to know if it has worked or what the error is?
The Balanced client libraries are written so that they will throw exceptions if there is a non 2xx HTTP response from the API.
The correct way to tell if the addCard operation failed for example would be to write some code that looks like
try {
$customer->addCard($card)
} catch (Balanced\Error $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}

Magento SOAP 2 API Fatal error: Procedure 'login' not present

I am getting: Fatal error: Procedure 'login' not present in /chroot/home/mystore/mystore.com/html/lib/Zend/Soap/Server.php on line 832
This is where the error is coming from
$soap = $this->_getSoap();
ob_start();
if($setRequestException instanceof Exception) {
// Send SOAP fault message if we've catched exception
$soap->fault("Sender", $setRequestException->getMessage());
} else {
try {
$soap->handle($request);
} catch (Exception $e) {
$fault = $this->fault($e);
$soap->fault($fault->faultcode, $fault->faultstring);
Any Ideas on how to fix the error?
I had the same issue, and which I did to fix it was to go to System/Configuration/Magento Core API and set the value WS-I Compliance as 'No'.
I'm working with a WebService which consumes the Magento V2 API, I don't recall if I generate the web reference using this value as 'Yes'; I'm working with a WS C# using VS 2010.
I had similar problem and I did not want to change the API version. Deleting the WSDL cache helped me.
Run this to get the WSDL cache folder:
php -i | grep soap
From the result you can see that the WDSL cache is enabled and stored in /tmp:
soap
soap.wsdl_cache => 1 => 1
soap.wsdl_cache_dir => /tmp => /tmp
soap.wsdl_cache_enabled => 1 => 1
soap.wsdl_cache_limit => 5 => 5
soap.wsdl_cache_ttl => 86400 => 86400
Remove the cache and run it again:
sudo rm -rf /tmp/*
I found the clue in this article - http://artur.ejsmont.org/blog/content/php-soap-error-procedure-xxx-not-present

Using Soap in magento

I'm trying to follow the information about how to use soap in magento, but always get same message in error.log
If any one experience something similar, that could give me some tip, it will be welcome.
"PHP Fatal error: SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://www.site.com/index.php/api/?wsdl' : failed to load external entity "http://www.site.com/index.php/api/?wsdl"\n in /var/www/test.php on line 1"
$client = new SoapClient('http://www.site.com/api/?wsdl');
$session = $client->login('apiUser', 'apiKey');
$result = $client->call($session, 'somestuff.method');
$result = $client->call($session, 'somestuff.method', 'arg1');
$result = $client->call($session, 'somestuff.method', array('arg1', 'arg2', 'arg3'));
$result = $client->multiCall($session, array(
array('somestuff.method'),
array('somestuff.method', 'arg1'),
array('somestuff.method', array('arg1', 'arg2'))
));
// If you don't need the session anymore
$client->endSession($session);
where you have www.site.com in your SOAP code, replace it with localhost or whatever the correct URL is for your server. You'll also need to replace somestuff.method with real objects and methods as per the Magento documentation

stream_socket_server: Client browser randomly aborting?

Below is partial code to an experimental http server app I'm building from scratch from a PHP CLI script (Why? Because I have too much time on my hands). The example below more closely matches PHP's manual page on this function. The problem I'm getting is when connecting to this server app via a browser (Firefox or IE8 from two separate systems tested so far), the browser sends an empty request payload to the server and aborts roughly every 1 in 6 page loads.
The server console displays the "Connected with [client info]" each time. However, about 1 in 6 connections will result in a "Client request is empty" error. No error is given telling the header/body response write to the socket failed. The browser will generally continue to read what I give it, but this isn't usable as I can't fulfill the client's intended request without knowing what it is.
<?php
$s_socket_uri = 'tcp://localhost:80';
// establish the server on the above socket
$s_socket = stream_socket_server($s_socket_uri, $errno, $errstr, 30) OR
trigger_error("Failed to create socket: $s_socket_uri, Err($errno) $errstr", E_USER_ERROR);
$s_name = stream_socket_get_name($s_socket, false) OR
trigger_error("Server established, yet has no name. Fail!", E_USER_ERROR);
if (!$s_socket || !$s_name) {return false;}
/*
Wait for connections, handle one client request at a time
Though to not clog up the tubes, maybe a process fork is
needed to handle each connection?
*/
while($conn = stream_socket_accept($s_socket, 60, $peer)) {
stream_set_blocking($conn, 0);
// Get the client's request headers, and all POSTed values if any
echo "Connected with $peer. Request info...\n";
$client_request = stream_get_contents($conn);
if (!$client_request) {
trigger_error("Client request is empty!");
}
echo $client_request."\n\n"; // just for debugging
/*
<Insert request handling and logging code here>
*/
// Build headers to send to client
$send_headers = "HTTP/1.0 200 OK\n"
."Server: mine\n"
."Content-Type: text/html\n"
."\n";
// Build the page for client view
$send_body = "<h1>hello world</h1>";
// Make sure the communication is still active
if ((int) fwrite($conn, $send_headers . $send_body) < 1) {
trigger_error("Write to socket failed!");
}
// Response headers and body sent, time to end this connection
stream_socket_shutdown($conn, STREAM_SHUT_WR);
}
?>
Any solution to bring down the number of unintended aborts down to 0, or any method to get more stable communication going? Is this solvable on my server's end, or just typical browser behavior?
I tested your code and it seems I got better results reading the socket with fread(). You also forgot the main loop(while(1), while(true) or for(;;).
Modifications to your code:
stream_socket_accept with #stream_socket_accept [sometimes you get warnings because "the connected party did not properly respond", which is, of course, the timeout of stream_socket_accept()]
Added the big while(1) { } loop
Changed the reading from the socket from $client_request = stream_get_contents($conn);
to while( !preg_match('/\r?\n\r?\n/', $client_request) ) { $client_request .= fread($conn, 1024); }
Check the source code below (I used 8080 port because I already had an Apache listening on 80):
<?php
$s_socket_uri = 'tcp://localhost:8080';
$s_socket = stream_socket_server($s_socket_uri, $errno, $errstr, 30) OR
trigger_error("Failed to create socket: $s_socket_uri, Err($errno) $errstr", E_USER_ERROR);
$s_name = stream_socket_get_name($s_socket, false) OR
trigger_error("Server established, yet has no name. Fail!", E_USER_ERROR);
if (!$s_socket || !$s_name) {return false;}
while(1)
{
while($conn = #stream_socket_accept($s_socket, 60, $peer))
{
stream_set_blocking($conn, 0);
echo "Connected with $peer. Request info...\n";
// $client_request = stream_get_contents($conn);
$client_request = "";
// Read until double \r
while( !preg_match('/\r?\n\r?\n/', $client_request) )
{
$client_request .= fread($conn, 1024);
}
if (!$client_request)
{
trigger_error("Client request is empty!");
}
echo $client_request."\n\n";
$headers = "HTTP/1.0 200 OK\n"
."Server: mine\n"
."Content-Type: text/html\n"
."\n";
$body = "<h1>hello world</h1><br><br>".$client_request;
if ((int) fwrite($conn, $headers . $body) < 1) {
trigger_error("Write to socket failed!");
}
stream_socket_shutdown($conn, STREAM_SHUT_WR);
}
}
Add sleep(1) after stream_set_blocking