symfony/mercure

Publisher throw error "failed to open stream: operation failed"

rafrsr opened this issue · 2 comments

Using the publisher or the following code, same as used in the Publisher class.
I got the error failed to open stream: operation failed

$url = 'https://..../hub';
$jwt = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXJjdXJlIjp7InN1YnNjcmliZSI6W10sInB1Ymxpc2giOltdfX0.bxavdlHOyum1AbQteaEdtJuEU0HyA2pp_s2b1HqNlN0';
$postData = 'topic=hello&data=welcome';

$result = @file_get_contents(
    $url,
    false,
    stream_context_create(
        [
            'http' => [
                'method' => 'POST',
                'header' => "Content-type: application/x-www-form-urlencoded\r\nAuthorization: Bearer $jwt",
                'content' => $postData,
            ],
        ]
    )
);

print_r($result); // null
print_r(error_get_last()['message']); //file_get_contents(https://...): failed to open stream: operation failed
exit;

But when use my own publisher using curl works fine:

$ch = curl_init($url);
curl_setopt_array(
    $ch,
    [
        CURLOPT_URL => $url,
        CURLOPT_POST => 1,
        CURLOPT_POSTFIELDS => $postData,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => [
            'Content-type: application/x-www-form-urlencoded',
            "Authorization: Bearer $jwt",
        ],
    ]
);

$server_output = curl_exec($ch);
curl_close($ch);
print_r($server_output);//e6dab35e-5158-416a-8613-b8023936bce1
exit;

I'm not sure what's the problem :X, but why not use curl in the publisher by default to do this?

Because curl may not be available, but we plan to switch to the new Symfony HttpClient component, that supports both file_get_contents and curl: #6 (comment)

Any help on this would be very welcome.

Do you think you can manage to gather more logs? It’s very hard to debug with the current error.

debugging a little more into this issue the error is: SSL: Handshake timed out in and a simple solution without the need of use curl is setting a timeout value for file_get_contents:

 $result = file_get_contents(
            $url,
            false,
            stream_context_create(
                [
                    'http' => [
                        'method' => 'POST',
                        'header' => "Content-type: application/x-www-form-urlencoded\r\nAuthorization: Bearer $jwt",
                        'content' => $postData,
                        'timeout'=> 5,
                    ],
                ]
            )
        );

print_r($result); //6c8ce48d-046c-44ff-9c9a-645c1994fbc0

now works fine