laravelprestashophomestead

Homestead Laravel to call Prestashop on host machine


I am in need to know how I could perform api calls from a laravel application under Homestead to Prestashop Apis on host machine.

Let's assume that I have

  $curl = curl_init();
        $curlOptUrl = 'http://'.$this->ps_ip_address.'/api/addresses/3';
        var_dump($curlOptUrl);
        curl_setopt_array($curl, array(
        CURLOPT_URL => $curlOptUrl,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'GET',
        CURLOPT_HTTPHEADER => array(
            'Authorization: Basic '.$this->apiKey,
        ),
        ));

returns

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL was not found on this server.</p>
<hr>
<address>Apache/2.4.52 (Ubuntu) Server at prestashop.test Port 80</address>
</body></html>

I have also edited /etc/hosts file on homestead machine in order to assign a url to the host ip (10.0.2.2 prestashop.test) in order to match the url of prestashop installation on host machine, but didn't get any luck.

The very same api call responds correctly if executed from Postman on host. I believe it's a configuration problem, but I'm not too educated about configurations. Can anyone help?


Solution

  • Should anyone occur into my same problem:

    1. Make sure that, on your host machine, your prestashop installation runs under 127.0.0.1 (10.0.2.2 is a "whitehole" leading to localhost only)

      1. To do so, go in your hostmachine and put 127.0.0.1 nameofprestashopapp.test in the etc/hosts file

      2. in etc/apache2/sites-available make sure that the tag "VirtualHost" is formatted like so: <VirtualHost 127.0.0.1:80> (the rest of the configurations remains the same)

    2. As reported in OP, make sure you have in the Homestead etc/hosts an entry like 10.0.2.2 nameofprestashopapp.test

    3. When referring to prestashop installation in Laravel code running under Homestead (i.e. a call to Prestashop api), make sure you refer by http://nameofprestashopapp.test/api/<resource>

    This way you should be able to connect Homestead Laravel to host machine served Prestashop.

    Hope it helps.