A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::open($save_path, $name) should either be compatible with SessionHandlerInterface::open(string $path, string $name): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 132

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::close() should either be compatible with SessionHandlerInterface::close(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 290

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::read($session_id) should either be compatible with SessionHandlerInterface::read(string $id): string|false, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 164

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::write($session_id, $session_data) should either be compatible with SessionHandlerInterface::write(string $id, string $data): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 233

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::destroy($session_id) should either be compatible with SessionHandlerInterface::destroy(string $id): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 313

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::gc($maxlifetime) should either be compatible with SessionHandlerInterface::gc(int $max_lifetime): int|false, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 354

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_cookie_params(): Session cookie parameters cannot be changed after headers have already been sent

Filename: Session/Session.php

Line Number: 296

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Session ini settings cannot be changed after headers have already been sent

Filename: drivers/Session_files_driver.php

Line Number: 108

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_save_handler(): Session save handler cannot be changed after headers have already been sent

Filename: Session/Session.php

Line Number: 110

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_start(): Session cannot be started after headers have already been sent

Filename: Session/Session.php

Line Number: 143

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

Logo
Code Ranks ×

PHP cURL (Getting Content Of Any URL)|coderanks

02/04/2019  .   2 minutes, 18 seconds to read  .   Posted by Admin
#php #curl #phpcurl #learnphp #phpframework #phpcrud #phptutorial #phpforms #phpcode #phpfunctions #phpcodeigniter #phpprogramming #database

What is php curl ?

cURL is a PHP library that is used to call different URLs using different methods(GET, POST, DELETE etc) and protocols. This library is very helpful when we need to load URLs programmatically to perform different actions.

Following is a set of use cases for which we can use PHP cURL.

  1. Getting Content Of Any URL
  2. File Downloading
  3. Checking URL existence
  4. Form Submission
  5. Authentication
  6. Cookies saving and sending

In this part of the article, we'll cover the first point to download the content of any URL.

Getting Content Of Any URL 

Content is anything that is visible when we open any URL. We can download that content programmatically by using cURL. We use this normally in scraping or API Calling. Here is a basic example of content downloading.

<?php
    $curl=curl_init();
    curl_setopt($curl,CURLOPT_URL,'http://www.coderanks.com');
    curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
    $response = curl_exec($curl);
    curl_close($curl);
    echo $response;
?>

Explanation of PHP cURL Common Functions

PHP cURL has different functions to add different flags and components in the request. Following are commonly used functions.

curl_init()

This function initializes a new cURL request and returns an object which we can use for further processing. In this case, $curl is an object returned by curl_init(). 

$curl=curl_init();

curl_setopt()

This function is used to add different components in the curl object. It normally takes the following arguments.

  1. Curl Object
  2. Component Key
  3. Component Value

In the above code, we use curl_setopt two times to set URL and Return Transfer flag to 1.

curl_setopt($curl,CURLOPT_URL,'http://www.coderanks.com');
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);

curl_exec()

As the name depicts, this function is used to execute the curl request by taking the curl object as an argument. After execution completes, it'll display the response but if CURLOPT_RETURNTRANSFER is set to 1(true) then it'll return the response as follows.

$response = curl_exec($curl);

curl_close()

After executing curl request, we need to close it by calling curl_close().

curl_close($curl)

Setting all options at once

We don't need to call curl_setopt for each option but we can pass an array of options by using curl_setopt_array. By using this approach, our code will look like this:

<?php
    $curl=curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => "http://www.coderanks.com",
        CURLOPT_RETURNTRANSFER => true,
    ));
    $response = curl_exec($curl);
    curl_close($curl);
    echo $response;
?>
1 vs true in PHP cURL

As in most cases in programming, we can use 1 or true interchangeably in PHP cURL options.

This is all for this 1st part of the article, we'll study File Downloading in the next part. 

Thanks for reading.