最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Homestead Laravel to call Prestashop on host machine - Stack Overflow

programmeradmin0浏览0评论

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

  • the prestashop base64 key in the member variable $this->apiKey
  • client code: the host prestashop installation answers to $this->ps_ip_address (this value being 10.0.2.2), the laravel-to-prestashop (homestead-to-hostmachine) curl written as follows:
  $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?

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

  • the prestashop base64 key in the member variable $this->apiKey
  • client code: the host prestashop installation answers to $this->ps_ip_address (this value being 10.0.2.2), the laravel-to-prestashop (homestead-to-hostmachine) curl written as follows:
  $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?

Share Improve this question asked Mar 18 at 14:46 Luigi IaderosaLuigi Iaderosa 631 silver badge7 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

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.

发布评论

评论列表(0)

  1. 暂无评论