how to send request to an external api

Posted 2 months ago by michaelnguyen547

At the lowest level, I can use curl to an external API server.

For some reasons, I can not do the same with Illuminate\Http\Request.

Here is my example


use Illuminate\Http\Request;

        $request = Request::create(
            'https://google.ca/search=laravel',
            'GET'
        );

$response = app()->handle($request);

The request is sent to myserver/search=laravel , not to google.ca/search=laravel.

I don't know why? Does Request not work for external API?

Here is the dump of Request object

Request {#1169 ▼
  #json: null
  #convertedFiles: null
  #userResolver: null
  #routeResolver: null
  +attributes: ParameterBag {#1194 ▶}
  +request: ParameterBag {#1176 ▶}
  +query: ParameterBag {#1175 ▶}
  +server: ServerBag {#1177 ▼
    #parameters: array:17 [▼
      "SERVER_NAME" => "google.ca"
      "SERVER_PORT" => 443
      "HTTP_HOST" => "google.ca"
      "HTTP_USER_AGENT" => "Symfony"
      "HTTP_ACCEPT" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
      "HTTP_ACCEPT_LANGUAGE" => "en-us,en;q=0.5"
      "HTTP_ACCEPT_CHARSET" => "ISO-8859-1,utf-8;q=0.7,*;q=0.7"
      "REMOTE_ADDR" => "127.0.0.1"
      "SCRIPT_NAME" => ""
      "SCRIPT_FILENAME" => ""
      "SERVER_PROTOCOL" => "HTTP/1.1"
      "REQUEST_TIME" => 1541090718
      "PATH_INFO" => ""
      "REQUEST_METHOD" => "GET"
      "HTTPS" => "on"
      "REQUEST_URI" => "/search=laravel"
      "QUERY_STRING" => ""
    ]
  }
  +files: FileBag {#1182 ▶}
  +cookies: ParameterBag {#1179 ▶}
  +headers: HeaderBag {#1170 ▶}
  #content: null
  #languages: null
  #charsets: null
  #encodings: null
  #acceptableContentTypes: null
  #pathInfo: null
  #requestUri: null
  #baseUrl: null
  #basePath: null
  #method: null
  #format: null
  #session: null
  #locale: null
  #defaultLocale: "en"
  -isHostValid: true
  -isForwardedValid: true
  pathInfo: "/search=laravel"
  requestUri: "/search=laravel"
  baseUrl: ""
  basePath: ""
  method: "GET"
  format: "html"
}

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.