|
curl_setopt
Set an option for a cURL transfer
Description
bool curl_setopt
( resource $ch
, int $option
, mixed $value
)
Parameters
-
ch
-
A cURL handle returned by
curl_init.
-
option
-
The CURLOPT_XXX option to set.
-
value
-
The value to be set on option .
value should be a bool for the
following values of the option parameter:
value should be an integer for the
following values of the option parameter:
value should be a string for the
following values of the option parameter:
value should be an array for the
following values of the option parameter:
value should be a stream resource (using
fopen, for example) for the following values of the
option parameter:
value should be the name of a valid function or a Closure
for the following values of the option parameter:
Other values:
Return Values
Returns TRUE on success or FALSE on failure.
Examples
Example #1 Initializing a new cURL session and fetching a web page
<?php // create a new cURL resource $ch = curl_init();
// set URL and other appropriate options curl_setopt($ch, CURLOPT_URL, "http://www.example.com/"); curl_setopt($ch, CURLOPT_HEADER, false);
// grab URL and pass it to the browser curl_exec($ch);
// close cURL resource, and free up system resources curl_close($ch); ?>
Example #2 Uploading file (deprecated as of PHP 5.5.0)
<?php
/* http://localhost/upload.php: print_r($_POST); print_r($_FILES); */
$ch = curl_init();
$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_SAFE_UPLOAD, false); // required as of PHP 5.6.0 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch); ?>
The above example will output:
Array
(
[name] => Foo
)
Array
(
[file] => Array
(
[name] => test.png
[type] => image/png
[tmp_name] => /tmp/phpcpjNeQ
[error] => 0
[size] => 279
)
)
Notes
Note:
Passing an array to CURLOPT_POSTFIELDS will
encode the data as multipart/form-data,
while passing a URL-encoded string will encode the data as
application/x-www-form-urlencoded.
|