-1

I want to get some data(HTML) from another website using CURL request in my octobercms backend. I don't know how do I do. Can anyone help me?

Script47
  • 14,230
  • 4
  • 45
  • 66
VR Patel
  • 61
  • 1
  • 1
  • 11

2 Answers2

1

OctoberCms provide a curl wrapper class Http

https://github.com/octobercms/library/blob/master/src/Network/Http.php

 Http::get('http://octobercms.com');
 Http::post('...');
 Http::delete('...');
 Http::patch('...');
 Http::put('...');
 Http::options('...');

 $result = Http::post('http://octobercms.com');
 echo $result;                          // Outputs: <html><head><title>...
 echo $result->code;                    // Outputs: 200
 echo $result->headers['Content-Type']; // Outputs: text/html; charset=UTF-8
 Http::post('http://octobercms.com', function($http){
      // Sets a HTTP header
 $http->header('Rest-Key', '...');
      // Set a proxy of type (http, socks4, socks5)
 $http->proxy('type', 'host', 'port', 'username', 'password');
      // Use basic authentication
 $http->auth('user', 'pass');
      // Sends data with the request
 $http->data('foo', 'bar');
 $http->data(['key' => 'value', ...]);
      // Disable redirects
 $http->noRedirect();
      // Check host SSL certificate
 $http->verifySSL();
      // Sets the timeout duration
 $http->timeout(3600);
      // Write response to a file
 $http->toFile('some/path/to/a/file.txt');
      // Sets a cURL option manually
 $http->setOption('CURLOPT_SSL_VERIFYHOST', false);
 });
1

First install the GuzzleHttp from here find all details: https://github.com/guzzle/guzzle

Then include this in Controller or component:

use GuzzleHttp\Client; 

Then You can code like, here some example code:

$token='API key or token ';
    $request_url = 'https://www.example.com/get?parsams=params&token='.$token;
    $client = new Client();
    $response = $client->request('GET', $request_url, [
        'headers' => ['Accept' => 'application/xml'],
        'timeout' => 120
    ])->getBody()->getContents();