oscarotero / Embed

Get info from any web service or page
MIT License
2.08k stars 310 forks source link

No ResponseFactoryInterface detected #367

Closed AliHassan13 closed 4 years ago

AliHassan13 commented 4 years ago

i'm just install this with composer require embed/embed use it in my controller use Embed\Embed this is my test function just for demo

public function test()
    {
        $embed = new Embed();

        //Load any url:
        $info = $embed->get('https://www.thenews.com.pk/latest/604982-prince-charles-disgusted-his-father-prince-philip-for-crying-easily');
        return response()->json($info->title,200);

    }

i got this error and i don't know how i fix it.

RuntimeException
No ResponseFactoryInterface detected
Mombuyish commented 4 years ago

duplicate #342

Mombuyish commented 4 years ago

Use $ composer require laminas/laminas-diactoros and works.

Also, I found slim/http and guzzle/psr7 that doesn't work, I'll check it later.

Mombuyish commented 4 years ago

I think I figure out what happened to this problem, you need to implement factories by yourself.

For example, you wanna use guzlehttp/psr7, for you, entry code likes:


// Implementing by yourself.
$client = new CurlClient(new ResponseFactory());
$request = new RequestFactory();
$uri = new UriFactory();

$crawler = new Crawler($client, $request, $uri);
$embed = new Embed($crawler);
$info = $embed->get('https://google.com');

Next, you need to step by step to implement what you need Factories.

RequestFactory

use GuzzleHttp\Psr7\Request;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;

class RequestFactory implements RequestFactoryInterface
{
    public function createRequest(string $method, $uri): RequestInterface
    {
        return new Request($method, $uri);
    }
}

ResponseFactory

use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;

class ResponseFactory implements ResponseFactoryInterface
{

    public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface
    {
        return (new Response())
            ->withStatus($code, $reasonPhrase);
    }
}

UriFactory

use GuzzleHttp\Psr7\Uri;
use Psr\Http\Message\UriFactoryInterface;
use Psr\Http\Message\UriInterface;

class UriFactory implements UriFactoryInterface
{

    public function createUri(string $uri = ''): UriInterface
    {
        return new Uri($uri);
    }
}

And then it should work, I suggest the documentation changing description:

Embed comes with a CURL client compatible with PSR-18 but you need to install a PSR-7 / PSR-17 library. Here you can see a list of popular libraries and the library can detect automatically 'laminas\diactoros', 'guzzleHttp\psr7', 'slim\psr7', 'nyholm\psr7' and 'sunrise\http' (in this order). If you want to use a different PSR implementation, you can do it in this way:

Embed\Http\FactoryDiscovery.php detect automatically should be changing, Excepting Laminas\Diactoros have implemented these factories, others no.

AliHassan13 commented 4 years ago

Thanks a lot.. i just install $ composer require laminas/laminas-diactoros and its work.

oscarotero commented 4 years ago

@Mombuyish guzzle/psr7 works with the version 2.x (not stable yet). Version 1.x does not include factories. A workaround is using one of the http-factories provided here: https://github.com/http-interop