Web студия "GrandView"
  Главная   Написать Контакты
   
   
О проекте
Руководство php
 

curl_exec

(PHP 4 >= 4.0.2, PHP 5)

curl_exec -- Выполняет запрос CURL

Описание

mixed curl_exec ( resource ch )

Эта функция вызывается после инициализации сеанса и установки всех необходимых параметров. Именна эта функция фактически выполняет требуемую операцию.

Пример 1. Инициализация сеанса CURL и загрузка web-страницы

<?php
// инициализация сеанса
$ch = curl_init();

// установка URL и других необходимых параметров
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().



curl_getinfo> <curl_error
Last updated: Sat, 27 Jan 2007
 
add a note add a note User Contributed Notes
curl_exec
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

   
// fictional URL to an existing file with no data in it (ie. 0 byte file)
   
$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);

   
// execute and return string (this should be an empty string '')
   
$str = curl_exec($curl);

   
curl_close($curl);

   
// the value of $str is actually bool(true), not empty string ''
   
var_dump($str);

?>
J
Новости
11 июля 2007
Сайт запущен
© 2007 info@grandviewstudio.com

Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/sites/grandviewstudiocom/www/65f67d67a94ad980786580ae69e11c07/sape.php on line 324

Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/sites/grandviewstudiocom/www/65f67d67a94ad980786580ae69e11c07/sape.php on line 330
Z058440144362 Z348613067571