Php curl curlopt connecttimeout

PHP: Setting cURL timeout options.

This is a short guide on how to use the cURL timeout options in PHP. In certain cases, you may want to specify a timeout in order to prevent your HTTP request from taking too long.

There are two cURL timeout options that you need to know about. These options are slightly different from one another, so I will explain them now:

  • CURLOPT_CONNECTTIMEOUT: The maximum amount of seconds that cURL should spend attempting to connect to a given URL.
  • CURLOPT_TIMEOUT: The maximum amount of seconds it should take for all cURL operations to be carried out. i.e. The maximum amount of time that the request should take.

Take a look at the following piece of code.

Пример #2 Закачка файла

$data = array( ‘name’ => ‘Foo’ , ‘file’ => ‘@/home/user/test.png’ );

curl_setopt ( $ch , CURLOPT_URL , ‘http://localhost/upload.php’ );
curl_setopt ( $ch , CURLOPT_POST , 1 );
curl_setopt ( $ch , CURLOPT_POSTFIELDS , $data );

Результат выполнения данного примера:

Array ( [name] => Foo ) Array ( [file] => Array ( [name] => test.png [type] => image/png [tmp_name] => /tmp/phpcpjNeQ [error] => 0 [size] => 279 ) )

Примечания

Замечание:

Передача массива в CURLOPT_POSTFIELDS закодирует данные в виде multipart/form-data, тогда как передача URL-кодированной строки закодирует данные в виде application/x-www-form-urlencoded.

Смотрите также

Источник

Оцените статью