curl_setopt

(PHP 4 >= 4.0.2, PHP 5)

curl_setoptУстанавливает параметр для сеанса CURL

Описание

bool curl_setopt ( resource $ch , int $option , mixed $value )

Устанавливает параметр для указанного сеанса cURL.

Список параметров

ch

Дескриптор cURL, полученный из curl_init().

option

Устанавливаемый параметр CURLOPT_XXX.

value

Значение параметра option.

Для следующих значений параметра option, параметр value должен быть типа bool:

Параметр Устанавливаемое значение value Замечания
CURLOPT_AUTOREFERER TRUE для автоматической установки поля Referer: в запросах, перенаправленных заголовком Location:.
CURLOPT_BINARYTRANSFER TRUE для возврата необработанного ответа при использовании константы CURLOPT_RETURNTRANSFER.
CURLOPT_COOKIESESSION TRUE для указания текущему сеансу начать новую "сессию" cookies. Это заставит libcurl проигнорировать все "сессионные" cookies, которые она должна была бы загрузить, полученные из предыдущей сессии. По умолчанию, libcurl всегда сохраняет и загружает все cookies, вне зависимости от того, являются ли они "сессионными" или нет. "Сессионные" cookies - это cookies без срока истечения, котоыре должны существовать только для текущей "сессии".
CURLOPT_CERTINFO TRUE для вывода информации о сертификате SSL в поток STDERR при безопасных соединениях. Доступна, начиная с версии PHP 5.3.2. Для корректной работы требует включенной опции CURLOPT_VERBOSE.
CURLOPT_CRLF TRUE для преобразования концов строк Unix в CRLF.
CURLOPT_DNS_USE_GLOBAL_CACHE TRUE для использования глобального кэша DNS. Этот параметр не является потокобезопасным и по умолчанию включен.
CURLOPT_FAILONERROR TRUE для тихого окончания работы, если полученный HTTP-код больше или равен 400. Поведение по умолчанию возвращает страницу как обычно, игнорируя код.
CURLOPT_FILETIME TRUE для попытки получения даты модификации удаленного документа. Это значение может быть получено с помощью параметра CURLINFO_FILETIME из функции curl_getinfo().
CURLOPT_FOLLOWLOCATION TRUE для следования любому заголовку "Location: ", отправленному сервером в своем ответе (учтите, что это происходит рекурсивно, PHP будет следовать за всеми посылаемыми заголовками "Location: ", за исключением случая, когда установлена константа CURLOPT_MAXREDIRS).
CURLOPT_FORBID_REUSE TRUE для принудительного закрытия соединения после завершения его обработки так, чтобы его нельзя было использовать повторно.
CURLOPT_FRESH_CONNECT TRUE для принудительного использования нового соединения вместо закэшированного.
CURLOPT_FTP_USE_EPRT TRUE для использования EPRT (и LPRT) при активных FTP загрузках. Используйте FALSE для того, чтобы отключить EPRT и LPRT и использовать только PORT.
CURLOPT_FTP_USE_EPSV TRUE для первоначальной пробы команды EPSV при FTP передачах. Если команда не удалась, будет произведен обратный откат к PASV. Установите в FALSE для отключения EPSV.
CURLOPT_FTP_CREATE_MISSING_DIRS TRUE для создания отсутствующих директорий, если FTP-операция обнаруживает несуществующий путь.
CURLOPT_FTPAPPEND TRUE для записи удаленного файла в конец, вместо перезаписывания его поверх существующего файла.
CURLOPT_FTPASCII Псевдоним CURLOPT_TRANSFERTEXT. Используйте его вместо этого.
CURLOPT_FTPLISTONLY TRUE для возврата только списка имен из FTP директории.
CURLOPT_HEADER TRUE для включения заголовков в вывод.
CURLINFO_HEADER_OUT TRUE для отслеживания строки запроса дескриптора. Доступен, начиная с версии PHP 5.1.3. Префикс CURLINFO_ употреблен специально.
CURLOPT_HTTPGET TRUE для сброса метода HTTP-запроса в метод GET. Так как GET используется по умолчанию, этот параметр необходим только в случае, если метод запроса был ранее изменен.
CURLOPT_HTTPPROXYTUNNEL TRUE для туннелирования через указанный HTTP-прокси.
CURLOPT_MUTE TRUE для полного отключения сообщений функций cURL.
CURLOPT_NETRC TRUE для считывания файла ~/.netrc на предмет логина и пароля для удаленного сайта, с которым устанавливается соединение.
CURLOPT_NOBODY TRUE для исключения тела ответа из вывода. Метод запроса устанавливается в HEAD. Смена этого параметра в FALSE не меняет его обратно в GET.
CURLOPT_NOPROGRESS

TRUE для отмены индиктора прогресса при передачах cURL.

Замечание:

PHP автоматически устанавливает этот параметр в TRUE, меняйте его только для отладочных целей.

CURLOPT_NOSIGNAL TRUE для игнорирования любой функции cURL, посылающей сигналы PHP процессу. Этот параметр включен по умолчанию в многопоточных SAPIs для корректной работы таймаут параметров. Добавлен в cURL 7.10.
CURLOPT_POST TRUE для использования обычного HTTP POST. Данный метод POST использует обычный application/x-www-form-urlencoded, обычно используемый в HTML-формах.
CURLOPT_PUT TRUE для загрузки файла методом HTTP PUT. Используемый файл должен быть установлен с помощью параметров CURLOPT_INFILE и CURLOPT_INFILESIZE.
CURLOPT_RETURNTRANSFER TRUE для возврата результата передачи в качестве строки из curl_exec() вместо прямого вывода в браузер.
CURLOPT_SSL_VERIFYPEER FALSE для остановки cURL от проверки сертификата узла сети. Альтернативные сверяемые сертификаты могут быть указаны с помощью параметра CURLOPT_CAINFO или директории с сертификатами, указываемой параметром CURLOPT_CAPATH. По умолчанию равно TRUE начиная с версии cURL 7.10. Дистрибутив по умолчанию устанавливается начиная с версии cURL 7.10.
CURLOPT_TRANSFERTEXT TRUE для использования ASCII режима при FTP передачах. При использовании LDAP данные возвращаются простым текстом вместо HTML. В Windows системах поток STDOUT не устанавливается в бинарный режим.
CURLOPT_UNRESTRICTED_AUTH TRUE для продолжения посылки логина и пароля при редиректах (при использовании CURLOPT_FOLLOWLOCATION), даже при изменении имени хоста.
CURLOPT_UPLOAD TRUE для подготовки к загрузке файла на сервер.
CURLOPT_VERBOSE TRUE для вывода дополнительной информации. Записывает вывод в поток STDERR, или файл, указанный параметром CURLOPT_STDERR.

Для следующих значений параметра option, параметр value должен быть типа integer:

Параметр Устанавливаемое значение value Замечания
CURLOPT_BUFFERSIZE Размер буфера, используемого при каждом чтении. Однако, нет никакой гарантии что данный запрос будет завершен. Добавлен в версии cURL 7.10.
CURLOPT_CLOSEPOLICY Либо CURLCLOSEPOLICY_LEAST_RECENTLY_USED, либо CURLCLOSEPOLICY_OLDEST. Есть еще три дополнительные константы CURLCLOSEPOLICY_, но cURL на данный момент их не поддерживает.
CURLOPT_CONNECTTIMEOUT Количество секунд ожидания при попытке соединения. Используйте 0 для бесконечного ожидания.
CURLOPT_CONNECTTIMEOUT_MS Количество миллисекунд ожидания при попытке соединения. Используйте 0 для бесконечного ожидания. Если библиотека libcurl скомпилирована с использованием стандартного системного преобразователя имен, то соединение будет по-прежнему использовать полносекундное ожидание в качестве тайм-аута с минимально допустимым тайм-аутом в 1 секунду. Добавлен в версии cURL 7.16.2. Доступно, начиная с версии PHP 5.2.3.
CURLOPT_DNS_CACHE_TIMEOUT Количество секунд, в течение которых в памяти хранятся DNS-записи. По умолчанию этот параметр равен 120 (2 минуты).
CURLOPT_FTPSSLAUTH Метод FTP аутентификации (в активном режиме): CURLFTPAUTH_SSL (сначала проверяется SSL), CURLFTPAUTH_TLS (сначала проверяется TLS) или CURLFTPAUTH_DEFAULT (cURL решает сама). Добавлен в версии cURL 7.12.2.
CURLOPT_HTTP_VERSION CURL_HTTP_VERSION_NONE (по умолчанию, CURL сама выбирает используемую версию), CURL_HTTP_VERSION_1_0 (принудительное использование HTTP/1.0), or CURL_HTTP_VERSION_1_1 (принудительное использование HTTP/1.1).
CURLOPT_HTTPAUTH

Используемые HTTP методы авторизации. Используемые параметры: CURLAUTH_BASIC, CURLAUTH_DIGEST, CURLAUTH_GSSNEGOTIATE, CURLAUTH_NTLM, CURLAUTH_ANY, and CURLAUTH_ANYSAFE.

Можно использовать побитовый оператор | (или) для комбинации нескольких методов вместе. В этом случае cURL опросит сервер на предмет поддерживаемых методов авторизации и выберет лучший из них.

CURLAUTH_ANY - это псевдоним CURLAUTH_BASIC | CURLAUTH_DIGEST | CURLAUTH_GSSNEGOTIATE | CURLAUTH_NTLM.

CURLAUTH_ANYSAFE - это псевдоним CURLAUTH_DIGEST | CURLAUTH_GSSNEGOTIATE | CURLAUTH_NTLM.

CURLOPT_INFILESIZE Ожидаемый размер файла, в байтах, при загрузке файла на удаленный сервер. Учтите, что использование этой опции не остановит дальнейшую посылку данных, превышающих это значение, так как посылаемые данные зависят от результата CURLOPT_READFUNCTION.
CURLOPT_LOW_SPEED_LIMIT Верхний порог скорости передачи данных, в байтах в секунду. Проверка происходит в течение CURLOPT_LOW_SPEED_TIME секунд, после чего PHP считает передачу слишком медленной и прерывает ее.
CURLOPT_LOW_SPEED_TIME Максимальное количество секунд, в течение которых скорость передачи не должна превышать CURLOPT_LOW_SPEED_LIMIT, иначе PHP пометит передачу как слишком медленную и прекратит ее.
CURLOPT_MAXCONNECTS Максимальное количество постоянных соединений. При достижении лимита для определения закрываемого соединения используется параметр CURLOPT_CLOSEPOLICY.
CURLOPT_MAXREDIRS Максимальное количество принимаемых редиректов. Используйте этот параметр вместе с параметром CURLOPT_FOLLOWLOCATION.
CURLOPT_PORT Альтернативный порт соединения.
CURLOPT_PROTOCOLS

Битовая маска из значений CURLPROTO_*. Данная маска ограничивает используемые libcurl протоколы. Это позволяет иметь libcurl, работающую с большим количеством протоколов, и ограничивать работу определенных передач только для некоторого их набора. По умолчанию, libcurl использует все поддерживаемые протоколы. Смотрите также параметр CURLOPT_REDIR_PROTOCOLS.

Корректные значения протоколов: CURLPROTO_HTTP, CURLPROTO_HTTPS, CURLPROTO_FTP, CURLPROTO_FTPS, CURLPROTO_SCP, CURLPROTO_SFTP, CURLPROTO_TELNET, CURLPROTO_LDAP, CURLPROTO_LDAPS, CURLPROTO_DICT, CURLPROTO_FILE, CURLPROTO_TFTP, CURLPROTO_ALL

Добавлен в версии cURL 7.19.4.
CURLOPT_PROXYAUTH Методы авторизации HTTP, используемые при соединении с прокси-сервером. Используйте те же самые битовые маски, которые были описаны у параметра CURLOPT_HTTPAUTH. В данный момент для авторизации прокси поддерживаются только CURLAUTH_BASIC и CURLAUTH_NTLM. Добавлен в версии cURL 7.10.7.
CURLOPT_PROXYPORT Номер порта прокси-сервера, к которому осуществляется соединение. Этот номер также может быть установлен с помощью параметра CURLOPT_PROXY.
CURLOPT_PROXYTYPE Либо CURLPROXY_HTTP (по умолчанию), либо CURLPROXY_SOCKS5. Добавлен в версии cURL 7.10.
CURLOPT_REDIR_PROTOCOLS Битовая маска из значений CURLPROTO_*. Данная битовая масска ограничивает протоколы используемые libcurl при редиректе (при включенном параметре CURLOPT_FOLLOWLOCATION). Это позволяет ограничить набор используемых протоколов при редиректах для некоторых передач. По умолчанию, libcurl поддерживает все протоколы, кроме FILE и SCP. В версиях, предшествовавших 7.19.4, перенаправление использовалось для всех протоколов без исключения. Смотрите также описание параметра CURLOPT_PROTOCOLS для списка констант со значениями протоколов. Добавлен в версии cURL 7.19.4.
CURLOPT_RESUME_FROM Смещение начала передачи, в байтах.
CURLOPT_SSL_VERIFYHOST Используйте 1 для проверки существования общего имени в сертификате SSL. Используйте 2 для проверки существования общего имени и также его совпадения с указанным хостом. В боевом окружении значение этого параметра должно быть 2 (установлено по умолчанию).
CURLOPT_SSLVERSION Используемая версия SSL (2 или 3). По умолчанию, PHP попытается определить ее сам, хотя в некоторых случаях ее нужно устанавливать вручную.
CURLOPT_TIMECONDITION Способ трактовки параметра CURLOPT_TIMEVALUE. Используйте CURL_TIMECOND_IFMODSINCE для возвращения страницы, только если она была изменена со времени, указанного в параметре CURLOPT_TIMEVALUE. Если страница не была изменена, вернется заголовок "304 Not Modified", подразумевая, что параметрCURLOPT_HEADER установлен в TRUE. Используйте CURL_TIMECOND_IFUNMODSINCE для обратного эффекта. По умолчанию используется CURL_TIMECOND_IFMODSINCE.
CURLOPT_TIMEOUT Максимально позволенное количество секунд для выполнения cURL-функций.
CURLOPT_TIMEOUT_MS Максимально позволенное количество миллисекунд для выполнения cURL-функций. Если libcurl собрана с использованием обычного системного распознавателя имен, то этот промежуток соединения все еще будет использовать секундное округление таймаутов, с минимально разрешенным таймаутом в одну секунду. Добавлен в версии cURL 7.16.2. Доступен, начиная с версии PHP 5.2.3.
CURLOPT_TIMEVALUE Количество секунд, начиная с 1 января 1970 года. Это время будет использовано параметром CURLOPT_TIMECONDITION. По умолчанию, используется параметр CURL_TIMECOND_IFMODSINCE.
CURLOPT_MAX_RECV_SPEED_LARGE Если скорость скачки превысит это значение (указанное в байтах в секунду) в среднем в течение всей передачи, то скачка будет приостановлена для поддержания средней скорости меньше либо равной данному параметру. По умолчанию скорость не ограничивается. Добавлен в версии cURL 7.15.5. Доступен, начиная с версии PHP 5.4.0.
CURLOPT_MAX_SEND_SPEED_LARGE Если загрузка на сервер превысит это значение (указанное в байтах в секунду) в среднем в течение всей передачи, то загрузка будет приостановлена для поддержания средней скорости меньше либо равной данному параметру. По умолчанию скорость не ограничивается. Добавлен в версии cURL 7.15.5. Доступен, начиная с версии PHP 5.4.0.
CURLOPT_SSH_AUTH_TYPES Битовая маска, состоящая из одной или более констант: CURLSSH_AUTH_PUBLICKEY, CURLSSH_AUTH_PASSWORD, CURLSSH_AUTH_HOST, CURLSSH_AUTH_KEYBOARD. Установите CURLSSH_AUTH_ANY для того, чтобы libcurl выбрал одну из них самостоятельно. Добавлено в cURL 7.16.1.

Для следующих значений параметра option, параметр value должен быть типа string:

Параметр Устанавливаемое значение value Замечания
CURLOPT_CAINFO Имя файла, содержащего один или более сертификатов, с которыми будут сверяться узлы. Этот параметр имеет смысл только при использовании совместно с CURLOPT_SSL_VERIFYPEER. Требует абсолютный путь.
CURLOPT_CAPATH Директория, содержащая несколько CA сертификатов. Используйте этот параметр совместно с CURLOPT_SSL_VERIFYPEER.
CURLOPT_COOKIE Содержимое заголовка "Cookie: ", используемого в HTTP-запросе. Обратите внимание, что несколько cookies разделяются точкой с запятой с последующим пробелом (например, "fruit=apple; colour=red")
CURLOPT_COOKIEFILE Имя файла, содержащего cookies. Данный файл должен быть в формате Netscape или просто заголовками HTTP, записанными в файл. Если в качестве имени файла передана пустая строка, то cookies сохраняться не будут, но их обработка все еще будет включена.
CURLOPT_COOKIEJAR Имя файла, в котором будут сохранены все внутрение cookies текущей передачи после закрытия дескриптора, например, после вызова curl_close.
CURLOPT_CUSTOMREQUEST

Собственный метод запроса, используемый вместо "GET" или "HEAD" при выполнении HTTP-запроса. Это полезно при запросах "DELETE" или других, более редких HTTP-запросах. Корректными значениями будут слова наподобие "GET", "POST", "CONNECT" и так далее; т.е. не вводите здесь всю строку с HTTP-запросом. Например, указание "GET /index.html HTTP/1.0\r\n\r\n" будет неправильным.

Замечание:

Не используйте эту возможность пока не убедитесь, что сервер поддерживает данный тип запроса.

CURLOPT_EGDSOCKET Наподобие CURLOPT_RANDOM_FILE, за исключением того, что имя файла устанавливается в сокет Entropy Gathering Daemon.
CURLOPT_ENCODING Содержимое заголовка "Accept-Encoding: ". Это позволяет декодировать запрос. Поддерживаемыми кодировками являются "identity", "deflate" и "gzip". Если передана пустая строка, "", посылается заголовок, содержащий все поддерживаемые типы кодировок. Добавлен в версии cURL 7.10.
CURLOPT_FTPPORT Значение, которое будет использоваться для определения IP-адреса для команды "PORT" FTP-протокола. Команда "PORT" сообщает серверу, с каким IP-адресом он должен устанавливать соединение. Это может быть IP-адрес, имя хоста, имя сетевого интерфейса (под Unix), или просто '-' для использования системного IP-адреса по умолчанию.
CURLOPT_INTERFACE Имя используемого сетевого интерфейса. Может быть именем интерфейса, IP адресом или именем хоста.
CURLOPT_KEYPASSWD Пароль, который требуется для использования приватного ключа CURLOPT_SSLKEY или CURLOPT_SSH_PRIVATE_KEYFILE. Добавлено в cURL 7.16.1.
CURLOPT_KRB4LEVEL Уровень безопасности KRB4 (Kerberos 4). Любое из следующих значений (в порядке от слабого к самому сильному) корректно: "clear", "safe", "confidential", "private".. Если указанная строка отличается от данных значений, будет использовано значение "private". Установка этого параметра в NULL полностью отключит безопасность KRB4. На данный момент безопасность KRB4 работает только с FTP транзакциями.
CURLOPT_POSTFIELDS Все данные, передаваемые в HTTP POST-запросе. Для передачи файла, укажите перед именем файла @, а также используйте полный путь к файлу. Тип файла также может быть указан с помощью формата ';type=mimetype', следующим за именем файла. Этот параметр может быть передан как в качестве url-закодированной строки, наподобие 'para1=val1&para2=val2&...', так и в виде массива, ключами которого будут имена полей, а значениями - их содержимое. Если value является массивом, заголовок Content-Type будет установлен в значение multipart/form-data. Начиная с версии PHP 5.2.0, при передаче файлов с префиксом @, value должен быть массивом.
CURLOPT_PROXY HTTP-прокси, через который будут направляться запросы.
CURLOPT_PROXYUSERPWD Логин и пароль, записанные в виде "[username]:[password]", используемые при соединении через прокси.
CURLOPT_RANDOM_FILE Имя файла, используемого для инициализации генератора случайных чисел для SSL.
CURLOPT_RANGE Диапазон данных, которые нужно загрузить, в формате "X-Y", причем либо X, либо Y могут быть опущены. Протокол HTTP также поддерживает передачу нескольких диапазонов, разделенных запятыми, они задаются в формате "X-Y,N-M".
CURLOPT_REFERER Содержимое заголовка "Referer: ", который будет использован в HTTP-запросе.
CURLOPT_SSH_HOST_PUBLIC_KEY_MD5 Строка, содержащая 32 шестнадцатеричных цифры. Строка должна являться контрольной суммой по алгоритму MD5 публичного ключа удаленного компьютера и libcurl будет сбрасывать соединение к удаленному хосту до тех пор, пока контрольная сумма не будет соответствовать публичному ключу. Эта опция предназначена только для передачи данных с помощью SCP и SFTP. Добавлено в cURL 7.17.1.
CURLOPT_SSH_PUBLIC_KEYFILE Имя файла для вашего публичного ключа. Если не задано, то libcurl использует по умолчанию файл $HOME/.ssh/id_dsa.pub, если переменная окружения HOME установлена и файл "id_dsa.pub" в текущей директории, если переменная HOME не установлена. Добавлено в cURL 7.16.1.
CURLOPT_SSH_PRIVATE_KEYFILE Имя файла для вашего приватного ключа. Если не задано, то libcurl использует по умолчанию файл $HOME/.ssh/id_dsa, если переменная окружения HOME установлена и файл "id_dsa" в текущей директории, если переменная HOME не установлена. Если файл защищен паролем, установите пароль с помощью CURLOPT_KEYPASSWD. Добавлено в cURL 7.16.1.
CURLOPT_SSL_CIPHER_LIST Список шифров, используемый в SSL-передачах. Например, RC4-SHA и TLSv1 являются корректными списками шифров.
CURLOPT_SSLCERT Имя файла с корректно отформатированным PEM-сертификатом.
CURLOPT_SSLCERTPASSWD Пароль, необходимый для использования сертификата CURLOPT_SSLCERT.
CURLOPT_SSLCERTTYPE Формат сертификата. Поддерживаются форматы "PEM" (по умолчанию), "DER" и "ENG". Добавлен в версии cURL 7.9.3.
CURLOPT_SSLENGINE Идентификатор механизма шифрования для закрытого ключа SSL, указанного в параметре CURLOPT_SSLKEY.
CURLOPT_SSLENGINE_DEFAULT Идентификатор механизма шифрования, используемого для ассиметричных операций шифрования.
CURLOPT_SSLKEY Имя файла с закрытым ключом SSL.
CURLOPT_SSLKEYPASSWD

Тайный пароль, необходимый для использования закрытого ключа SSL, указанного параметром CURLOPT_SSLKEY.

Замечание:

Так как этот параметр содержит ценный пароль, помните, что данный PHP-скрипт нужно хранить в безопасном месте.

CURLOPT_SSLKEYTYPE Тип закрытого ключа SSL, указанного в параметре CURLOPT_SSLKEY. Поддерживаются следующие типы ключей: "PEM" (по умолчанию), "DER" и "ENG".
CURLOPT_URL Загружаемый URL. Данный параметр может быть также установлен при инициализации сенса с помощью curl_init().
CURLOPT_USERAGENT Содержимое заголовка "User-Agent: ", посылаемого в HTTP-запросе.
CURLOPT_USERPWD Логин и пароль, используемые при соединении, указанные в формате "[username]:[password]".

Для следующих значений параметра option, параметр value должен быть массивом:

Параметр Устанавливаемое значение value Замечания
CURLOPT_HTTP200ALIASES Массив HTTP 200 ответов, которые будут трактоваться корректными ответами, а не ошибочными. Добавлен в версии cURL 7.10.3.
CURLOPT_HTTPHEADER Массив устанавливаемых HTTP-заголовков, в формате array('Content-type: text/plain', 'Content-length: 100')
CURLOPT_POSTQUOTE Массив FTP-команд, выполняемых на сервере, после выполнения FTP-запроса.
CURLOPT_QUOTE Массив FTP-команд, выполняемых на сервере, перед выполнением FTP-запроса.

Для следующих значений параметра option, параметр value должен быть потоковым дескриптором (возвращаемым, например, функцией fopen()):

Параметр Устанавливаемое значение value
CURLOPT_FILE Файл, в который будет записан результат передачи. По умолчанию используется поток вывода STDOUT (окно браузера).
CURLOPT_INFILE Файл, из которого должно идти чтение данных, при загрузке на сервер.
CURLOPT_STDERR Альтернативное файл для вывода ошибок, используемый вместо потока ошибок STDERR.
CURLOPT_WRITEHEADER Файл, в который будут записаны заголовки текущей операции.

Для следующих значений параметра option, параметр value должен быть корректной строкой, указывающей имя callback-функции:

Параметр Устанавливаемое значение value
CURLOPT_HEADERFUNCTION Имя callback-функции, принимающей два параметра. Первым параметром является дескриптор cURL, вторым параметром является строка с записываемыми заголовками. Заголовки должны быть записаны с помощью данной callback-функции. Должна возвратить количество записанных байт.
CURLOPT_PASSWDFUNCTION Имя callback-функции, принимающей три параметра. Первым параметром является дескриптор cURL, вторым параметром является строка с запросом пароля, третьим параметром является максимальная длина пароля. Должна возвратить строку, содержащую пароль.
CURLOPT_PROGRESSFUNCTION Имя callback-функции, принимающей три параметра. Первым параметром является дескриптор cURL, вторым параметром является дескриптор файла, а третьим параметром - его длина. Должна возвратить строку с данными.
CURLOPT_READFUNCTION Имя callback-функции, принимающей три параметра. Первым параметром является дескриптор cURL, вторым параметром является ресурс потока, переданный cURL через опцию CURLOPT_INFILE, а третьим параемтром является максимально разрешенное количество данных для чтения. Callback-функция должна возвратить строку, с длиной не превышающей запрошенного количества данных, обычно с помощью чтения из переданного потокового ресурса. Должна возвратить пустую строку для сигнала о конце файла EOF.
CURLOPT_WRITEFUNCTION Имя callback-функции, принимающей два параметра. Первым параметром является дескриптор cURL, а вторым параметром является строка с записываемыми данными. Данные должны быть сохранены с помощью данной функции. Она должна возвратить точное количество записанных байт, иначе закачка будет прервана с ошибкой.

Возвращаемые значения

Возвращает TRUE в случае успешного завершения или FALSE в случае возникновения ошибки.

Список изменений

Версия Описание
5.2.10 Добавлены параметры CURLOPT_PROTOCOLS и CURLOPT_REDIR_PROTOCOLS.
5.1.0 Добавлены параметры CURLOPT_AUTOREFERER, CURLOPT_BINARYTRANSFER, CURLOPT_FTPSSLAUTH, CURLOPT_PROXYAUTH и CURLOPT_TIMECONDITION.
5.0.0 Добавлены параметры CURLOPT_FTP_USE_EPRT, CURLOPT_NOSIGNAL, CURLOPT_UNRESTRICTED_AUTH, CURLOPT_BUFFERSIZE, CURLOPT_HTTPAUTH, CURLOPT_PROXYPORT, CURLOPT_PROXYTYPE, CURLOPT_SSLCERTTYPE и CURLOPT_HTTP200ALIASES.

Примеры

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

<?php
// создание нового ресурса cURL
$ch curl_init();

// установка URL и других необходимых параметров
curl_setopt($chCURLOPT_URL"http://www.example.com/");
curl_setopt($chCURLOPT_HEADERfalse);

// загрузка страницы и выдача её браузеру
curl_exec($ch);

// завершение сеанса и освобождение ресурсов
curl_close($ch);
?>

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

<?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($chCURLOPT_URL'http://localhost/upload.php');
curl_setopt($chCURLOPT_POST1);
curl_setopt($chCURLOPT_POSTFIELDS$data);

curl_exec($ch);
?>

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

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.

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

  • curl_setopt_array() - Устанавливает несколько параметров для сеанса cURL

Коментарии

About the CURLOPT_HTTPHEADER option, it took me some time to figure out how to format the so-called 'Array'. It fact, it is a list of strings. If Curl was already defining a header item, yours will replace it. Here is an example to change the Content Type in a POST:

<?php curl_setopt ($chCURLOPT_HTTPHEADER, Array("Content-Type: text/xml")); ?>

Yann
2002-11-13 06:19:06
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
If you want to Curl to follow redirects and you would also like Curl to echo back any cookies that are set in the process, use this:

<?php curl_setopt($chCURLOPT_COOKIEJAR'-'); ?>

'-' means stdout

-dw
2003-04-02 18:08:56
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
If you specify a CAINFO, note that the file must be in PEM format! (If not, it won't work).
Using Openssl you can use: 
openssl x509 -in <cert> -inform d -outform PEM -out cert.pem 
To create a pem formatted certificate from a binary certificate (the one you get if you download the ca somewhere).
2004-05-07 11:00:06
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
Автор:
Problems can occur if you mix CURLOPT_URL with a 'Host:' header in CURLOPT_HEADERS on redirects because cURL will combine the host you explicitly stated in the 'Host:' header with the host from the Location: header of the redirect response. 

In short, don't do this:

<?php
$host 
"www.example.com";
$url "http://$host/";

$headers = array("Host: $host");

$ch curl_init();

curl_setopt($chCURLOPT_URL$url);
curl_setopt($chCURLOPT_HTTPHEADER$headers);

Do 
this instead:

$host "www.example.com";
$url "http://$host/";

$ch curl_init();

curl_setopt($chCURLOPT_URL$url);
?>
2005-06-01 17:57:05
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
<?php
/*
  Here is a script that is usefull to :
  - login to a POST form,
  - store a session cookie,
  - download a file once logged in.
*/

// INIT CURL
$ch curl_init();

// SET URL FOR THE POST FORM LOGIN
curl_setopt($chCURLOPT_URL'http://www.example.com/Members/Login.php');

// ENABLE HTTP POST
curl_setopt ($chCURLOPT_POST1);

// SET POST PARAMETERS : FORM VALUES FOR EACH FIELD
curl_setopt ($chCURLOPT_POSTFIELDS'fieldname1=fieldvalue1&fieldname2=fieldvalue2');

// IMITATE CLASSIC BROWSER'S BEHAVIOUR : HANDLE COOKIES
curl_setopt ($chCURLOPT_COOKIEJAR'cookie.txt');

# Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL
# not to print out the results of its query.
# Instead, it will return the results as a string return value
# from curl_exec() instead of the usual true/false.
curl_setopt ($chCURLOPT_RETURNTRANSFER1);

// EXECUTE 1st REQUEST (FORM LOGIN)
$store curl_exec ($ch);

// SET FILE TO DOWNLOAD
curl_setopt($chCURLOPT_URL'http://www.example.com/Members/Downloads/AnnualReport.pdf');

// EXECUTE 2nd REQUEST (FILE DOWNLOAD)
$content curl_exec ($ch);

// CLOSE CURL
curl_close ($ch); 

/*
  At this point you can do do whatever you want
  with the downloaded file stored in $content :
  display it, save it as file, and so on.
*/
?>
2005-09-21 10:09:05
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
There is really a problem of transmitting $_POST data with curl in php 4+ at least.
I improved the encoding function by Alejandro Moreno to work properly with mulltidimensional arrays.

<?php
function data_encode($data$keyprefix ""$keypostfix "") {
 
assertis_array($data) );
 
$vars=null;
  foreach(
$data as $key=>$value) {
    if(
is_array($value)) $vars .= data_encode($value$keyprefix.$key.$keypostfix.urlencode("["), urlencode("]"));
    else 
$vars .= $keyprefix.$key.$keypostfix."=".urlencode($value)."&";
  }
  return 
$vars;
}

curl_setopt($chCURLOPT_POSTFIELDSsubstr(data_encode($_POST), 0, -1) );

?>
2005-12-22 04:13:31
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
Автор:
To further expand upon use of CURLOPT_CAPATH and CURLOPT_CAINFO...

In my case I wanted to prevent curl from talking to any HTTPS server except my own using a self signed certificate. To do this, you'll need openssl installed and access to the HTTPS Server Certificate (server.crt by default on apache)

You can then use a command simiar to this to translate your apache certificate into one that curl likes.

$ openssl x509 -in server.crt -out outcert.pem -text

Then set CURLOPT_CAINFO equal to the the full path to outcert.pem and turn on CURLOPT_SSL_VERIFYPEER.

If you want to use the CURLOPT_CAPATH option, you should create a directory for all the valid certificates you have created, then use the c_rehash script that is included with openssl to "prepare" the directory.

If you dont use the c_rehash utility, curl will ignore any file in the directory you set.
2005-12-27 16:24:15
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
If you're getting trouble with cookie handling in curl: 

- curl manages tranparently cookies in a single curl session
- the option 
<?php curl_setopt($chCURLOPT_COOKIEJAR"/tmp/cookieFileName"); ?>

makes curl to store the cookies in a file at the and of the curl session

- the option 
<?php curl_setopt($chCURLOPT_COOKIEFILE"/tmp/cookieFileName"); ?>

makes curl to use the given file as source for the cookies to send to the server. 

so to handle correctly cookies between different curl session, the you have to do something like this:

<?php
       $ch 
curl_init(); 
       
curl_setopt ($chCURLOPT_URL$url); 
       
curl_setopt ($chCURLOPT_COOKIEJARCOOKIE_FILE_PATH);
       
curl_setopt ($chCURLOPT_COOKIEFILECOOKIE_FILE_PATH);

       
curl_setopt ($chCURLOPT_RETURNTRANSFER1); 
       
$result curl_exec ($ch);
       
curl_close($ch);
       return 
$result;
?>

in particular this is NECESSARY if you are using PEAR_SOAP libraries to build a webservice client over https and the remote server need to establish a session cookie. in fact each soap message is sent using a different curl session!!

I hope this can help someone
Luca
2006-02-02 05:55:27
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
Автор:
Sometimes you can't use CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE becoz of the server php-settings(They say u may grab any files from server using these options). Here is the solution
1)Don't use CURLOPT_FOLLOWLOCATION
2)Use curl_setopt($ch, CURLOPT_HEADER, 1)
3)Grab from the header cookies like this:
preg_match_all('|Set-Cookie: (.*);|U', $content, $results);   
$cookies = implode(';', $results[1]);
4)Set them using curl_setopt($ch, CURLOPT_COOKIE,  $cookies);

Good Luck, Yevgen
2006-04-14 10:22:19
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
Clarification on the callback methods:

- CURLOPT_HEADERFUNCTION is for handling header lines received *in the response*,
- CURLOPT_WRITEFUNCTION is for handling data received *from the response*,
- CURLOPT_READFUNCTION is for handling data passed along *in the request*.

The callback "string" can be any callable function, that includes the array(&$obj, 'someMethodName') format.

 -Philippe
2006-05-30 14:31:24
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
Note that if you want to use a proxy and use it as a _cache_, you'll have to do:

<?php curl_setopt($chCURLOPT_HTTPHEADER, array("Pragma: ")); ?>

else by default Curl puts a "Pragma: no-cache" header in and thus force cache misses for all requests.
2006-08-23 13:35:29
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
If you are trying to use CURLOPT_FOLLOWLOCATION and you get this warning:
Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set...

then you will want to read http://www.php.net/ChangeLog-4.php which says "Disabled CURLOPT_FOLLOWLOCATION in curl when open_basedir or safe_mode are enabled." as of PHP 4.4.4/5.1.5.  This is due to the fact that curl is not part of PHP and doesn't know the values of open_basedir or safe_mode, so you could comprimise your webserver operating in safe_mode by redirecting (using header('Location: ...')) to "file://" urls, which curl would have gladly retrieved.

Until the curl extension is changed in PHP or curl (if it ever will) to deal with "Location:" headers, here is a far from perfect remake of the curl_exec function that I am using.

Since there's no curl_getopt function equivalent, you'll have to tweak the function to make it work for your specific use.  As it is here, it returns the body of the response and not the header.  It also doesn't deal with redirection urls with username and passwords in them.

<?php
   
function curl_redir_exec($ch)
    {
        static 
$curl_loops 0;
        static 
$curl_max_loops 20;
        if (
$curl_loops++ >= $curl_max_loops)
        {
           
$curl_loops 0;
            return 
FALSE;
        }
       
curl_setopt($chCURLOPT_HEADERtrue);
       
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
       
$data curl_exec($ch);
        list(
$header$data) = explode("\n\n"$data2);
       
$http_code curl_getinfo($chCURLINFO_HTTP_CODE);
        if (
$http_code == 301 || $http_code == 302)
        {
           
$matches = array();
           
preg_match('/Location:(.*?)\n/'$header$matches);
           
$url = @parse_url(trim(array_pop($matches)));
            if (!
$url)
            {
               
//couldn't process the url to redirect to
               
$curl_loops 0;
                return 
$data;
            }
           
$last_url parse_url(curl_getinfo($chCURLINFO_EFFECTIVE_URL));
            if (!
$url['scheme'])
               
$url['scheme'] = $last_url['scheme'];
            if (!
$url['host'])
               
$url['host'] = $last_url['host'];
            if (!
$url['path'])
               
$url['path'] = $last_url['path'];
           
$new_url $url['scheme'] . '://' $url['host'] . $url['path'] . ($url['query']?'?'.$url['query']:'');
           
curl_setopt($chCURLOPT_URL$new_url);
           
debug('Redirecting to'$new_url);
            return 
curl_redir_exec($ch);
        } else {
           
$curl_loops=0;
            return 
$data;
        }
    }
?>
2006-11-21 22:40:43
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
Options not included in the above, but that work (Taken from the libcurl.a C documentation)

CURLOPT_FTP_SSL 

Pass a long using one of the values from below, to make libcurl use your desired level of SSL for the ftp transfer. (Added in 7.11.0) 

CURLFTPSSL_NONE 

Don't attempt to use SSL. 

CURLFTPSSL_TRY 

Try using SSL, proceed as normal otherwise. 

CURLFTPSSL_CONTROL 

Require SSL for the control connection or fail with CURLE_FTP_SSL_FAILED. 

CURLFTPSSL_ALL 

Require SSL for all communication or fail with CURLE_FTP_SSL_FAILED.
2007-04-24 12:01:33
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
In case you wonder how come, that cookies don't work under Windows, I've googled for some answers, and here is the result: Under WIN you need to input absolute path of the cookie file.

This piece of code solves it:

<?php

if ($cookies != '')
    {
    if (
substr(PHP_OS03) == 'WIN')
        {
$cookies str_replace('\\','/'getcwd().'/'.$cookies);}
   
curl_setopt($chCURLOPT_COOKIEJAR$cookies);
   
curl_setopt($chCURLOPT_COOKIEFILE$cookies);
    }

?>
2007-06-03 19:51:58
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
Автор:
<?php
/*
* Author: Ojas Ojasvi
* Released: September 25, 2007
* Description: An example of the disguise_curl() function in order to grab contents from a website while remaining fully camouflaged by using a fake user agent and fake headers.
*/

$url 'http://www.php.net';

// disguises the curl using fake headers and a fake user agent.
function disguise_curl($url)
{
 
$curl curl_init();

 
// Setup headers - I used the same headers from Firefox version 2.0.0.6
  // below was split up because php.net said the line was too long. :/
 
$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
 
$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
 
$header[] = "Cache-Control: max-age=0";
 
$header[] = "Connection: keep-alive";
 
$header[] = "Keep-Alive: 300";
 
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
 
$header[] = "Accept-Language: en-us,en;q=0.5";
 
$header[] = "Pragma: "// browsers keep this blank.

 
curl_setopt($curlCURLOPT_URL$url);
 
curl_setopt($curlCURLOPT_USERAGENT'Googlebot/2.1 (+http://www.google.com/bot.html)');
 
curl_setopt($curlCURLOPT_HTTPHEADER$header);
 
curl_setopt($curlCURLOPT_REFERER'http://www.google.com');
 
curl_setopt($curlCURLOPT_ENCODING'gzip,deflate');
 
curl_setopt($curlCURLOPT_AUTOREFERERtrue);
 
curl_setopt($curlCURLOPT_RETURNTRANSFER1);
 
curl_setopt($curlCURLOPT_TIMEOUT10);

 
$html curl_exec($curl); // execute the curl command
 
curl_close($curl); // close the connection

 
return $html// and finally, return $html
}

// uses the function and displays the text off the website
$text disguise_curl($url);
echo 
$text;
?>

Ojas Ojasvi
2007-09-25 13:52:57
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
After much struggling, I managed to get a SOAP request requiring HTTP authentication to work.  Here's some source that will hopefully be useful to others. 

         <?php

         $credentials 
"username:password";
         
         
// Read the XML to send to the Web Service
         
$request_file "./SampleRequest.xml";
       
$fh fopen($request_file'r');
       
$xml_data fread($fhfilesize($request_file));
       
fclose($fh);
               
       
$url "http://www.example.com/services/calculation";
       
$page "/services/calculation";
       
$headers = array(
           
"POST ".$page." HTTP/1.0",
           
"Content-type: text/xml;charset=\"utf-8\"",
           
"Accept: text/xml",
           
"Cache-Control: no-cache",
           
"Pragma: no-cache",
           
"SOAPAction: \"run\"",
           
"Content-length: ".strlen($xml_data),
           
"Authorization: Basic " base64_encode($credentials)
        );
       
       
$ch curl_init();
       
curl_setopt($chCURLOPT_URL,$url);
       
curl_setopt($chCURLOPT_RETURNTRANSFER1);
       
curl_setopt($chCURLOPT_TIMEOUT60);
       
curl_setopt($chCURLOPT_HTTPHEADER$headers);
       
curl_setopt($chCURLOPT_USERAGENT$defined_vars['HTTP_USER_AGENT']);
       
       
// Apply the XML to our curl call
       
curl_setopt($chCURLOPT_POST1);
       
curl_setopt($chCURLOPT_POSTFIELDS$xml_data); 

       
$data curl_exec($ch); 

        if (
curl_errno($ch)) {
            print 
"Error: " curl_error($ch);
        } else {
           
// Show me the result
           
var_dump($data);
           
curl_close($ch);
        }

?>
2008-01-07 11:48:02
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
if you would like to send xml request to a server (lets say, making a soap proxy),
you have to set

<?php
curl_setopt
($chCURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
?>

makesure you watch for cache issue:
the below code will prevent cache...

<?php
curl_setopt
($chCURLOPT_FORBID_REUSE1);
curl_setopt($chCURLOPT_FRESH_CONNECT1);
?>

hope it helps ;)
2008-03-04 15:16:10
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
If you are doing a POST, and the content length is 1,025 or greater, then curl exploits a feature of http 1.1: 100 (Continue) Status.

See http://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html#sec8.2.3

* it adds a header, "Expect: 100-continue". 
* it then sends the request head, waits for a 100 response code, then sends the content 

Not all web servers support this though.  Various errors are returned depending on the server.  If this happens to you, suppress the "Expect" header with this command:

<?php
curl_setopt
($chCURLOPT_HTTPHEADER, array('Expect:'));
?>

See http://www.gnegg.ch/2007/02/the-return-of-except-100-continue/
2008-04-09 20:55:35
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
Автор:
In PHP5, for the "CURLOPT_POSTFIELDS" option, we can use:

<?php
$ch 
curl_init($URI);
$Post http_build_query($PostData);
curl_setopt($chCURLOPT_POSTFIELDS$Post);
$Output curl_exec($ch);
curl_close($ch);
?>
2008-06-19 12:02:32
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
If you want to connect to a server which requires that you identify yourself with a certificate, use following code. Your certificate and servers certificate are signed by an authority whose certificate is in ca.ctr.

<?php
curl_setopt
($chCURLOPT_VERBOSE'1');
curl_setopt($chCURLOPT_SSL_VERIFYHOST'2');
curl_setopt($chCURLOPT_SSL_VERIFYPEER'1');
curl_setopt($chCURLOPT_CAINFOgetcwd().'/cert/ca.crt');
curl_setopt($chCURLOPT_SSLCERTgetcwd().'/cert/mycert.pem');
curl_setopt($chCURLOPT_SSLCERTPASSWD'password');
?>

If your original certificate is in .pfx format, you have to convert it to .pem using following commands
# openssl pkcs12 -in mycert.pfx -out mycert.key
# openssl rsa -in mycert.key -out mycert.pem
# openssl x509 -in mycert.key >> mycert.pem
2008-07-10 13:08:48
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
To find what encoding a given HTTP POST request uses is easy -- passing an array to CURLOPT_POSTFIELDS results in  multipart/form-data:

<?php
curl_setopt
(CURLOPT_POSTFIELDS, array('field1' => 'value'));
?>

Passing a URL-encoded string will result in application/x-www-form-urlencoded:

<?php
curl_setopt
(CURLOPT_POSTFIELDS, array('field1=value&field2=value2'));
?>

I ran across this when integrating with both a warehouse system and an email system; neither would accept multipart/form-data, but both happily accepted application/x-www-form-urlencoded.
2008-08-05 19:28:29
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
Passing in PHP's $_SESSION into your cURL call:

<?php
session_start
();
$strCookie 'PHPSESSID=' $_COOKIE['PHPSESSID'] . '; path=/';
session_write_close();

$curl_handle curl_init('enter_external_url_here');
curl_setopt$curl_handleCURLOPT_COOKIE$strCookie );
curl_exec($curl_handle);
curl_close($curl_handle);
?>

This worked great for me.  I was calling pages from the same server and needed to keep the $_SESSION variables.  This passes them over.  If you want to test, just print_r($_SESSION);

Enjoy!
2008-11-19 15:31:11
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
Note that CURLOPT_RETURNTRANSFER when used with CURLOPT_WRITEFUNCTION has effectively three settings: default, true, and false.

default - callbacks will be called as expected.
true - content will be returned but callback function will not be called.
false - content will be output and callback function will not be called.

Note that CURLOPT_HEADERFUNCTION callbacks are always called.
2008-12-01 01:28:58
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
To send a post as a different content-type (ie.. application/json or text/xml) add this setopt call

<?php
curl_setopt
($chCURLOPT_HTTPHEADER,array('Content-Type: application/json'));
?>
2009-01-28 08:49:15
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
Автор:
I noticed that if you want to get current cookie file after curl_exec() - you need to close current curl handle (like it said in manual), but if you want cookies to be dumped to file after any curl_exec (without curl_close) you can:

<?php
#call it normally
$ch curl_init(); 
curl_setopt($chCURLOPT_HEADER0); 
curl_setopt($chCURLOPT_COOKIEFILE"cookiefile"); 
curl_setopt($chCURLOPT_COOKIEJAR"cookiefile"); 
curl_setopt($chCURLOPT_FOLLOWLOCATION1); 
curl_setopt($chCURLOPT_URL'http://www.example.com/'); 
$result1 curl_exec($ch); 

#and then  make a temp copy
$ch_temp=curl_copy_handle(ch);
curl_close($ch);
$ch=$ch_temp;
?>

Only this way, if you close $ch_temp - cookies wont be dumped.
2009-06-14 07:56:26
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
Автор:
Seems like some options not mentioned on this page, but listed on http://curl.haxx.se/libcurl/c/curl_easy_setopt.html is actually supported.

I was happy to see that I could actually use CURLOPT_FTP_CREATE_MISSING_DIRS even from PHP.
2009-07-03 07:12:02
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
Автор:
When you are using CURLOPT_FILE to download directly into a file you must close the file handler after the curl_close() otherwise the file will be incomplete and you will not be able to use it until the end of the execution of the php process.

<?php

$fh 
fopen('/tmp/foo''w');
$ch curl_init('http://example.com/foo');
curl_setopt($chCURLOPT_FILE$fh);
curl_exec($ch);
curl_close($ch);

# at this point your file is not complete and corrupted

fclose($fh);

# now you can use your file;

read_file('/tmp/foo');

?>
2009-07-28 06:05:03
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
Автор:
Whats not mentioned in the documentation is that you have to set CURLOPT_COOKIEJAR to a file for the CURL handle to actually use cookies, if it is not set then cookies will not be parsed.
2009-08-24 00:07:21
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
Автор:
There is a function to send POST data in page with five parameters :

$post must be an array
$page is the page where POST datas will be send.
$n must be true to continue if they are php redirection (Location: )
$session must be define true if you want to use cookies 
$referer must be a link to get a wrong referer or only to have a referer.

<?php
function curl_data_post($post$page$n$session$referer)
    {
        if(!
is_array($post))
        {
         return 
false;
        }
       
       
$DATA_POST curl_init();
       
curl_setopt($DATA_POSTCURLOPT_RETURNTRANSFERtrue);
       
curl_setopt($DATA_POSTCURLOPT_URL$page);
       
curl_setopt($DATA_POSTCURLOPT_POSTtrue);
        if(
$n)
        {
         
curl_setopt($DATA_POSTCURLOPT_FOLLOWLOCATIONtrue);
        }
        if(
$session)
        {
         
curl_setopt($DATA_POSTCURLOPT_COOKIEFILE'cookiefile.txt');
         
curl_setopt($DATA_POSTCURLOPT_COOKIEJAR'cookiefile.txt'); 
        }
       
        if(
$referer)
        {
         
curl_setopt($DATA_POSTCURLOPT_REFERER$referer); 
        }
       
       
curl_setopt($DATA_POSTCURLOPT_POSTFIELDS$post);
       
$data curl_exec($DATA_POST);
        if(
$data == false)
        {
         echo
'Warning : ' curl_error($DATA_POST);
         
curl_close($DATA_POST);
         return 
false;
        }
        else
        {
         
curl_close($DATA_POST);
         return 
$data;
        }
    }
?>
2009-09-04 17:42:41
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
I've found that setting CURLOPT_HTTPHEADER more than once will clear out any headers you've set previously with CURLOPT_HTTPHEADER.

Consider the following:
<?php
   
# ...

   
curl_setopt($cURL,CURLOPT_HTTPHEADER,array (
       
"Content-Type: text/xml; charset=utf-8",
       
"Expect: 100-continue"
   
));

   
# ... do some other stuff ...

   
curl_setopt($cURL,CURLOPT_HTTPHEADER,array (
       
"Accept: application/json"
   
));

   
# ...
?>

Both the Content-Type and Expect I set will not be in the outgoing headers, but Accept will.
2009-09-22 18:54:17
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
Автор:
Hello. 
During problems with "CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set" 
I was looking for solution. 
I've found few methods on this page, but none of them was good enough, so I made one.
<?php
function curl_redirect_exec($ch, &$redirects$curlopt_header false) {
   
curl_setopt($chCURLOPT_HEADERtrue);
   
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
   
$data curl_exec($ch);
   
$http_code curl_getinfo($chCURLINFO_HTTP_CODE);
    if (
$http_code == 301 || $http_code == 302) {
        list(
$header) = explode("\r\n\r\n"$data2);
       
$matches = array();
       
preg_match('/(Location:|URI:)(.*?)\n/'$header$matches);
       
$url trim(array_pop($matches));
       
$url_parsed parse_url($url);
        if (isset(
$url_parsed)) {
           
curl_setopt($chCURLOPT_URL$url);
           
$redirects++;
            return 
curl_redirect_exec($ch$redirects);
        }
    }
    if (
$curlopt_header)
        return 
$data;
    else {
        list(,
$body) = explode("\r\n\r\n"$data2);
        return 
$body;
    }
}
?>

Main issue in existing functions was lack of information, how many redirects was done. 
This one will count it. 
First parameter as usual. 
Second should be already initialized integer, it will be incremented by number of done redirects. 
You can set CURLOPT_HEADER if You need it.
2009-12-09 14:07:51
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
Автор:
PUT requests are very simple, just make sure to specify a content-length header and set post fields as a string.

Example:

<?php
function doPut($url$fields)
{
   
$fields = (is_array($fields)) ? http_build_query($fields) : $fields;

   if(
$ch curl_init($url))
   {
     
curl_setopt($chCURLOPT_CUSTOMREQUEST'PUT');
     
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
     
curl_setopt($chCURLOPT_HTTPHEADER, array('Content-Length: ' strlen($fields)));
     
curl_setopt($chCURLOPT_POSTFIELDS$fields);
     
curl_exec($ch);

     
$status curl_getinfo($chCURLINFO_HTTP_CODE);

     
curl_close($ch);

      return (int) 
$status;
   }
   else
   {
      return 
false;
   }
}

if(
doPut('http://example.com/api/a/b/c', array('foo' => 'bar')) == 200)
   
// do something
else
   
// do something else.
?>

You can grab the request data on the other side with:

<?php
if($_SERVER['REQUEST_METHOD'] == 'PUT')
{
   
parse_str(file_get_contents('php://input'), $requestData);

   
// Array ( [foo] => bar )
   
print_r($requestData);

   
// Do something with data...
}
?>

DELETE  can be done in exactly the same way.
2010-02-05 12:47:22
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
If you wish to find the size of the file you are streaming and use it as your header this is how:

<?php

function write_function($curl_resource$string)
{
    if(
curl_getinfo($curl_resourceCURLINFO_SIZE_DOWNLOAD) <= 2000)
    {
       
header('Expires: 0');
       
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
       
header('Pragma: public');
       
header('Content-Description: File Transfer');
       
header("Content-Transfer-Encoding: binary");
       
header("Content-Type: ".curl_getinfo($curl_resourceCURLINFO_CONTENT_TYPE)."");
       
header("Content-Length: ".curl_getinfo($curl_resourceCURLINFO_CONTENT_LENGTH_DOWNLOAD)."");
    }
   
    print 
$string;

    return 
mb_strlen($string'8bit');
}

?>

1440 is the the default number of bytes curl will call the write function (BUFFERSIZE does not affect this, i actually think you can not change this value), so it means the headers are going to be set only one time.

write_function must return the exact number of bytes of the string, so you can return a value with mb_strlen.
2010-03-22 04:48:12
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
Автор:
If you need to send deta in a DELETE request, use:

<?php
$request_body 
'some data';
$ch curl_init('http://www.example.com');
       
curl_setopt($chCURLOPT_POSTFIELDS$request_body);
       
curl_setopt($chCURLOPT_FOLLOWLOCATION1);
       
curl_setopt($chCURLOPT_HEADER0); 
       
curl_setopt($chCURLOPT_RETURNTRANSFER1);
       
curl_setopt($chCURLOPT_CUSTOMREQUEST"DELETE");
       
$response curl_exec($ch);
var_dump($response);
?>
2010-04-08 06:56:37
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
If you have turned on conditional gets on a curl handle, and then for a subsequent request, you don't have a good setting for CURLOPT_TIMEVALUE , you can disable If-Modified-Since checking with:

<?php

$ch 
curl_init();
curl_setopt($chCURLOPT_URL$foo);
curl_setopt($chCURLOPT_TIMEVALUEfilemtime($foo_path));
curl_setopt($chCURLOPT_TIMECONDITIONCURLOPT_TIMECOND_IFMODIFIEDSINCE);
curl_exec($ch);
// Reuse same curl handle
curl_setopt($chCURLOPT_URL$bar);
curl_setopt($chCURLOPT_TIMEVALUEnull); // don't know mtime
curl_setopt($chCURLOPT_TIMECONDITION0); // set it to 0, turns it off
curl_exec($ch);

?>
2010-04-16 17:56:04
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
Some additional notes for curlopt_writefunction. I struggled with this at first because it really isn't documented very well.

When you write a callback function and use it with curlopt_writefunction it will be called MULTIPLE times. Your function MUST return the ammount of data written to it each time. It is very picky about this. Here is a snippet from my code that may help you

<?php
curl_setopt
($this->curl_handleCURLOPT_WRITEFUNCTION, array($this"receiveResponse"));

// later on in the class I wrote my receive Response method

private function receiveResponse($curlHandle,$xmldata)
                {
                       
$this->responseString $xmldata;
                       
$this->responseXML .=  $this->responseString;
                       
$this->length strlen($xmldata);
                       
$this->size += $this->length;
                        return 
$this->length;

                }
?>

Now I did this for a class. If you aren't doing OOP then you will obviously need to modify this for your own use.

CURL calls your script MULTIPLE times because the data will not always be sent all at once. Were talking internet here so its broken up into packets. You need to take your data and concatenate it all together until it is all written. I was about to pull my damn hair out because I would get broken chunks of XML back from the server and at random lengths. I finally figured out what was going on. Hope this helps
2010-06-18 10:29:21
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
It appears that setting CURLOPT_FILE before setting CURLOPT_RETURNTRANSFER doesn't work, presumably because CURLOPT_FILE depends on CURLOPT_RETURNTRANSFER being set.

So do this:

<?php
curl_setopt
($chCURLOPT_RETURNTRANSFER1);
curl_setopt($chCURLOPT_FILE$fp);
?>

not this:

<?php
curl_setopt
($chCURLOPT_FILE$fp);
curl_setopt($chCURLOPT_RETURNTRANSFER1);
?>
2010-07-26 15:57:50
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
In order to reset CURLOPT_HTTPHEADER, set it to array(). The cURL C API says you should set it to NULL, but that doesn’t work in the PHP wrapper.
2010-11-29 06:01:05
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
FYI... unless you specifically set the user agent, no user agent will be sent in your request as there is no default value like some of the other options.

As others have said, not sending a user agent may cause you to not  get the results that you expected, e.g., 0 byte length content, different content, etc.
2011-01-26 15:42:03
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
Автор:
Handling redirections with curl if safe_mode or open_basedir is enabled. The function working transparent, no problem with header and returntransfer options. You can handle the max redirection with the optional second argument (the function is set the variable to zero if max redirection exceeded).
Second parameter values:
- maxredirect is null or not set: redirect maximum five time, after raise PHP warning
- maxredirect is greather then zero: no raiser error, but parameter variable set to zero
- maxredirect is less or equal zero: no follow redirections

<?php
function curl_exec_follow(/*resource*/ $ch/*int*/ &$maxredirect null) {
   
$mr $maxredirect === null intval($maxredirect);
    if (
ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) {
       
curl_setopt($chCURLOPT_FOLLOWLOCATION$mr 0);
       
curl_setopt($chCURLOPT_MAXREDIRS$mr);
    } else {
       
curl_setopt($chCURLOPT_FOLLOWLOCATIONfalse);
        if (
$mr 0) {
           
$newurl curl_getinfo($chCURLINFO_EFFECTIVE_URL);

           
$rch curl_copy_handle($ch);
           
curl_setopt($rchCURLOPT_HEADERtrue);
           
curl_setopt($rchCURLOPT_NOBODYtrue);
           
curl_setopt($rchCURLOPT_FORBID_REUSEfalse);
           
curl_setopt($rchCURLOPT_RETURNTRANSFERtrue);
            do {
               
curl_setopt($rchCURLOPT_URL$newurl);
               
$header curl_exec($rch);
                if (
curl_errno($rch)) {
                   
$code 0;
                } else {
                   
$code curl_getinfo($rchCURLINFO_HTTP_CODE);
                    if (
$code == 301 || $code == 302) {
                       
preg_match('/Location:(.*?)\n/'$header$matches);
                       
$newurl trim(array_pop($matches));
                    } else {
                       
$code 0;
                    }
                }
            } while (
$code && --$mr);
           
curl_close($rch);
            if (!
$mr) {
                if (
$maxredirect === null) {
                   
trigger_error('Too many redirects. When following redirects, libcurl hit the maximum amount.'E_USER_WARNING);
                } else {
                   
$maxredirect 0;
                }
                return 
false;
            }
           
curl_setopt($chCURLOPT_URL$newurl);
        }
    }
    return 
curl_exec($ch);
}
?>
2011-01-27 19:12:02
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
Автор:
When using CURLOPT_POSTFIELDS with an array as parameter, you have to pay high attention to user input. Unvalidated user input will lead to serious security issues.

<?php

/**
 * test.php:
 */
$ch curl_init('http://example.com');

curl_setopt($chCURLOPT_POST1);
curl_setopt($chCURLOPT_POSTFIELDS, array(
   
'foo' => $_GET['bar']
));

curl_exec($ch);

?>

Requesting "test.php?bar=@/home/user/test.png" will send "test.png" to example.com. 
Make sure you remove the leading "@" from user input.
2011-02-18 13:04:06
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
When CURLOPT_FOLLOWLOCATION and CURLOPT_HEADER are both true and redirect/s have happened then the header returned by curl_exec() will contain all the headers in the redirect chain in the order they were encountered.
2011-04-02 05:31:41
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
Автор:
If you want cURL to timeout in less than one second, you can use CURLOPT_TIMEOUT_MS, although there is a bug/"feature"  on "Unix-like systems" that causes libcurl to timeout immediately if the value is < 1000 ms with the error "cURL Error (28): Timeout was reached".  The explanation for this behavior is:

"If libcurl is built to use the standard system name resolver, that portion of the transfer will still use full-second resolution for timeouts with a minimum timeout allowed of one second."

What this means to PHP developers is "You can use this function without testing it first, because you can't tell if libcurl is using the standard system name resolver (but you can be pretty sure it is)"

The problem is that on (Li|U)nix, when libcurl uses the standard name resolver, a SIGALRM is raised during name resolution which libcurl thinks is the timeout alarm.

The solution is to disable signals using CURLOPT_NOSIGNAL.  Here's an example script that requests itself causing a 10-second delay so you can test timeouts:

<?php
if (!isset($_GET['foo'])) {
       
// Client
       
$ch curl_init('http://localhost/test/test_timeout.php?foo=bar');
       
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
       
curl_setopt($chCURLOPT_NOSIGNAL1);
       
curl_setopt($chCURLOPT_TIMEOUT_MS200);
       
$data curl_exec($ch);
       
$curl_errno curl_errno($ch);
       
$curl_error curl_error($ch);
       
curl_close($ch);

        if (
$curl_errno 0) {
                echo 
"cURL Error ($curl_errno): $curl_error\n";
        } else {
                echo 
"Data received: $data\n";
        }
} else {
       
// Server
       
sleep(10);
        echo 
"Done.";
}
?>
2011-06-24 14:47:47
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
Автор:
CURLOPT_POST must be left unset if you want the Content-Type header set to "multipart/form-data" (e.g., when CURLOPT_POSTFIELDS is an array). If you set CURLOPT_POSTFIELDS to an array and have CURLOPT_POST set to TRUE, Content-Length will be -1 and most sane servers will reject the request. If you set CURLOPT_POSTFIELDS to an array and have CURLOPT_POST set to FALSE, cURL will send a GET request.
2012-01-10 18:36:48
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
The description of the use of the CURLOPT_POSTFIELDS option should be emphasize, that using POST with HTTP/1.1 with cURL implies the use of a "Expect: 100-continue" header. Some web servers will not understand the handling of chunked transfer of post data.

To disable this behavior one must disable the use of the "Expect:" header with

    curl_setopt($ch, CURLOPT_HTTPHEADER,array("Expect:"));
2012-03-31 21:45:22
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
Автор:
This may be not obvious, but if you specify the CURLOPT_POSTFIELDS and don't specify the CURLOPT_POST - it will still send POST, not GET (as you might think - since GET is default).
So the line:

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

is synonym to:

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

Even if you set the options like this (in this order):

curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

it will send POST, since CURLOPT_POSTFIELDS is latter.
So if you want GET - make sure you don't have CURLOPT_POSTFIELDS specified somewhere.
2012-04-03 18:55:05
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
Автор:
Note that if you put a certificate chain in a PEM file, the certificates need to be ordered so that each certificate is followed by its issuer (i.e., root last.)

Source: http://publib.boulder.ibm.com/tividd/td/ITIM/SC32-1493-00/en_US/HTML/im451_config09.htm
2012-04-18 01:52:40
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
if you want to do a GET request with additional body data it will become tricky not to implicitly change the request to a POST, like many notes below correctly state.
So to do the analogy of command line's

curl -XGET 'http://example.org?foo=bar' -d '<baz>some additional data</baz>'

in PHP you'll do, besides your other necessary stuff,

<?php
    curl_setopt
($curlHandleCURLOPT_CUSTOMREQUEST'GET');
   
curl_setopt($curlHandleCURLOPT_POSTFIELDS'<baz>some additional data</baz>');
?>

during my experiments, every other "similar" way, like e.g. CURLOPT_HTTPGET, didn't send the additional data or fell into POST.
2012-07-06 10:37:56
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
If you are trying to update something on your server and you need to handle this update operation by PUT;

<?php
curl_setopt
($chCURLOPT_CUSTOMREQUEST"PUT");
curl_setopt($chCURLOPT_PUT1);
?>

are "useless" without;

<?php
curl_setopt
($chCURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: PUT'));
?>

Example;

Updating a book data in database identified by "id 1";

--cURL Part--
<?php
$data 
http_build_query($_POST);
// or
$data http_build_query(array(
   
'name'  => 'PHP in Action',
   
'price' => 10.9
));

$ch curl_init();
curl_setopt($chCURLOPT_URL"http://api.localhost/rest/books/1");
curl_setopt($chCURLOPT_RETURNTRANSFER1);
// curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); // no need anymore
// or
// curl_setopt($ch, CURLOPT_PUT, 1); // no need anymore
curl_setopt($chCURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: PUT'));
curl_setopt($chCURLOPT_POSTFIELDS$data);
$ce curl_exec($ch);
curl_close($ch);
print_r($ce);
?>

--API class--
<?php
public function putAction() {
    echo 
"putAction() -> id: "$this->_getParam('id') ."\n";
   
print_r($_POST);
   
// do stuff with post data
   
...
?>

--Output--
putAction() -> id: 15
Array
(
    [name] => PHP in Action
    [price] => 10.9
)

---Keywords--
rest, restfull api, restfull put, curl put, curl customrequest put
2012-08-03 11:57:54
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
I've created an example that gets the file on url passed to script and outputs it to the browser. 

<?php
//get the file (e.g. image) and output it to the browser
$ch curl_init(); //open curl handle
curl_setopt($chCURLOPT_URL$_GET['url']); //set an url
curl_setopt($chCURLOPT_RETURNTRANSFER1); //do not output directly, use variable
curl_setopt($chCURLOPT_BINARYTRANSFER1); //do a binary transfer
curl_setopt($chCURLOPT_FAILONERROR1); //stop if an error occurred
$file=curl_exec($ch); //store the content in variable
if(!curl_errno($ch))
{
   
//send out headers and output
   
header ("Content-type: ".curl_getinfo($chCURLINFO_CONTENT_TYPE)."");
   
header ("Content-Length: ".curl_getinfo($chCURLINFO_CONTENT_LENGTH_DOWNLOAD)."");
    echo 
$file;
} else echo 
'Curl error: ' curl_error($ch);
curl_close($ch); //close curl handle
?>

p.s. Make sure that there're no new lines before and after code or script may not work.
2012-09-25 11:47:34
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
I spent a couple of days trying to POST a multi-dimensional array of form fields, including a file upload, to a remote server to update a product. Here are the breakthroughs that FINALLY allowed the script to run as desired.

Firstly, the HTML form used input names like these:
<input type="text" name="product[name]" />
<input type="text" name="product[cost]" />
<input type="file" name="product[thumbnail]" />
in conjunction with two other form inputs not part of the product array
<input type="text" name="method" value="put" />
<input type="text" name="mode" />

I used several cURL options, but the only two (other than URL) that mattered were:
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $postfields);
Pretty standard so far.
Note: headers didn't need to be set, cURL automatically sets headers (like content-type: multipart/form-data; content-length...) when you pass an array into CURLOPT_POSTFIELDS.
Note: even though this is supposed to be a PUT command through an HTTP POST form, no special PUT options needed to be passed natively through cURL. Options such as
curl_setopt($handle, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: PUT', 'Content-Length: ' . strlen($fields)));
or
curl_setopt($handle, CURLOPT_PUT, true);
or
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "PUT);
were not needed to make the code work.

The fields I wanted to pass through cURL were arranged into an array something like this:
$postfields = array("method" => $_POST["method"],
                    "mode" => $_POST["mode"],
                    "product" => array("name" => $_POST["product"], 
                                        "cost" => $_POST["product"]["cost"], 
                                        "thumbnail" => "@{$_FILES["thumbnail"]["tmp_name"]};type={$_FILES["thumbnail"]["type"]}")
                    );

-Notice how the @ precedes the temporary filename, this creates a link so PHP will upload/transfer an actual file instead of just the file name, which would happen if the @ isn't included.
-Notice how I forcefully set the mime-type of the file to upload. I was having issues where images filetypes were defaulting to octet-stream instead of image/png or image/jpeg or whatever the type of the selected image.

I then tried passing $postfields straight into curl_setopt($this->handle, CURLOPT_POSTFIELDS, $postfields); but it didn't work.
I tried using http_build_query($postfields); but that didn't work properly either.
In both cases either the file wouldn't be treated as an actual file and the form data wasn't being sent properly. The problem was HTTP's methods of transmitting arrays. While PHP and other languages can figure out how to handle arrays passed via forms, HTTP isn't quite as sofisticated. I had to rewrite the $postfields array like so:
$postfields = array("method" => $_POST["method"],
                    "mode" => $_POST["mode"],
                    "product[name]" => $_POST["product"], 
                    "product[cost]" => $_POST["product"]["cost"], 
                    "product[thumbnail]" => "@{$_FILES["thumbnail"]["tmp_name"]}");
curl_setopt($handle, CURLOPT_POSTFIELDS, $postfields);

This, without the use of http_build_query, solved all of my problems. Now the receiving host outputs both $_POST and $_FILES vars correctly.
2012-10-23 01:22:21
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
Please everyone, stop setting CURLOPT_SSL_VERIFYPEER to false or 0. If your PHP installation doesn't have an up-to-date CA root certificate bundle, download the one at the curl website and save it on your server:

http://curl.haxx.se/docs/caextract.html

Then set a path to it in your php.ini file, e.g. on Windows:

curl.cainfo=c:\php\cacert.pem

Turning off CURLOPT_SSL_VERIFYPEER allows man in the middle (MITM) attacks, which you don't want!
2012-10-24 03:21:23
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
Автор:
If you have a mixture of strings starting with @ (at character) and files in CURLOPT_POSTFIELDS you have a problem (such as posting a tweet with attached media) because curl tries to interpret anything starting with @ as a file.

<?php

$postfields 
= array(
   
'upload_file' => '@file_to_upload.png',
   
'upload_text' => '@text_to_upload'
);

$curl curl_init();
curl_setopt($curlCURLOPT_URL'http://example.com/upload-test');
curl_setopt($curlCURLOPT_POSTFIELDS$postfields);
curl_exec($curl);
curl_close($curl);

?>

To get around this, prepend the text string with the NULL character like so:

<?php
    $postfields 
= array(
       
'upload_file' => '@file_to_upload.png',
       
'upload_text' => sprintf("\0%s"'@text_to_upload')
    );
?>

Original source: http://bit.ly/AntMle
2013-05-06 18:14:51
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
CURLOPT_POST should be set before CURLOPT_POSTFIELDS. Otherwise you might encounter 411 Length required error.

Following code generates "411 Length Required" on nginx/1.1.15
<?php
curl_setopt 
($chCURLOPT_POSTFIELDS$postfields); 
curl_setopt ($chCURLOPT_POST1);
?>

But this one works.

<?php
curl_setopt 
($chCURLOPT_POST1);
curl_setopt ($chCURLOPT_POSTFIELDS$postfields); 
?>
2013-06-01 21:23:51
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
Автор:
Many hosters use PHP safe_mode or/and open_basedir, so you can't use CURLOPT_FOLLOWLOCATION. If you try, you see message like this:
CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode is enabled or an open_basedir is set in [you script name & path] on line XXX

First, I try to use zsalab function (http://us2.php.net/manual/en/function.curl-setopt.php#102121) from this page, but for some reason it did not work properly. So, I wrote my own.

It can be use instead of curl_exec. If server HTTP response codes is 30x, function will forward the request as long as the response is not different from 30x (for example, 200 Ok). Also you can use POST.

function curlExec(/* Array */$curlOptions='', /* Array */$curlHeaders='', /* Array */$postFields='')
{
  $newUrl = '';
  $maxRedirection = 10;
  do
  {
    if ($maxRedirection<1) die('Error: reached the limit of redirections');

    $ch = curl_init();
    if (!empty($curlOptions)) curl_setopt_array($ch, $curlOptions);
    if (!empty($curlHeaders)) curl_setopt($ch, CURLOPT_HTTPHEADER, $curlHeaders);
    if (!empty($postFields))
    {
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
    }
   
    if (!empty($newUrl)) curl_setopt($ch, CURLOPT_URL, $newUrl); // redirect needed
   
    $curlResult = curl_exec($ch);
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    if ($code == 301 || $code == 302 || $code == 303 || $code == 307)
    {
      preg_match('/Location:(.*?)\n/', $curlResult, $matches);
      $newUrl = trim(array_pop($matches));
      curl_close($ch);

      $maxRedirection--;
      continue;
    }
    else // no more redirection
    {
      $code = 0;
      curl_close($ch);
    }
  }
  while($code);
  return $curlResult;
}
2013-11-15 12:36:49
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
Автор:
If you only want to enable cookie handling and you don't need to save the cookies for a separate session, just set CURLOPT_COOKIEFILE to an empty string.  I was given the advice to use php://memory but that did not seem to have the same effect.

Although this is stated in the documentation I thought it was worth reiterating since it cause me so much trouble.
2014-02-24 19:36:21
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
Автор:
If you use cURL to fetch user-supplied URLs (for instance, in a web-based RSS aggregator), be aware of the risk of server-side request forgery (SSRF). This is an attack where the user takes advantage of the fact that cURL requests are sent from the web server itself, to reach network locations they wouldn't be able to reach from outside the network.

For instance, they could enter a "http://localhost" URL, and access things on the web server via "localhost". Or, "ftp://localhost". cURL supports a lot of protocols!

If you are using CURLOPT_FOLLOWLOCATION, the malicious URL could be in a redirect from the original request. cURL also will follow redirect headers to other protocols! (303 See Other; Location: ftp://localhost).

So if you're using cURL with user-supplied URLs, at the very least use CURLOPT_PROTOCOLS (which also sets CURLOPT_REDIR_PROTOCOLS), and either disable CURLOPT_FOLLOWLOCATION or use the "SafeCurl" library to safely follow redirects.
2014-11-25 01:34:36
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
Please note that if you want to handle progress using CURLOPT_PROGRESSFUNCTION option, you need to take into consideration what version of PHP are you using. Since version 5.5.0, compatibility-breaking change was introduced in number/order of the arguments passed to the callback function, and cURL resource is now passed as first argument.

Prior to version 5.5.0:
<?php
// ...
curl_setopt($resourceCURLOPT_PROGRESSFUNCTION'progressCallback');
curl_setopt($resourceCURLOPT_NOPROGRESSfalse);
// ...
function progressCallback($download_size 0$downloaded 0$upload_size 0$uploaded 0)
{
   
// Handle progress
}
?>

From version 5.5.0:
<?php
// ...
curl_setopt($resourceCURLOPT_PROGRESSFUNCTION'progressCallback');
curl_setopt($resourceCURLOPT_NOPROGRESSfalse);
// ...
function progressCallback($resource$download_size 0$downloaded 0$upload_size 0$uploaded 0)
{
   
// Handle progress
}
?>

However, if your code needs to be compatible with PHP version both before and after 5.5.0, consider adding a version check:
<?php
// ...
curl_setopt($resourceCURLOPT_PROGRESSFUNCTION'progressCallback');
curl_setopt($resourceCURLOPT_NOPROGRESSfalse);
// ...
function progressCallback($resource$download_size 0$downloaded 0$upload_size 0$uploaded 0)
{
   
/**
    * $resource parameter was added in version 5.5.0 breaking backwards compatibility;
    * if we are using PHP version lower than 5.5.0, we need to shift the arguments
    * @see function.curl-setopt#refsect1-function.curl-setopt-changelog
    */
   
if (version_compare(PHP_VERSION'5.5.0') < 0) {
       
$uploaded $upload_size;
       
$upload_size $downloaded;
       
$downloaded $download_size;
       
$download_size $resource;
    }

   
// Handle progress
}
?>
2015-03-12 18:09:28
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
Автор:
Please notice that CURLINFO_HEADER_OUT and CURLOPT_VERBOSE option does not work together:
"When CURLINFO_HEADER_OUT is set to TRUE than CURLOPT_VERBOSE does not work."(from https://bugs.php.net/bug.php?id=65348). 
This took me an hour or two to figure it out.
2016-01-04 12:19:36
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
Автор:
It is important that anyone working with cURL and PHP keep in mind that not all of the CURLOPT and CURLINFO constants are documented. I always recommend reading the cURL documentation directly as it sometimes contains better information. The cURL API in tends to be fubar as well so do not expect things to be where you would normally logically look for them.

curl is especially difficult to work with when it comes to cookies. So I will talk about what I found with PHP 5.6 and curl 7.26.

If you want to manage cookies in memory without using files including reading, writing and clearing custom cookies then continue reading.

To start with, the way to enable in memory only cookies associated with a cURL handle you should use:

    curl_setopt($curl, CURLOPT_COOKIEFILE, "");

cURL likes to use magic strings in options as special commands. Rather than having an option to enable the cookie engine in memory it uses a magic string to do that. Although vaguely the documentation here mentions this however most people like me wouldn't even read that because a COOKIEFILE is the complete opposite of what we want.

To get the cookies for a curl handle you can use:

    curl_getinfo($curl, CURLINFO_COOKIELIST);

This will give an array containing a string for each cookie. It is tab delimited and unfortunately you will have to parse it yourself if you want to do anything beyond copying the cookies.

To clear the in memory cookies for a cURL handle you can use:

    curl_setopt($curl, CURLOPT_COOKIELIST, "ALL");

This is a magic string. There are others in the cURL documentation. If a magic string isn't used, this field should take a cookie in the same string format as in getinfo for the cookielist constant. This can be used to delete individual cookies although it's not the most elegant API for doing so.

For copying cookies I recommend using curl_share_init.

You can also copy cookies from one handle to another like so:

    foreach(curl_getinfo($curl_a, CURLINFO_COOKIELIST) as $cookie_line)
        curl_setopt($curl, CURLOPT_COOKIELIST, $cookie_line);

An inelegant way to delete a cookie would be to skip the one you don't want. 

I only recommend using COOKIELIST with magic strings because the cookie format is not secure or stable. You can inject tabs into at least path and name so it becomes impossible to parse reliably. If you must parse this then to keep it secure I recommend prohibiting more than 6 tabs in the content which probably isn't a big loss to most people.

A the absolute minimum for validation I would suggest:

    /^([^\t]+\t){5}[^\t]+$/D

Here is the format:

    #define SEP  "\t"  /* Tab separates the fields */
 
    char *my_cookie =
      "example.com"    /* Hostname */
      SEP "FALSE"      /* Include subdomains */
      SEP "/"          /* Path */
      SEP "FALSE"      /* Secure */
      SEP "0"          /* Expiry in epoch time format. 0 == Session */
      SEP "foo"        /* Name */
      SEP "bar";       /* Value */
2016-03-09 20:54:55
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
Contrary to the documentation, CURLOPT_STDERR should be set to a handle to the file you want to write to, not the file's location.
2017-03-07 17:22:34
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
What is not mentioned in the documentation is that if you want to set  a local-port or local-port-range to establish a connection is possible by adding   CURLOPT_LOCALPORT and CURLOPT_LOCALPORTRANGE options.   

Ex:
$conn=curl_init ('example.com');
curl_setopt($conn, CURLOPT_LOCALPORT, 35000);
curl_setopt($conn, CURLOPT_LOCALPORTRANGE, 200);

CURLOPT_LOCALPORT: This sets the local port number of the socket used for the connection.

CURLOPT_LOCALPORTRANGE:  The range argument is the number of attempts libcurl will make to find a working local port number. It starts with the given CURLOPT_LOCALPORT and adds one to the number for each retry. Setting this option to 1 or below will make libcurl do only one try for the exact port number.

Interface can be also configured using CURLOPT_INTERFACE:

Ex:

curl_setopt($conn,  CURLOPT_INTERFACE, "eth1");
2017-11-15 21:54:45
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
This is howto upload an existing file to an FTP server with cURL in PHP.

You should remember that CURLOPT_URL should contain the file's basename to save on the FTP server. For example, if you upload hello.txt to ftp://www.wuxiancheng.cn/text/, CURLOPT_URL should be ftp://www.wuxiancheng.cn/text/hello.txt rather than ftp://www.wuxiancheng.cn/text/, otherwise you will get an error message like "Uploading to a URL without a file name! " when you call curl_error();

<?php
    $ch 
curl_init();
   
$filepath 'D:\Web\www\wuxiancheng.cn\hello.txt';
   
$basename pathInfo($filepathPATHINFO_BASENAME);
   
$filesize fileSize($filepath);
   
curl_setopt_array(
       
$ch,
        array(
           
CURLOPT_URL => 'ftp://www.wuxiancheng.cn/text/' $basename,
           
CURLOPT_USERPWD => 'USERNAME:PASSWORD',
           
CURLOPT_PROTOCOLS => CURLPROTO_FTP,
           
CURLOPT_UPLOAD => true,
           
CURLOPT_INFILE => $filepath,
           
CURLOPT_INFILESIZE => $filesize,
           
CURLOPT_RETURNTRANSFER => true,
           
CURLOPT_HEADER => false,
        )
    );
   
curl_exec($ch);
   
$message curl_errno($ch) === CURLE_OK 'success' 'failure';
    echo 
$message;
?>
2017-12-04 16:59:35
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
In the long documentation, it's easy to miss the fact that CURLOPT_POSTFIELDS will set the Content-Type to "multipart/form-data" (instead of the usual "application/x-www-form-urlencoded") IFF you supply an array (instead of a query string)!

Some servers will return weird errors (like "SSL read: error:00000000:lib(0):func(0):reason(0), errno 104") for the wrong Content-Type, and you may waste many hours of time trying to figure out why!
2018-01-17 01:57:44
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
When you get this error using a PUT request: "SSL read: error:00000000:lib(0):func(0):reason(0), errno 104")

It could be caused by:
<?php
curl_setopt
($chCURLOPT_PUTTRUE);
?>

Instead try:
<?php
curl_setopt
($chCURLOPT_CUSTOMREQUEST"PUT");
?>
2018-11-13 15:21:44
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
Автор:
As the "example #2 Uploading file" says it is deprecated as of PHP 5.5.0 but doesn't tell you how it's done right,
here is a really easy example using the CURLFile class:

<?php
$request 
= [
   
'firstName' => 'John',
   
'lastName' => 'Doe',
   
'file' => new CURLFile('example.txt''text/plain'// or use curl_file_create()
];

$curlOptions = [
   
CURLOPT_URL => 'http://example.com/upload.php',
   
CURLOPT_POST => true,
   
CURLOPT_HEADER => false,
   
CURLOPT_RETURNTRANSFER => true,
   
CURLOPT_POSTFIELDS => $request,
];

$ch curl_init();
curl_setopt_array($ch$curlOptions);

$response curl_exec($ch);
?>

This is just like posting a html form with an input[type=file] field.
The result on windows could look like this:

<?php
// $_POST
Array
(
    [
firstName] => John
   
[lastName] => Doe
)

// $_FILES
Array
(
    [
file] => Array
        (
            [
name] => example.txt
           
[type] => text/plain
           
[tmp_name] => C:\wamp64\tmp\php3016.tmp
           
[error] => 0
           
[size] => 14
       
)

)
?>

Since the request is an array (and not a string), curl will automatically encode the data as "multipart/form-data".
Please be aware that if you pass an invalid file path to CURLFile, setting the CURLOPT_POSTFIELDS option will fail.
So if you are using curl_setopt_array for setting the options at once, according to the manual, "If an option could not be successfully set, FALSE is immediately returned, ignoring any future options in the options array.".
So you should make sure that the file exists or set CURLOPT_POSTFIELDS with curl_setopt() and check if it returns false and act accordingly.
2020-11-06 13:18:47
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
Автор:
Set order when using CURLOPT_POST and CURLOPT_POSTFIELDS *matters*.
Setting CURL_POST to true will ERASE any previous CURLOPT_POSTFIELDS using an array. Result is request be a POST with empty body.

CURLOPT_POSTFIELDS will set CURLOPT_POST to true for you, no need for repeat.
If you really need to set both, then either:
  - set CURLOPT_POST *before* CURLOPT_POSTFIELDS
  - or don't use array and convert CURLOPT_POSTFIELDS to URL-encoded string, it will not be affected this way (ie. <?php curl_setopt($chCURLOPT_POSTFIELDShttp_build_query($yourArray)); ?> )
2023-04-04 14:44:41
http://php5.kiev.ua/manual/ru/function.curl-setopt.html
If you want cURL to successfully write cookies to a file specified with CURLOPT_COOKIEJAR, ensure that cURL has the necessary permissions to modify the file if it already exists.

I spent nearly a day trying to understand why cURL wasn't saving cookies to an existing file, even though I could easily modify the exact same file using file_put_contents(). Moreover, cURL itself could create the same file and save cookies, but only if it didn't previously exist.

Ultimately, the issue was related to file ownership. I was working within WSL2, inside a symlinked Windows directory. The [automount]"metadata" in wsl.conf was not set, causing every file created from PHP to have the default owner, which differed from the user running PHP.

Once I configured wsl.conf and then changed the ownership of the entire directory to match the user running PHP, cookies were successfully written to any file without any issues.
2023-08-02 17:06:56
http://php5.kiev.ua/manual/ru/function.curl-setopt.html

    Поддержать сайт на родительском проекте КГБ