if you are trying to connect to 'https://...' and after that want to work with POST data - that's the way:
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_COOKIEFILE, "cookiefile");
curl_setopt($curl, CURLOPT_COOKIEJAR, "cookiefile"); # SAME cookiefile
curl_setopt($curl, CURLOPT_URL, "url1"); # this is where you first time connect - GET method authorization in my case, if you have POST - need to edit code a bit
$xxx = curl_exec($curl);
curl_setopt($curl, CURLOPT_URL, "url2"); # this is where you are requesting POST-method form results (working with secure connection using cookies after auth)
curl_setopt($curl, CURLOPT_POSTFIELDS, "var1=value&var2=value&var3=value&"); # form params that'll be used to get form results
$xxx = curl_exec($curl);
curl_close ($curl);
echo $xxx;
curl_setopt
(PHP 4 >= 4.0.2, PHP 5)
curl_setopt — Eine Option für einen cURL Transfer setzen
Beschreibung
Setzt eine Option für das angegeben cURL-Handle
Parameter-Liste
-
ch -
Ein von curl_init() zurückgegebenes cURL-Handle.
-
option -
Die zu setzende CURLOPT_XXX-Option
-
value -
Der Wert für
valuevaluesollte ein boolean für die folgenden Werte des Parametersoptionsein:Option Wert für valueAnmerkungen CURLOPT_AUTOREFERERTRUEum automatisch den Referer: in Abfragen zu setzen, die einem Redirect per Location: folgenCURLOPT_BINARYTRANSFERTRUEum die unbearbeiteten Daten zurüchzugeben wennCURLOPT_RETURNTRANSFERgesetzt ist.CURLOPT_COOKIESESSIONTRUEum diesen Request als neue "Cookie Session" zu behandeln. Somit wird libcurl alle cookies die geladen werden sollen ignorieren, sofern es sich um "Session Cookies" einer vorherigen Session handelt. Standardmässig lädt und speichert libcurl alle cookies, unabhängig davon, ob es "Session Cookies" sind. Bei "Session Cookies" handelt es sich um Cookies ohne Ablaufdatum, die nur für die aktuelle Session gültig sind.CURLOPT_CRLFTRUEum Unix-Zeilenumbrüche beim Transfer in CRLF-Zeilenumbrüche umzuwandeln.CURLOPT_DNS_USE_GLOBAL_CACHETRUEum den globalen DNS-Cache zu nutzen. Diese Option ist nicht thread-safe und standardmässig aktiviert.CURLOPT_FAILONERRORTRUEum PHP anzuweisen, bei Fehlern (HTTP-Code ist größer oder gleich 400) ohne Fehlermeldung weiter zu arbeiten. Dieses Verhalten ist standardmässig aktiviert.CURLOPT_FILETIMETRUEum zu versuchen, das Änderungsdatum des serverseitigen Dokuments zu ermitteln. Der Wert kann über die Funktion curl_getinfo() unter Verwendung des ParametersCURLINFO_FILETIMEermittelt werden.CURLOPT_FOLLOWLOCATIONTRUEum jedem "Location: "-Header zu folgen, den der Server als Teil der HTTP-Header zurückgibt. Die Verarbeitung erfolgt rekursiv, PHP wird jedem "Location: "-Header folgen, sofern nichtCURLOPT_MAXREDIRSgesetzt ist.CURLOPT_FORBID_REUSETRUEum die Verbindung nach der Verarbeitung explizit zu schließen, so daß sie nicht wiederverwendet werden kann.CURLOPT_FRESH_CONNECTTRUEum den expliziten Aufbau einer neuen Verbindung zu erzwingen, anstatt auf eine gecachte zurückzugreifen.CURLOPT_FTP_USE_EPRTTRUE, um EPRT (und LPRT) für aktive FTP-Downloads zu nutzen. AufFALSEsetzen, um EPRT und LPRT zu deaktivieren und ausschließlich PORT zu nutzen.CURLOPT_FTP_USE_EPSVTRUEum vor dem Fallback auf PASV zunächst eine EPSV-Anweisung für einen FTP-Transfer auszuführen. AufFALSEsetzen, um EPSV zu deaktivieren.CURLOPT_FTPAPPENDTRUEum Daten an die serverseitige Datei anzuhängen anstatt diese zu überschreiben.CURLOPT_FTPASCIIEin Alias für CURLOPT_TRANSFERTEXT, das bevorzugt werden sollte.CURLOPT_FTPLISTONLYTRUEum nur die Namen in einem FTP-Verzeichnis aufzulisten.CURLOPT_HEADERTRUEum den Header in die Ausgabe aufzunehmen.CURLINFO_HEADER_OUTTRUEum den Request-String des Handles zu verfolgen.Verfügbar seit PHP 5.1.3. DasPrefix CURLINFO_ist beabsichtigt.CURLOPT_HTTPGETTRUEum die HTTP-Request-Methode auf GET zu setzen. Da dies die Standard-Methode ist sollte dies nur nach einem Wechsel der Request-Methode notwendig sein.CURLOPT_HTTPPROXYTUNNELTRUEum einen vorhandenen Proxy-Tunnel zu nutzenCURLOPT_MUTETRUEum jegliche Ausgabe der cURL-Funktionen zu unterbindenCURLOPT_NETRCTRUEum die lokale Datei ~/.netrc nach Benutzername/Passwort für die Authorisierung des Zugriffs zu durchsuchen.CURLOPT_NOBODYTRUEum den Body nicht in die Ausgabe aufzunehmen.CURLOPT_NOPROGRESSTRUEum die Fortschrittsanzeige für den Transfer auszublenden.Hinweis:
PHP setzt die Option automatisch auf
TRUE. Dies sollte ausschließlich für die Fehlersuche geändert werden.CURLOPT_NOSIGNALTRUEum jegliche cURL-Funktion zu ignorieren, die ein Signal an den PHP-Prozess sendet. In multi-threaded SAPIs ist diese Option standardmässig aktiviert, so daß Timeouts weiterhin abgefangen werden können.Hinzugefügt in cURL 7.10 und PHP 5.0.0. CURLOPT_POSTTRUEum einen HTTP-POST-Request abzusetzen. Dabei handelt es sich um das übliche application/x-www-form-urlencoded, wie es im Allgemeinen von HTML-Formularen erzeugt wird.CURLOPT_PUTTRUEum ein HTTP-PUT für eine Datei abzusetzen. Die fragliche Datei muss dabei über die KonstantenCURLOPT_INFILEundCURLOPT_INFILESIZEgesetzt werden.CURLOPT_RETURNTRANSFERTRUEum den Tranfer als String zurückzuliefern, anstatt ihn direkt auszugeben.CURLOPT_SSL_VERIFYPEERFALSEum die Überprüfung des Peerzertifikats seitens cURL zu unterdrücken. Alternative Zertifikate zur Überprüfung können mit der OptionCURLOPT_CAINFOangegeben werden oder ein Zertifikat-Verzeichniss kann mitCURLOPT_CAPATHausgewiesen werden. Ebenso mußCURLOPT_SSL_VERIFYHOSTggf. aufTRUEoderFALSEgesetzt werden, sofernCURLOPT_SSL_VERIFYPEERdeaktiviert ist.TRUEals Standard seit cURL 7.10.CURLOPT_TRANSFERTEXTTRUEum FTP-Transfers im ASCII-Modus durchzuführen. Für LDAP werden Daten in Klartext statt HTML übertragen. Unter Windows wird STDOUT nicht in den binären Modus gesetzt.CURLOPT_UNRESTRICTED_AUTHTRUEum beim Folgen eines Location: -Headers (sieheCURLOPT_FOLLOWLOCATION) weiterhin Benutzername und Passwort zu senden, sogar nach einem Wechsel des Hostnamens.CURLOPT_UPLOADTRUEum einen Upload vorzubereitenCURLOPT_VERBOSETRUEum ausführliche Informationen auszugeben, entweder nach STDERR oder in die mittels der OptionCURLOPT_STDERRgewählte Datei.Für die folgenden
option-Parameter solltevalueein Integer sein:Option Wert für valueAnmerkungen CURLOPT_BUFFERSIZEDie für jede Leseoperation zulässige Buffergröße. Es ist nicht garantiert, daß diese Einstellung genutzt wird. Hinzugefügt in cURL 7.10 and PHP 5.0.0. CURLOPT_CLOSEPOLICYSollte entweder CURLCLOSEPOLICY_LEAST_RECENTLY_USEDoderCURLCLOSEPOLICY_OLDEST. Zwar existieren drei weitere CURLCLOSEPOLICY_-Konstanten, diese werden allerdings bisher nicht von cURL unterstützt.CURLOPT_CONNECTTIMEOUTDie Anzahl Sekunden, die der Verbindungsaufbau maximal dauern darf; 0 hebt die Begrenzung auf. CURLOPT_CONNECTTIMEOUT_MSDie Anzahl Millisekunden, die der Verbindungsaufbau maximal dauern darf; 0 hebt die Begrenzung auf. Added in cURL 7.16.2. Available since PHP 5.2.3. CURLOPT_DNS_CACHE_TIMEOUTDie Dauer in Sekunden, die ein DNS-Eintrag im Speicher gehalten wird. Der Standard sind 120 Sekunden (2 Minuten). CURLOPT_FTPSSLAUTHDie FTP-Authentisierungsmethode (wenn aktiviert): CURLFTPAUTH_SSL (versuche zunächst SSL), CURLFTPAUTH_TLS (versuche zunächst TLS), oder CURLFTPAUTH_DEFAULT (lass cURL entscheiden). Hinzugefügt in cURL 7.12.2 und PHP 5.1.0. CURLOPT_HTTP_VERSIONCURL_HTTP_VERSION_NONE(Standard, lässt cURL entscheiden, welche Version genutzt werden soll),CURL_HTTP_VERSION_1_0(nutze HTTP/1.0), orCURL_HTTP_VERSION_1_1(nutze HTTP/1.1).CURLOPT_HTTPAUTHDie zu nutzende HTTP-Authentisierungsmethode; zur Verfügung stehen:
CURLAUTH_BASIC,CURLAUTH_DIGEST,CURLAUTH_GSSNEGOTIATE,CURLAUTH_NTLM,CURLAUTH_ANY, undCURLAUTH_ANYSAFE.Um mehrere Methoden zu kombinieren kann der Bit-|(oder)-Operator verwendet werden; in diesem Fall wird cURL in Abstimmung mit dem Server die günstigste Methode auswählen.
CURLAUTH_ANYist ein Alias für CURLAUTH_BASIC | CURLAUTH_DIGEST | CURLAUTH_GSSNEGOTIATE | CURLAUTH_NTLM.CURLAUTH_ANYSAFEist ein Alias für CURLAUTH_DIGEST | CURLAUTH_GSSNEGOTIATE | CURLAUTH_NTLM.CURLOPT_INFILESIZEDie erwartete Dateigröße der hochzuladenden Datei in Bytes CURLOPT_LOW_SPEED_LIMITDie Transfergeschwindigkeit in Bytes pro Sekunde, die bei Unterschreitung in Kombination mit der Überschreitung von CURLOPT_LOW_SPEED_TIMESekunden bewirkt, dass der Transfer aufgrund der zu niedrigen Transferrate abgebrochen wird.CURLOPT_LOW_SPEED_TIMEDie Zeit in Sekunden, in der die Transferrate unter CURLOPT_LOW_SPEED_LIMITgedultet wird. Nach dieser Zeit wird die Transferrate als zu langsam angesehen und der Transfer wird beendet.CURLOPT_MAXCONNECTSDie maximal erlaubte Anzahl persistenter Verbindungen; bei Erreichen des Limits wird mittels CURLOPT_CLOSEPOLICYentschieden, welche Verbindung geschlossen wird.CURLOPT_MAXREDIRSDie maximal erlaubte Anzahl von HTTP-Weiterleitungen. Verwenden Sie diese Option zusammen mit CURLOPT_FOLLOWLOCATION.CURLOPT_PORTErlaubt das Setzen eines alternativen Ports für die Verbindung. CURLOPT_PROTOCOLSEine Bitmaske von
CURLPROTO_*-Werten. Mit dieser Einstellung lassen sich die Protokolle einschränken die libcurl für diesen Transfer nutzen darf. Damit wird es möglich ein libcurl mit einer Vielzahl an Protokollen zu benutzen, gleichzeitig aber lassen sich die für einen bestimmten Transfer zulässigen Protokolle beschränken. Standardmässig akzeptiert libcurl alle unterstützten Protokolle. Siehe auchCURLOPT_REDIR_PROTOCOLS.Zulässige Optionen:
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_ALLHinzugefügt in cURL 7.19.4. CURLOPT_PROXYAUTHDie HTTP-Authentisierungsmethode(n) für die Proxy-Verbindung. Nutzt die gleiche Bitmaske wie in CURLOPT_HTTPAUTHbeschrieben. NurCURLAUTH_BASICundCURLAUTH_NTLMsind momentan für Proxy-Verbindungen zulässig.Hinzugefügt in cURL 7.10.7 und PHP 5.1.0. CURLOPT_PROXYPORTDer Port, auf den die Proxy-Verbindung erfolgen soll; kann auch mittels CURLOPT_PROXYgesetzt werden.CURLOPT_PROXYTYPEEntweder CURLPROXY_HTTP(Standard) oderCURLPROXY_SOCKS5.Hinzugefügt in cURL 7.10. CURLOPT_REDIR_PROTOCOLSEine Bitmaske von CURLPROTO_*-Werten. Mit dieser Einstellung lassen sich die Protokolle einschränken die libcurl für diesen Transfer nutzen darf, wenn eine Weiterleitung stattfindet (setzt voraus daßCURLOPT_FOLLOWLOCATIONerlaubt ist). Damit wird es möglich ein libcurl mit einer Vielzahl an Protokollen zu benutzen, gleichzeitig aber lassen sich die für eine Weiterleitung zulässigen Protokolle beschränken. Standardmässig akzeptiert libcurl alle unterstützten Protokolle außer FILE und SCP. Siehe auchCURLOPT_PROTOCOLSfür weitere Informationen zu den Protokoll-Konstanten.Added in cURL 7.19.4. CURLOPT_RESUME_FROMDie Position in Bytes ab der ein Transfer fortgesetzt werden soll. CURLOPT_SSL_VERIFYHOST1 um auf die Existenz des Namens im Zertifikat zu prüfen, 2 stellt sicher, daß eine Übereinstimmung mit dem angegebenen Hostnamen vorliegt. CURLOPT_SSLVERSIONEin Wert von 2 oder 3 wählt die entsprechende SSL-Version. Standardmässig wird PHP versuchen, die entsprechende Einstellung zu ermitteln, aber in einigen Fällen mag das manuelle Setzen dieser Option nötig sein. CURLOPT_TIMECONDITIONDieser Wert gibt an, wie CURLOPT_TIMEVALUEbehandelt werden soll. Mögliche Werte sindCURL_TIMECOND_IFMODSINCE(Standard) undCURL_TIMECOND_ISUNMODSINCE. Im ersten Fall wird geprüft, ob die Seite seitCURLOPT_TIMEVALUEgeändert wurde; andernfalls wird ein "304 Not Modified"-Header zurückgegeben (vorausgesetztCURLOPT_HEADERistTRUE).CURL_TIMECOND_ISUNMODSINCEbewirkt das gegenteilige Verhalten.CURLOPT_TIMEOUTDie maximale Ausführungszeit in Sekunden für cURL-Funktionen. CURLOPT_TIMEOUT_MSDie maximale Ausführungszeit in Millisekunden für cURL-Funktionen. Added in cURL 7.16.2. Available since PHP 5.2.3. CURLOPT_TIMEVALUEZeit in Sekunden seit dem 1. Januar 1970. Dieser Wert wird von CURLOPT_TIMECONDITIONgenutzt. Als Standard wirdCURL_TIMECOND_IFMODSINCEgenutzt.Für die folgenden
option-Parameter solltevalueein String sein:Option Wert für valueAnmerkungen CURLOPT_CAINFODer Name einer Datei, die ein oder mehrere Zerifikate enthält, gegen die der Peer geprüft wird. Macht nur Sinn in Verbindung mit CURLOPT_SSL_VERIFYPEER.CURLOPT_CAPATHEin Verzeichnis, das mehrere CA-Zertifikate enthält. Diese Option sollte In Kombination mit CURLOPT_SSL_VERIFYPEERgenutzt werden.CURLOPT_COOKIEDer Inhalt des im HTTP-Request zu setzenden "Set-Cookie: "-Headers Beachten Sie daß mehrere Cookies durch ein Semikolon gefolgt von einem Leerzeichen getrennt werden (z.B. "fruit=apple; colour=red") CURLOPT_COOKIEFILEDer Name einer Datei, die Cookiedaten enthält. Diese Datei kann im Netscape-Format sein oder HTTP-geformte Header enthalten. CURLOPT_COOKIEJARDer Name einer Datei in der alle internen Cookies beim Schließen des Handles gespeichert werden, z.B. nach einem Aufruf von curl_close. CURLOPT_CUSTOMREQUESTEine benutzerdefinierte Request-Methode, die anstelle von GET oder HEAD für den HTTP-Request benutzt werden soll. Dies ist nützlich bei DELETE oder anderen unüblichen Requests. Zulässige Werte sind GET, POST, CONNECT etc. Vollständige HTTP-Requests wie GET /index.html HTTP/1.0\r\n\r\n sind unzulässig!
Hinweis:
Führen Sie diese Anfragen nicht durch ohne sicherzugehen, daß Ihr Server die betreffenden Kommandos unterstützt.
CURLOPT_EGDSOCKETAnalog CURLOPT_RANDOM_FILE, hier wird der Dateiname eines Entropy Gathering Daemon Socket erwartet.CURLOPT_ENCODINGDer Inhalt des "Accept-Encoding: "-Headers. Damit wird das Dekodieren der Serverantwort aktiviert. Unterstützte Kodierungen sind identity, deflate und gzip. Wird ein leerer String "" gesetzt wird ein Header mit allen unterstützten Kodierungen gesetzt. Hinzugefügt in cURL 7.10. CURLOPT_FTPPORTEin String, der genutzt wird, um die IP-Adresse zu erlangen, die von der FTP-POST-Anweisung genutzt wird. Die POST-Anweisung teilt dem entfernten Server mit, zu der angegebenen IP-Adresse zu verbinden. Der String kann eine einfache IP-Addresse, ein Hostname, der Name eines Netzwerkinterfaces (unter Unix) oder nur '-' sein, um die vordefinierte IP-Addresse (des Systems) zu nutzen. CURLOPT_INTERFACEDer Name des zu nutzenden Netzwerkinterfaces für ausgehende Daten. Statt des Namens des Interfaces kann auch eine IP-Adresse oder ein Hostname übergeben werden. CURLOPT_KRB4LEVELDie KRB4 (Kerberos 4) Sicherheitsstufe. Folgende Werte (von niedriger zu höherer Stufe) sind gültig: clear, safe, confidential, private. Sollte der String keinen dieser Werte enthalten dann wird die höchste Stufe genutzt, d.h private. Sollte diese Option mit dem Wert NULL besetzt werden, wird KRB4 Security deaktiviert. Bislang wird KRB4 nur unter Verwendung des FTP unterstützt. CURLOPT_POSTFIELDSDie in einem HTTP-POST-Request zu nutzenden Daten. Um eine Datei zu posten stellen Sie dem Dateinamen @ voran; bitte geben Sie den vollen Pfad zur Datei an. Als Wert kann entweder ein URL-kodierter String übergeben werden wie z.B. 'para1=val1¶2=val2&...' oder ein Array, wobei die Feldnamen als Schlüssel und die Felddaten als Wert verwendet werden. Wird ein Array für valuedann wird der Content-Type-Header auf multipart/form-data gesetzt.CURLOPT_PROXYDer HTTP-Proxy, durch den Requests getunnelt werden sollen. CURLOPT_PROXYUSERPWDBenutzername und Passwort in der Form "[benutzername]:[passwort]" für die Proxy-Verbindung. CURLOPT_RANDOM_FILEEin Dateiname der zur Initialisierung des Zufallsgenerators für SSL benutzt wird. CURLOPT_RANGEBereiche an Daten, die empfangen werden sollen. Das Format sollte "X-Y" sein, wobei X oder Y optional sind. HTTP-Transfers unterstützen auch mehrere Komma-getrennte Intervalle im Format "X-Y,N-M". CURLOPT_REFERERDer Inhalt des "Referer: "-Headers CURLOPT_SSL_CIPHER_LISTEine Liste der für SSL zulässigen Chiffren. RC4-SHA und TLSv1 sind zulässig. CURLOPT_SSLCERTDer Name einer Datei die ein Zertifikat im PEM-Format enthält. CURLOPT_SSLCERTPASSWDDas Passwort für das CURLOPT_SSLCERT-Zertifikat.CURLOPT_SSLCERTTYPEDas Format des Zertifikats. Unterstützt werden PEM (Standard), DER und ENG. Hinzugefügt in cURL 7.9.3. CURLOPT_SSLENGINEDer Bezeichner für die Crypto Engine des privaten SSL-Schlüssels, der in CURLOPT_SSLKEYdefiniert ist.CURLOPT_SSLENGINE_DEFAULTDer Bezeichner für die Crypto-Engine für asymmetrische kryptographische Operationen. CURLOPT_SSLKEYDer Name einer Datei, die einen privaten SSL-Schlüssel enthält. CURLOPT_SSLKEYPASSWDDas geheime Passwort das für den in
CURLOPT_SSLKEYdefinierten privaten SSL-Schlüssel.Hinweis:
Da diese Option sensible Daten enthält sollte das PHP-Script in einer sicheren Umgebung liegen.
CURLOPT_SSLKEYTYPEDer Typ des in CURLOPT_SSLKEYdefinierten privaten SSL-Schlüssels. Unterstützte Schlüssel-Typen sind PEM (Standard), DER und ENG.CURLOPT_URLDer abzurufende URL; kann auch beim initialisieren der Session mittels curl_init() gesetzt werden. CURLOPT_USERAGENTDer Wert des "User-Agent: "-Headers für den HTTP-Request CURLOPT_USERPWDBenutzername und Passwort im Format "[benutzername]:[passwort]" Für die folgenden
option-Parameter solltevalueein Array sein:Option Wert für valueAnmerkungen CURLOPT_HTTP200ALIASESEin Array von HTTP-200-Status die als gültige Antoworten und nicht als Fehler behandelt werden. Hinzugefügt in cURL 7.10.3. CURLOPT_HTTPHEADEREin Array von HTTP-Headern, im Format array('Content-type: text/plain', 'Content-length: 100')CURLOPT_POSTQUOTEEin Array von FTP-Kommandos, die nach dem FTP-Request auf dem Server ausgeführt werden sollen. CURLOPT_QUOTEEin Array von FTP-Kommandos, die vor dem FTP-Request auf dem Server ausgeführt werden sollen. Für die folgenden
option-Parameter solltevalueeine Stream-Resource sein, so wie sie z.B. mittels fopen() erstellt werden kannOption Wert für valueCURLOPT_FILEDie Datei, in die der Transfer geschrieben werden soll. Standard ist STDOUT (der Browser). CURLOPT_INFILEDie Datei die zum Upload gelesen werden soll. CURLOPT_STDERREine Datei, in die Fehler ausgegeben werden, alternativ zu STDERR. CURLOPT_WRITEHEADERIn diese Datei werden die Header eines Transfers geschrieben. Für die folgenden
option-Parameter solltevalueein Callback sein, der eine valide Callback-Funktion repräsentiert.Option Wert für valueCURLOPT_HEADERFUNCTIONEine Callback-Funktion, die zwei Parameter erwartet. Der erste ist die cURL-Resource, der zweite ist ein String mit den zu schreibenden Headern. Bei Nutzung dieser Callback-Funktion liegt die Verantwortung für das Schreiben der Header bei Ihnen. Die Funktion sollte die Anzahl der geschriebenen Bytes zurückgeben. CURLOPT_PASSWDFUNCTIONEine Callback-Funktion, die drei Parameter erwartet. Der erste ist die cURL-Resource, der zweite ein String der ein Passwort-Prompt enthält, der dritte Parameter enthält die maximal zulässige Länge des Passworts. Die Funktion sollte das Passwort als String zurückgeben. CURLOPT_PROGRESSFUNCTIONEine Callback-Funktion, die drei Parameter erwartet. Der erste ist die cURL-Resource, der zweite ist eine Datei-Resource und der dritte ist die Länge; liefert eineb String mit den Werten zurück. CURLOPT_READFUNCTIONEine Callback-Funktion, die zwei Parameter erwartet. Der erste ist die cURL-Resource, der zweite ist ein String mit den zu lesenden Daten. Bei Nutzung dieser Callback-Funktion liegt die Verantwortung für das Lesen der Daten bei Ihnen. Die Funktion sollte die Anzahl der gelesenen Bytes zurückgeben, 0 um EOF zu signalisieren. CURLOPT_WRITEFUNCTIONEine Callback-Funktion, die zwei Parameter erwartet. Der erste ist die cURL-Resource, der zweite ist ein String mit den zu schreibenden Daten. Bei Nutzung dieser Callback-Funktion liegt die Verantwortung für das Speichern der Daten bei Ihnen. Die Funktion muss die genaue Anzahl geschriebener Bytes zurückgeben!
Rückgabewerte
Gibt bei Erfolg TRUE zurück. Im Fehlerfall wird FALSE zurückgegeben.
Changelog
| Version | Beschreibung |
|---|---|
| 5.2.10 |
Es wurden CURLOPT_PROTOCOLS und
CURLOPT_REDIR_PROTOCOLS eingeführt.
|
| 5.1.0 |
Es wurden CURLOPT_AUTOREFERER,
CURLOPT_BINARYTRANSFER,
CURLOPT_FTPSSLAUTH,
CURLOPT_PROXYAUTH und
CURLOPT_TIMECONDITION eingeführt.
|
| 5.0.0 |
Es wurden CURLOPT_FTP_USE_EPRT,
CURLOPT_NOSIGNAL,
CURLOPT_UNRESTRICTED_AUTH,
CURLOPT_BUFFERSIZE,
CURLOPT_HTTPAUTH,
CURLOPT_PROXYPORT,
CURLOPT_PROXYTYPE,
CURLOPT_SSLCERTTYPE und
CURLOPT_HTTP200ALIASES eingeführt.
|
Beispiele
Beispiel #1 Initialisierung einer neuen cURL-Session und das Abrufen einer Webseite
<?php
// erzeuge einen neuen cURL-Handle
$ch = curl_init();
// setze die URL und andere Optionen
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, false);
// führe die Aktion aus und gebe die Daten an den Browser weiter
curl_exec($ch);
// schließe den cURL-Handle und gebe die Systemresourcen frei
curl_close($ch);
?>
Beispiel #2 Datei-Upload
<?php
/* http://localhost/upload.php:
print_r($_POST);
print_r($_FILES);
*/
$ch = curl_init();
$data = array('name' => 'Foo', 'file' => '@/home/user/test.png');
curl_setopt($ch, CURLOPT_URL, 'http://localhost/upload.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
?>
Das oben gezeigte Beispiel erzeugt folgende Ausgabe:
Array
(
[name] => Foo
)
Array
(
[file] => Array
(
[name] => test.png
[type] => image/png
[tmp_name] => /tmp/phpcpjNeQ
[error] => 0
[size] => 279
)
)
Anmerkungen
Hinweis:
Ein für
CURLOPT_POSTübergebenes Array wird als multipart/form-data, ein URL-kodierter String als application/x-www-form-urlencoded kodiert.
curl_setopt
06-Jul-2007 12:09
27-Jun-2007 08:18
Two things that I noted, one of which has been mentioned earlier, if you are connecting to an SSL site (https) and don't have the appropriate certificate, don't forget to set CURLOPT_SSL_VERIFYPEER as "false"... it's set to "true" by default. Scratched my head over 2 hours to figure this one out as I had a machine with an older version installed and everything worked fine without using this option on that one - but failed on other machines with newer versions.
Second very important thing, I've never had my scripts work (tried on various machines, multiple platforms) with a Relative path to a COOKIEJAR or COOKIEFILE. In my experience I HAVE to specify the absolute path and not the relative path.
Small script I wrote to connect to a page, gather all cookies into a jar, connect to another page to login, taking the cookiejar with you for authentication:
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, "/Library/WebServer/Documents/tmp/cookieFileName");
curl_setopt($ch, CURLOPT_URL,"https://www.example.com/myaccount/start.asp");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
ob_start(); // Prevent output
curl_exec ($ch);
ob_end_clean(); // End preventing output
curl_close ($ch);
unset($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "field1=".$f1."&field2=".$f2."&SomeFlag=True");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_COOKIEFILE, "/Library/WebServer/Documents/tmp/cookieFileName");
curl_setopt($ch, CURLOPT_URL,"https://www.example.com/myaccount/Login.asp");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec ($ch);
curl_close ($ch);
12-Jun-2007 11:35
If you are getting the following error:
SSL: certificate subject name 'domain-or-ip-1.com' does not match target host name 'domain-or-ip-2.com'
Then you can set the following option to get around it:
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
12-Jun-2007 10:55
Please note that the CURLOPT_INTERFACE setting only accepts IP addresses and hostnames of the local machine. It is not meant to send a URL to a specific IP address.
12-May-2007 06:52
curl will sometimes return an "Empty reply from server" error if you don't send a User-Agent string. Use the CURLOPT_USERAGENT option.
24-Apr-2007 06:01
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.
14-Mar-2007 01:26
Seems that CURLOPT_RETURNTRANSFER Option set to TRUE, returns a "1" when the transaction returns a blank page.
I think is for eliminate the FALSE to can be with a blank page as return
08-Mar-2007 03:50
How to get rid of response after POST: just add callback function for returned data (CURLOPT_WRITEFUNCTION) and make this function empty.
function curlHeaderCallback($ch, $strHeader) {
}
curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'curlHeaderCallback')
03-Mar-2007 03:36
In response to "brett at brettbrewer dot com" and Jamolkhon, the fact you don't take into account is when a redirection (Location: ...) occurs, all the headers are given so the \r\n\r\n can also separate twp consecutive headers.
An even better trick to get the header is to use CURLINFO_HEADER_SIZE :
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$headers = substr($output, 0, $header_size - 4);
$body = substr($output, $header_size);
Then $headers contains all the headers.
15-Dec-2006 08:11
in response to "brett at brettbrewer dot com":
another useful way of getting headers without using regular expression
<?php
class CCurl {
var $m_handle;
var $m_header;
var $m_body;
function CCurl($sUrl) {
$this->m_handle = curl_init();
curl_setopt($this->m_handle, CURLOPT_URL, $sUrl);
curl_setopt($this->m_handle, CURLOPT_HEADER, 1);
curl_setopt($this->m_handle, CURLOPT_RETURNTRANSFER, 1);
return;
}
function getHeader() {
return $this->m_header;
}
function execute() {
$sResponse = curl_exec($this->m_handle);
$this->m_body = substr($sResponse, strpos($sResponse, "\r\n\r\n") + 4);
$this->m_header = substr($sResponse, 0, -strlen($this->m_body));
return $this->m_body;
}
function close() {
curl_close($this->m_handle);
return;
}
}
?>
(you can add something or make some changes to suit your needs)
22-Nov-2006 05:40
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($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
list($header, $data) = explode("\n\n", $data, 2);
$http_code = curl_getinfo($ch, CURLINFO_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($ch, CURLINFO_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($ch, CURLOPT_URL, $new_url);
debug('Redirecting to', $new_url);
return curl_redir_exec($ch);
} else {
$curl_loops=0;
return $data;
}
}
?>
09-Nov-2006 08:27
I have seen two posts complaining the "CURLOPT_POSTFIELDS" option is not working well.
This was the solution given below:
$o="";
foreach ($post_data as $k=>$v)
{
$o.= "$k=".utf8_encode($v)."&";
}
$post_data=substr($o,0,-1);
The assumption that this is an error is not true, as stated on http://curl.rtin.bz/docs/httpscripting.html:
" The data you send to the server MUST already be properly encoded, curl will
not do that for you. For example, if you want the data to contain a space,
you need to replace that space with %20 etc. Failing to comply with this
will most likely cause your data to be received wrongly and messed up."
This means you do really have to encode the data yourself the right way.
01-Oct-2006 01:51
As Yevgen mentioned earlier sometimes we can't use CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE. Below is a header callback function I wrote back in January that lets you maintain cookies between cURL requests. Cookies are added to $ch during all requests even during redirection, so you can use it together with CURLOPT_FOLLOWLOCATION.
Here is the code:
function read_header($ch, $string)
{
global $location; #keep track of location/redirects
global $cookiearr; #store cookies here
global $ch;
# ^overrides the function param $ch
# this is okay because we need to
# update the global $ch with
# new cookies
$length = strlen($string);
if(!strncmp($string, "Location:", 9))
{ #keep track of last redirect
$location = trim(substr($string, 9, -1));
}
if(!strncmp($string, "Set-Cookie:", 11))
{ #get the cookie
$cookiestr = trim(substr($string, 11, -1));
$cookie = explode(';', $cookiestr);
$cookie = explode('=', $cookie[0]);
$cookiename = trim(array_shift($cookie));
$cookiearr[$cookiename] = trim(implode('=', $cookie));
}
$cookie = "";
if(trim($string) == "")
{ #execute only at end of header
foreach ($cookiearr as $key=>$value)
{
$cookie .= "$key=$value; ";
}
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
}
return $length;
}
Using the header function with curl (add this before curl_exec):
#don't forget globals, especially if you are using this in function
curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');
This code assumes that you will reuse $ch without initializing it every time (call curl_init only once, in the beginning). If you need to initialize $ch again at any point in your code you can access the currently stored cookies in $cookiearr and include them in the new $ch.
I wrote this function before I had enough experience with regular expressions so you won't find any preg_match calls here. I have used this code for quite a while and without any problems accessing gmail, yahoo, hotmail, aol etc. where I had to go through login and a few pages before getting to what I was looking for.
Svetlozar Petrov (http://svetlozar.net)
23-Aug-2006 07:35
Note that if you want to use a proxy and use it as a _cache_, you'll have to do:
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Pragma: "));
else by default Curl puts a "Pragma: no-cache" header in and thus force cache misses for all requests.
14-Jul-2006 12:58
You can use CURLOPT_HEADERFUNCTION with a callback inside an object. This makes is it easy to capture the headers for later use. For example:
<?
class Test
{
public $headers;
//...
public function exec($opts)
{
$this->headers = array();
$opts[CURLOPT_HEADERFUNCTION] = array($this, '_setHeader');
$ch = curl_init();
curl_setopt_array($ch, $opts);
return curl_exec($ch);
}
private function _setHeader($ch, $header)
{
$this->headers[] = $header;
return strlen($header);
}
}
$test = new Test();
$opts = array(
//... your curl opts here
);
$data = $test->exec($opts);
print_r($test->headers);
?>
...something like that
(This works in php v. 5.1.4)
30-May-2006 08:31
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
14-Apr-2006 04:22
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
27-Mar-2006 12:59
- CURLOPT-HTTPAUTH is defined in php 4.4.2 9and maybe lower), even tough the manual says php 5.0 only...
- it is not clear from the manual what kind of http auth is carried out by default if user specifies username/pwd: it is BASIC auth
ps: note to manual writers: the version info on this page always looks a bit inaccurate, especially regarding the php version where a given option first appeared...
27-Dec-2005 11:24
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.
22-Dec-2005 11:13
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 = "") {
assert( is_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($ch, CURLOPT_POSTFIELDS, substr(data_encode($_POST), 0, -1) );
?>
23-Oct-2005 03:34
Resetting CURLOPT_FILE to STDOUT won't work by calling curl_setopt() with the STDOUT constant or a php://output stream handle (at least I get error messages when trying the code from phpnet at andywaite dot com). Instead, one can simply reset it as a side effect of CURLOPT_RETURNTRANSFER. Just say
curl_setopt($this->curl,CURLOPT_RETURNTRANSFER,0);
and following calls to curl_exec() will output to STDOUT again.
20-Oct-2005 10:42
A little mistake, that took a half-day to fix it:
When specifing CURLOPT_COOKIEFILE or CURLOPT_COOKIEJAR options, don't forget to "chmod 777" that directory where cookie-file must be created.
23-Aug-2005 05:10
when specifing the file for either CURLOPT_COOKIEFILE or CURLOPT_COOKIEJAR you may need to use the full file path instead of just the relative path.
07-Jun-2005 11:08
After setting CURLOPT_FILE, you may want want to revert back to the normal behaviour of displaying the results. This can be achieved using:
$fp = fopen ("php://output", "w") or die("Unable to open stdout for writing.\n");
curl_setopt($ch, CURLOPT_FILE, $fp);
09-May-2005 06:26
Using cURL, I needed to call a third-party script which was returning binary data as attachment to pass on retrieved data again as attachment.
Problem was that the third-party script occassionally returned HTTP errors and I wanted to avoid passing on zero-length attachment in such case.
Combination of using CURLOPT_FAILONERROR and CURLOPT_HEADERFUNCTION callback helped to process the third-party script HTTP errors neatly:
<?
function curlHeaderCallback($resURL, $strHeader) {
if (preg_match('/^HTTP/i', $strHeader)) {
header($strHeader);
header('Content-Disposition: attachment; filename="file-name.zip"');
}
return strlen($strHeader);
}
$strURL = 'http://www.somesite.com/script-whichs-dumps-binary-attachment.php';
$resURL = curl_init();
curl_setopt($resURL, CURLOPT_URL, $strURL);
curl_setopt($resURL, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($resURL, CURLOPT_HEADERFUNCTION, 'curlHeaderCallback');
curl_setopt($resURL, CURLOPT_FAILONERROR, 1);
curl_exec ($resURL);
$intReturnCode = curl_getinfo($resURL, CURLINFO_HTTP_CODE);
curl_close ($resURL);
if ($intReturnCode != 200) {
print 'was error: ' . $intReturnCode;
}
?>
05-May-2005 02:00
I don't know if in PHP 5+ this problem is solved, but, when I send via POST using CURLOPT_POSTFIELDS some times it doesnt work an associative array as parameter... and, if I need to send non-ascii characters (like á,é,ñ,ü,etc..) this problem appears too.
The solution is to encode in a unique string all the post key=>values
Possibly this code doesnt work:
<?php
$post_data = array();
$post_data['var1'] = "Diseño web uruguay";
$post_data['var2'] = "Other string";
$url = "http://www.someurl.com/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, );
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$result = curl_exec($ch);
?>
But this works:
<?php
$post_data = array();
$post_data['var1'] = "Diseño web uruguay";
$post_data['var2'] = "Other string";
$url = "http://www.someurl.com/";
$o="";
foreach ($post_data as $k=>$v)
{
$o.= "$k=".utf8_encode($v)."&";
}
$post_data=substr($o,0,-1);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, );
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$result = curl_exec($ch);
?>
Rgds.
Alejandro Moreno.-
23-Mar-2005 07:03
AZAds is a traffic exchange that we wrote. We wanted to come up with a way to check the UP or DOWN status of a submitted URL. Here is the solution we came up with. Can also be used for a site monitoring service.
<?
$uServer="$url";
@$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$uServer");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
@curl_exec($ch);
$errnum=curl_errno($ch);
@curl_close($ch);
if($errnum != "0") {
print "Remote Site Status: DOWN";
} else {
print "Remote Site Status: UP";
}
?>
Simply feed the URL to be tested into $url. The code will check the site and return the error code if there is one and display to appropriate message.
Hope this is useful.
Keith
www.azads.net
21-Feb-2005 06:38
Another way to automate a login process and store multiple cookies for additional transfers.
(The easiest way is probably just to use CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE and save them to a file. But if you need to change those cookie values or add/delete them then you can use the code below to set them.)
Continuing with Paul Ebermann's parsing function.
function parse_response($this_response)
{
// Split response into header and body sections
list($response_headers, $response_body) = explode("\r\n\r\n", $this_response, 2);
$response_header_lines = explode("\r\n", $response_headers);
// First line of headers is the HTTP response code
$http_response_line = array_shift($response_header_lines);
if(preg_match('@^HTTP/[0-9]\.[0-9] ([0-9]{3})@',$http_response_line, $matches)) { $response_code = $matches[1]; }
// put the rest of the headers in an array
$response_header_array = array();
foreach($response_header_lines as $header_line)
{
list($header,$value) = explode(': ', $header_line, 2);
$response_header_array[$header] .= $value."\n";
}
return array("code" => $response_code, "header" => $response_header_array, "body" => $response_body);
}
// Do transfer, and make sure to include header in response
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "[login process]");
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
// Parse the response message
$response = parse_response($response);
// Create the basic header
$this_header = array(
"MIME-Version: 1.0",
"Content-type: text/html; charset=iso-8859-1",
"Content-transfer-encoding: text"
);
// Add each cookie that has been returned in the response
// If cookies need to be added/deleted or value changed, then add code here
$cookies = explode("\n", $response["header"]["Set-Cookie"]);
foreach($cookies as $this_cookie) { array_push($this_header, "Cookie: ".$this_cookie); }
// Next set of transfers
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "[new transfer]");
curl_setopt($ch, CURLOPT_HTTPHEADER, $this_header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
11-Nov-2004 06:51
The parse_response code below does not return multiple cookies as the value of 'Set-Cookie' if multiple cookies are set by the server. It appears to only return the last cookie set.
08-Oct-2004 11:10
Each example of CURLOPT_POSTFIELDS in these comments has missing urlencode() on variable values. This should be applied according to RFC1738
<?php
$vars=null;
foreach($data as $key=>$value)
if($key && $value)
$vars.=$key."=".urlencode($value)."&";
?>
22-Aug-2004 05:51
I changed the parse_response() function given by "brett at brettbrewer dot com", so it now ignores 1xx-responses, which may come before the real response.
<?php
function parse_response($response){
/*
***original code extracted from examples at
***http://www.webreference.com/programming
/php/cookbook/chap11/1/3.html
***returns an array in the following format which varies depending on headers returned
[0] => the HTTP error or response code such as 404
[1] => Array
(
[Server] => Microsoft-IIS/5.0
[Date] => Wed, 28 Apr 2004 23:29:20 GMT
[X-Powered-By] => ASP.NET
[Connection] => close
[Set-Cookie] => COOKIESTUFF
[Expires] => Thu, 01 Dec 1994 16:00:00 GMT
[Content-Type] => text/html
[Content-Length] => 4040
)
[2] => Response body (string)
*/
do
{
list($response_headers,$response) = explode("\r\n\r\n",$response,2);
$response_header_lines = explode("\r\n",$response_headers);
// first line of headers is the HTTP response code
$http_response_line = array_shift($response_header_lines);
if (preg_match('@^HTTP/[0-9]\.[0-9] ([0-9]{3})@',$http_response_line,
$matches)) {
$response_code = $matches[1];
}
else
{
$response_code = "Error";
}
}
while (substr($response_code, 0,1) == "1");
$response_body = $response;
// put the rest of the headers in an array
$response_header_array = array();
foreach ($response_header_lines as $header_line) {
list($header,$value) = explode(': ',$header_line,2);
$response_header_array[$header] = $value;
}
return array($response_code,$response_header_array,$response_body);
}
?>
21-Jul-2004 10:19
Hi!
I have found some information I am sure it could help lot of programmers when they want to connect with curl to any https website and they haven't a good or right CA Cert :)
I give you just one example It has resolved me 2 hours of my time looking for a solution.
It is simple, just if you get any error in the curl_exec (use curl_error(...) to see the error to trace it) add the next line and everything is solved:
(note: replace $ch with the right curl variable)
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
<?php
$ch = curl_init();
$res= curl_setopt ($ch, CURLOPT_URL,"https://yoururl/cgi");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "Idc=si&");
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$xyz = curl_exec ($ch);
curl_close ($ch);
echo $xyz;
if ($xyz == NULL) {
echo "Error:<br>";
echo curl_errno($ch) . " - " . curl_error($ch) . "<br>";
}
?>
I hope this helps.
Raul Mate Galan
Ceo Navenetworks Corp.
Note: Thanks to Ruben Lopez Gea for his help too.
07-May-2004 05:00
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).
29-Apr-2004 04:48
I found it very annoying that cURL did not offer any easy way to extract the response headers. If you do
curl_setopt($ch, CURLOPT_HEADER, 1);
then you only get the headers output inline with the rest of your response. Here's a nice little function to parse the cURL response when you use CURLOPT_HEADER. I took it from an example on another site and turned it into a function. It assumes you're using CURLOPT_RETURNTRANSFER to return the response to some variable that is then passed to the function. You just pass it a curl_exec result and it returns the result parsed into an array with headers broken up into their correct parts.
<?php
function parse_response($response){
/*
***original code extracted from examples at
***http://www.webreference.com/programming
/php/cookbook/chap11/1/3.html
***returns an array in the following format which varies depending on headers returned
[0] => the HTTP error or response code such as 404
[1] => Array
(
[Server] => Microsoft-IIS/5.0
[Date] => Wed, 28 Apr 2004 23:29:20 GMT
[X-Powered-By] => ASP.NET
[Connection] => close
[Set-Cookie] => COOKIESTUFF
[Expires] => Thu, 01 Dec 1994 16:00:00 GMT
[Content-Type] => text/html
[Content-Length] => 4040
)
[2] => Response body (string)
*/
list($response_headers,$response_body) = explode("\r\n\r\n",$response,2);
$response_header_lines = explode("\r\n",$response_headers);
// first line of headers is the HTTP response code
$http_response_line = array_shift($response_header_lines);
if (preg_match('@^HTTP/[0-9]\.[0-9] ([0-9]{3})@',$http_response_line,
$matches)) {
$response_code = $matches[1];
}
// put the rest of the headers in an array
$response_header_array = array();
foreach ($response_header_lines as $header_line) {
list($header,$value) = explode(': ',$header_line,2);
$response_header_array[$header] = $value;
}
return array($response_code,$response_header_array,$response_body);
}
?>
04-Apr-2004 10:20
A bit more documentation (without minimum version numbers):
- CURLOPT_WRITEFUNCTION
- CURLOPT_HEADERFUNCTION
Pass a function which will be called to write data or headers respectively. The callback function prototype:
long write_callback (resource ch, string data)
The ch argument is CURL session handle. The data argument is data received. Note that its size is variable. When writing data, as much data as possible will be returned in all invokes. When writing headers, exactly one complete header line is returned for better parsing.
The function must return number of bytes actually taken care of. If that amount differs from the amount passed to this function, an error will occur.
- CURLOPT_READFUNCTION
Pass a function which will be called to read data. The callback function prototype:
string read_callback (resource ch, resource fd, long length)
The ch argument is CURL session handle. The fd argument is file descriptor passed to CURL by CURLOPT_INFILE option. The length argument is maximum length which can be returned.
The function must return string containing the data which were read. If length of the data is more than maximum length, it will be truncated to maximum length. Returning anything else than a string means an EOF.
[Note: there is more callbacks implemented in current cURL library but they aren't unfortunately implemented in php curl interface yet.]
19-Nov-2003 03:18
The page http://curl.haxx.se/libcurl/c/curl_easy_setopt.html at the cURL site has a list of all the CURLOPTS, including many not mentioned here. Also see http://curl.haxx.se/libcurl/php/examples/ for cURL examples in PHP.
14-May-2003 06:36
Just a reminder: When setting your CURLOPT_POSTFIELDS remember to replace the spaces in your values with %20
24-Feb-2003 10:38
Often times people ask about how to get data from secure servers.
Here is an example of getting the balance page from Paypal
<?
# script by Joel De Gan
# http://listbid.com
function GetCurlPage ($pageSpec) {
$agent = "up.b";
$header[] = "Accept: text/vnd.wap.wml,*.*";
$ch = curl_init($pageSpec);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1); ########### debug
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt ($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, "cook");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cook");
$tmp = curl_exec ($ch);
curl_close ($ch);
return $tmp;
}
function PostCurlPage ($pageSpec, $data) {
$agent = "up.b";
$header[] = "Accept: text/vnd.wap.wml,*.*";
$ch = curl_init($pageSpec);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1); ########### debug
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt ($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, "cook");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cook");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$tmp = curl_exec ($ch);
curl_close ($ch);
return $tmp;
}
$first = GetCurlPage("https://www.paypal.com/cgi-bin/phscr?cmd=login");
$exp = explode("=", $first);
$id = explode("\"", $exp[16]);
$second = PostCurlPage("https://www.paypal.com/cgi-bin/phscr?rs=".$id[0], "email=EMAIL@HOST.COM&pass=YOUR_PP_PASS&cmd=login-submit-pass");
echo $second."\n\n";
?>
13-Nov-2002 01:19
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:
curl_setopt ($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
Yann
beware that not all cURLlib constants are supported under php :
e.g. CURLOPT_PROGRESSFUNCTION or CURLOPT_WRITEDATA are not supported.
CURLOPT_WRITEFUNCTION, although undocumented is supported. It takes the name of a user_defined function.
the function should take two arguments (the curl handle, and the inputdata) and return the length of the written data
e.g.
function myPoorProgressFunc($ch,$str){
global $fd;
$len = fwrite($fd,$str);
print("#");
return $len;
}
curl_setopt($ch,CURLOPT_WRITEFUNCTION,"myPoorProgressFunc");
Also be aware that CURLOPT_WRITEFUNCTION does NOT take the CURLOPT_FILE as a parameter!
in curl lib it would take CURLOPT_WRITEDATA but this is not supported by php; that's why I use "global $fd;" in my exemple function.
CURLOPT_HEADERFUNCTION works the same, and is guaranteed to receive complete header lines as input!
Hope this helps
Ivan
09-Oct-2002 12:42
note: if your option comes from a variable eg:
$option = "CURLOPT_RETURNTRANSFER";
$value = "1";
this will fail:
curl_setopt($ch,$option,$value);
because the curl option is a constant.
use the function constant() to get the value:
curl_setopt($ch,constant($option),$value);
13-Sep-2002 11:34
If you set return transfer to 1 and are sending a post form and find that this crashes php try setting follow location to 1 also. I'm not exactly sure why this crashed, but after i used follow it stopped.
curl_setopt ($sess, CURLOPT_FOLLOWLOCATION, '1');
curl_setopt ($sess, CURLOPT_RETURNTRANSFER, '1');
26-Aug-2002 11:31
To make a POST in multipart/form-data mode
this worked for me, the " \n" at the end of the variables was very important on my OS X server.
<?php
$file = "file_to_upload.txt";
$submit_url = "http://www.url_to_upload_to.com/upload_page.php";
$formvars = array("cc"=>"us \n");
$formvars[variable_1] = "bla bla \n";
$formvars[variable_2] = "bla bla \n";
$formvars[variable_3] = "bla bla \n";
$formvars[variable_4] = "bla bla \n";
$formvars[upfile] = "@$file"; // "@" causes cURL to send as file and not string (I believe)
// init curl handle
$ch = curl_init($submit_url);
curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies.txt"); //initiates cookie file if needed
curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies.txt"); // Uses cookies from previous session if exist
curl_setopt($ch, CURLOPT_REFERER, "http://www.last_url_for_referer_logs.com"); //if server needs to think this post came from elsewhere
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); // follow redirects recursively
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $formvars);
// perform post
echo $pnp_result_page = curl_exec($ch);
curl_close ($ch);
?>
16-Apr-2002 05:05
#!/usr/local/bin/php -q
<?php
$id = "xxxx";
$pw = "xxxx";
$LOGINURL = "http://login.daum.net/Mail-bin/login.cgi";
$GETURL = "http://www.daum.net/Mail-bin/mail_list";
$POSTFIELDS = "id=$id&pw=$pw";
function AUTH_SITE_COOKIE_STORE($LOGINURL,$POSTFIELDS)
{
$parseURL = parse_url($LOGINURL);
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, "$parseURL[host].cookie");
curl_setopt($ch, CURLOPT_URL,"$LOGINURL");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "$POSTFIELDS");
ob_start();
curl_exec ($ch);
ob_end_clean();
curl_close ($ch);
return "$parseURL[host].cookie";
}
function AUTH_SITE_GET($GETURL,$cookieFile)
{
$parseURL = parse_url($GETURL);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_COOKIEFILE, "$cookieFile");
curl_setopt($ch, CURLOPT_URL,"$GETURL");
$result = curl_exec ($ch);
curl_close ($ch);
$fp = fopen ("$parseURL[host].html", "w");
fwrite($fp,$result);
fclose ($fp);
return $result;
}
$cookieFile = AUTH_SITE_COOKIE_STORE($LOGINURL,$POSTFIELDS);
echo $result = AUTH_SITE_GET($GETURL,$cookieFile);
?>
03-Apr-2002 11:14
CURLOPT_HTTPHEADER is NOT like the -H command line switch. The command line switch adds or replaces headers (much like the header() line in PHP, but for HTTP clients instead of servers), but the curl extension will eliminate the headers cURL sends by default.
For instance, your Authorization, Host, Referer, Pragma, and Accept headers which are normally written by default or by other CURLOPT_*'s.
Also, it might seem intuitive that this should accept an array hash of header->values, but this is not the case. It accepts an array of strings of the format "Header: Value", much like the -H command-line switch.
Hope this helps,
terry
01-Mar-2002 04:18
It's possible to take advantage of multiple URLs on the same host in one curl_exec transaction ... just use multiple instances of CURLOPT_URL.
Example:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://foo.com/a.html");
curl_setopt($ch, CURLOPT_URL, "http://foo.com/b.html");
curl_setopt($ch, CURLOPT_URL, "http://foo.com/c.html");
curl_exec($ch);
curl_close($ch);
... the URLs appear to be hit in the same order they are entered. This takes advantage of cURL's Persistant Connection capability if all the URLs are on the same host!
18-Dec-2001 10:35
To collect cookies recieved with a request, set CURLOPT_COOKIEJAR "cookieFileName". Then use CURLOPT_COOKIEFILE "cookieFileName" to recall them in subsequent transactions.
To make a POST in multipart/form-data mode
(to upload a file for example) you can use
curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
where $post is an array :
$post['key1'] = 'data1'
// like a text field in a POST
$post['file1'] = '@filename1'
// upload filename1
For more informations see the
curl_formparse man page.
14-May-2001 11:00
CURLOPT_RETURNTRANSFER has the interesting behaviour of tacking a null char onto the end of the string. This null char is actually on the end of the php string, and can cause some odd results if you're not expecting it to be there.
22-Feb-2001 02:50
If you want to connect to a secure server for posting info/reading info, you need to make cURL with the openSSL options. Then the sequence is nearly identical to the previous example (except http_S_://, and possibly add the useragent):
curl_setopt($ch, CURLOPT_URL,"https://secure.website.com");
//some sites only accept your request if your browser looks legit, so send a useragent profile...
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
07-Feb-2001 04:09
If you'd like to include extra headers in your POST request, you can accomplish this by setting the following option:
CURLOPT_HTTPHEADER
This is similar to the CURL -H command line switch.
Thanks to Daniel Stenberg for pointing out this usefull feature!
Note: this option was first supported in PHP version 4.03 .
25-Jan-2001 08:33
Just because the docs are rather sparse on this, to set multiple values in a cookie, you separate them with a semicolon, as usual. An example, yo set j to j and k to k:
curl_setopt($ch,CURLOPT_COOKIE,"j=j;k=k");
-- Alex