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?
Asked
Active
Viewed 1,887 times
-1
-
this manual shows a lot of examples you can use: http://php.net/manual/en/book.curl.php this is the only thing you need to implement it. laravel or October has nothing to do with it. it doesn't depend on it. – Hardik Satasiya Jun 19 '18 at 08:31
-
@VR Patel finding any solution? – Muhammad Awais Zulifqar Jul 04 '18 at 10:02
-
yes @MuhammadAwaisZulifqar, I allow PHP file execution by making changes in root directory .htaccess file and get data using PHP curl script. – VR Patel Dec 18 '19 at 14:00
2 Answers
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);
});

Pierre-André Vullioud
- 846
- 6
- 19
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();

Muhammad Awais Zulifqar
- 192
- 2
- 16