0

For context, I do have 2 Laravel projects on my local unit. 1 for the app and 1 for internal API. The app will send API request to the internal API. I expected for the Laravel homestead to work smoothly but I encountered a problem wherein I keep on getting null response from internal API.

I tried to add exception to VerifyCsrfToken class as commented here.

I also tried to clear cache by running

php artisan optimize:clear

and vagrant provision

vagrant reload --provision

The goal is for the app to have a successful response from internal API. Please help me, I am stuck.

On my internal API project (email-sender-api - routes/api.php).

Route::get('/tasks', function () {
    return 'test';
});

On my app project (email-sender-app - routes/web.php).

Route::get('/test', function () {
    $url = 'https://email-sender-api.local.com/api/tasks';

    // Initialize cURL session
    $curl = curl_init();

    // Set cURL options
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);

    // Execute the GET request
    $response = curl_exec($curl);

    // Check for errors
    if ($response === false) {
        $error = curl_error($curl);
        echo "Error: " . $error;
    } else {
        // Process the response
        $data = json_decode($response, true);
        // Assuming the response is in JSON format, you can decode it into an associative array
        
        // Close the cURL session
        curl_close($curl);
        
        // Display the response
        dd('response', $data);
    }
});

enter image description here

My setup

  1. /etc/hosts (C drive)
192.168.56.56 email-sender-api.local.com
192.168.56.56 email-sender-app.local.com
  1. Homestead.yaml
---
ip: "192.168.56.56"
memory: 2048
cpus: 2
provider: virtualbox

authorize: C:/Users/mena/.ssh/id_rsa.pub

keys:
    - C:/Users/mena/.ssh/id_rsa

folders:
    - map: D:/mena/email-sender/email-sender-app
      to: /home/vagrant/code/email-sender/email-sender-app
    - map: D:/mena/email-sender/email-sender-api
      to: /home/vagrant/code/email-sender/email-sender-api

sites:
    - map: email-sender-app.local.com
      to: /home/vagrant/code/email-sender/email-sender-app/public
      php: "8.1"
      type: "apache"
    - map: email-sender-api.local.com
      to: /home/vagrant/code/email-sender/email-sender-api/public
      php: "8.1"
      type: "apache"

databases:
    - homestead
    - task

features:
    - mysql: true
    - mariadb: false
    - postgresql: false
    - ohmyzsh: false
    - webdriver: false

services:
    - enabled:
          - "mysql"
          - "redis-server"
#    - disabled:
#        - "postgresql@11-main"

#ports:
#    - send: 33060 # MySQL/MariaDB
#      to: 3306
#    - send: 4040
#      to: 4040
#    - send: 54320 # PostgreSQL
#      to: 5432
#    - send: 8025 # Mailhog
#      to: 8025
#    - send: 9600
#      to: 9600
#    - send: 27017
#      to: 27017

kamer
  • 11
  • 3
  • 1
    I am not sure what Laravel version you are using, but if you are using Laravel 7.x+, please do switch to the [`Http` facade](https://laravel.com/docs/10.x/http-client), do not use `curl_xxxx` again. Then, I have never used `homestead` so I am not sure if it works like Docker or not (network), so I have no idea if you can specify a domain (that is local) or not. So you are getting a `500` with a `null` response? – matiaslauriti May 22 '23 at 17:17
  • 2
    I don't think you're getting a null response from the api. Your api is returning the string `test`. Your app is then calling `json_decode()` on that string and dumping the result of the `json_decode()`. However, `test` is not a valid json string, so the `json_decode()` result is `null`. Make sure you `dd()` the response before you do any processing on it to see what the actual response was. – patricus May 22 '23 at 20:31
  • What I did was obey what you guys suggested matiaslauriti and patricus. I used Laravel's HTTP facade and updated the response of the internal API to JSON. It is now working tho I revert things out just like what I wanted to (.local.com). Thanks a lot for helping! – kamer May 23 '23 at 02:04

0 Answers0