【问题标题】:How to fetch website data using CURL in octobercms backend?如何在 octobercms 后端使用 CURL 获取网站数据?
【发布时间】:2018-06-19 07:55:32
【问题描述】:

我想在我的 octobercms 后端使用 CURL 请求从另一个网站获取一些数据(HTML)。我不知道我该怎么做。谁能帮帮我?

【问题讨论】:

  • 本手册展示了许多您可以使用的示例:php.net/manual/en/book.curl.php 这是您实现它的唯一需要。 laravel 或十月与它无关。它不依赖于它。
  • @VR Patel 找到任何解决方案?
  • 是的@MuhammadAwaisZulifqar,我允许通过更改根目录 .htaccess 文件来执行 PHP 文件并使用 PHP curl 脚本获取数据。

标签: php laravel curl octobercms octobercms-backend


【解决方案1】:

OctoberCms 提供 curl 包装类 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);
 });

【讨论】:

    【解决方案2】:

    首先从这里安装 GuzzleHttp 找到所有详细信息: https://github.com/guzzle/guzzle

    然后在Controller或组件中包含这个:

    use GuzzleHttp\Client; 
    

    然后你可以像这样编码,这里有一些示例代码:

    $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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-24
      • 2021-04-02
      • 1970-01-01
      • 1970-01-01
      • 2018-05-18
      • 1970-01-01
      • 2017-02-11
      相关资源
      最近更新 更多