|
|
curl_exec (PHP 4 >= 4.0.2, PHP 5) curl_exec -- Выполняет запрос CURL Описаниеmixed curl_exec ( resource ch )
Эта функция вызывается после инициализации сеанса и установки всех
необходимых параметров. Именна эта функция фактически выполняет
требуемую операцию.
Пример 1.
Инициализация сеанса CURL и загрузка web-страницы
|
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
?>
|
|
Замечание:
Если вам нужно, чтобы эта функция вернула результат, а не вывела
его в браузер, используйте опцию CURLOPT_RETURNTRANSFER с функцией
curl_setopt().
cameron at bulock dot com
21-Oct-2007 10:14
A slightly more efficient way to return the HTTP code would be to use the curl_getinfo function
function getHttpResponseCode($url)
{
$ch = @curl_init($url);
@curl_setopt($ch, CURLOPT_HEADER, TRUE);
@curl_setopt($ch, CURLOPT_NOBODY, TRUE);
@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
@curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$status = array();
$status = @curl_getinfo($ch);
return $status['http_code'];
}
Florian Holzhauer
12-Sep-2007 02:58
If you use apache2+mod_chroot with php5, add
LoadFile /lib/libnss_dns.so.2
to your mod_chroot config - this should resolver problems.
shanto at ultimawebsolutions dot com
17-Aug-2007 09:01
A function to retrieve the status code of an HTTP request using CURL:
function getHttpResponseCode($url)
{
$ch = @curl_init($url);
@curl_setopt($ch, CURLOPT_HEADER, TRUE);
@curl_setopt($ch, CURLOPT_NOBODY, TRUE);
@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
@curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$status = array();
$response = @curl_exec($ch);
preg_match('/HTTP\/.* ([0-9]+) .*/', $response, $status);
return $status[1];
}
test at test dot com
13-Aug-2007 06:43
If you see a "0" at the end of the output, you might want to switch to HTTP/1.0:
curl_setopt($ch, CURLOPT_HTTP_VERSION, 1.0);
lukasl at ackleymedia dot com
06-Oct-2006 07:52
Thank you for sharing this. I was wondering why my result was 1.
To get around this in a safe way, this is how I check if the result is valid.
$ch = curl_init(); /// initialize a cURL session
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$xmlResponse = curl_exec ($ch);
curl_close ($ch);
if (!is_string($xmlResponse) || !strlen($xmlResponse)) {
return $this->_set_error( "Failure Contacting Server" );
} else {
return $xmlResponse;
}
04-Oct-2006 01:41
Be careful when using curl_exec() and the CURLOPT_RETURNTRANSFER option. According to the manual and assorted documentation:
Set CURLOPT_RETURNTRANSFER to TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
When retrieving a document with no content (ie. 0 byte file), curl_exec() will return bool(true), not an empty string. I've not seen any mention of this in the manual.
Example code to reproduce this:
<?php
$url = 'http://www.example.com/empty_file.txt';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$str = curl_exec($curl);
curl_close($curl);
var_dump($str);
?>
|