PHP: CURL, Client URL Library 関数 - Manual
PHP  
downloads | documentation | faq | getting help | mailing lists | | php.net sites | links | my php.net 
search for in the  
<crack_opendictcurl_close>
view the version of this page
Last updated: Tue, 21 Dec 2004

XIV. CURL, Client URL Library 関数

導入

PHP は、Daniel Stenbergにより開発されたライブラリlibcurl をサポー トします。このライブラリにより、多くの異なったプロトコルで様々なサー バーと接続し、通信することが可能になります。libcurl は現在 http, https, ftp, gopher, telnet, dict, file, ldap プロトコルをサ ポートしてします。libcurl は HTTPS 認証、HTTP POST、HTTP PUT、FTP アップロード(これはPHPのftp拡張機能でも実行可能です)、HTTPフォーム によるアップロード、プロキシ、クッキー、ユーザ名+パスワードによる 認証もサポートします。

これらの関数は、PHP 4.0.2で追加されました。

要件

CURL関数を使用するためには、 パッケージをインストールしておく必要があります。PHPは、 CURL 7.0.2-beta 以降を必要とします。7.0.2-beta以前のバージョンで は動作しません。PHP バージョン4.2.3以降、少なくともCURLバージョン 7.9.0以降が必要となっています。

インストール手順

PHPのCURLサポート機能を使用するには、--with-curl[=DIR] を付けてPHPをコンパイル しておく必要があります。ただし、DIRは、ディレクトリlibおよび includeを有するディレクトリの場所となります。 ディレクトリ"include"には、"curl"という名前のフォルダーがある必要 があり、そのフォルダには、ファイルeash.hおよびcurl.hがある必要があ ります。"libcurl.a"という名前のファイルがディレクトリ"lib"にある必 要があります。PHP 4.3.0以降、urlストリームでCURLを使用するようPHPを 設定するために--with-curlwrappers を指定できます。

Win32ユーザへの注意: このモジュールをWindows環境で使用可能とするには、PHP/Win32バイナリ パッケージのDLLフォルダにあるlibeay32.dll および ssleay32.dllを使用するWindowsマシンのSYSTEM32 フォルダ(例: C:\WINNT\SYSTEM32 または C:\WINDOWS\SYSTEM32)にコピー する必要があります。

定義済みの定数

これらの定数は、この拡張モジュールで定義されており、 この拡張モジュールがPHP内部にコンパイルされているか実行時に動的にロー ドされるかのどちらかの場合のみ使用可能です。

CURLOPT_DNS_USE_GLOBAL_CACHE (integer)

CURLOPT_DNS_CACHE_TIMEOUT (integer)

CURLOPT_FTPSSLAUTH (integer)

Available since PHP 5.1.0

CURLOPT_PORT (integer)

CURLOPT_FILE (integer)

CURLOPT_INFILE (integer)

CURLOPT_INFILESIZE (integer)

CURLOPT_URL (integer)

CURLOPT_PROXY (integer)

CURLOPT_VERBOSE (integer)

CURLOPT_HEADER (integer)

CURLOPT_HTTPHEADER (integer)

CURLOPT_NOPROGRESS (integer)

CURLOPT_NOBODY (integer)

CURLOPT_FAILONERROR (integer)

CURLOPT_UPLOAD (integer)

CURLOPT_POST (integer)

CURLOPT_FTPLISTONLY (integer)

CURLOPT_FTPAPPEND (integer)

CURLOPT_NETRC (integer)

CURLOPT_FOLLOWLOCATION (integer)

CURLOPT_FTPASCII (integer)

CURLOPT_PUT (integer)

CURLOPT_MUTE (integer)

CURLOPT_USERPWD (integer)

CURLOPT_PROXYUSERPWD (integer)

CURLOPT_RANGE (integer)

CURLOPT_TIMEOUT (integer)

CURLOPT_POSTFIELDS (integer)

CURLOPT_REFERER (integer)

CURLOPT_USERAGENT (integer)

CURLOPT_FTPPORT (integer)

CURLOPT_FTP_USE_EPSV (integer)

CURLOPT_LOW_SPEED_LIMIT (integer)

CURLOPT_LOW_SPEED_TIME (integer)

CURLOPT_RESUME_FROM (integer)

CURLOPT_COOKIE (integer)

CURLOPT_SSLCERT (integer)

CURLOPT_SSLCERTPASSWD (integer)

CURLOPT_WRITEHEADER (integer)

CURLOPT_SSL_VERIFYHOST (integer)

CURLOPT_COOKIEFILE (integer)

CURLOPT_SSLVERSION (integer)

CURLOPT_TIMECONDITION (integer)

CURLOPT_TIMEVALUE (integer)

CURLOPT_CUSTOMREQUEST (integer)

CURLOPT_STDERR (integer)

CURLOPT_TRANSFERTEXT (integer)

CURLOPT_RETURNTRANSFER (integer)

CURLOPT_QUOTE (integer)

CURLOPT_POSTQUOTE (integer)

CURLOPT_INTERFACE (integer)

CURLOPT_KRB4LEVEL (integer)

CURLOPT_HTTPPROXYTUNNEL (integer)

CURLOPT_FILETIME (integer)

CURLOPT_WRITEFUNCTION (integer)

CURLOPT_READFUNCTION (integer)

CURLOPT_PASSWDFUNCTION (integer)

CURLOPT_HEADERFUNCTION (integer)

CURLOPT_MAXREDIRS (integer)

CURLOPT_MAXCONNECTS (integer)

CURLOPT_CLOSEPOLICY (integer)

CURLOPT_FRESH_CONNECT (integer)

CURLOPT_FORBID_REUSE (integer)

CURLOPT_RANDOM_FILE (integer)

CURLOPT_EGDSOCKET (integer)

CURLOPT_CONNECTTIMEOUT (integer)

CURLOPT_SSL_VERIFYPEER (integer)

CURLOPT_CAINFO (integer)

CURLOPT_CAPATH (integer)

CURLOPT_COOKIEJAR (integer)

CURLOPT_SSL_CIPHER_LIST (integer)

CURLOPT_BINARYTRANSFER (integer)

CURLOPT_NOSIGNAL (integer)

CURLOPT_PROXYTYPE (integer)

CURLOPT_BUFFERSIZE (integer)

CURLOPT_HTTPGET (integer)

CURLOPT_HTTP_VERSION (integer)

CURLOPT_SSLKEY (integer)

CURLOPT_SSLKEYTYPE (integer)

CURLOPT_SSLKEYPASSWD (integer)

CURLOPT_SSLENGINE (integer)

CURLOPT_SSLENGINE_DEFAULT (integer)

CURLOPT_SSLCERTTYPE (integer)

CURLOPT_CRLF (integer)

CURLOPT_ENCODING (integer)

CURLOPT_PROXYPORT (integer)

CURLOPT_UNRESTRICTED_AUTH (integer)

CURLOPT_FTP_USE_EPRT (integer)

CURLOPT_HTTP200ALIASES (integer)

CURLOPT_HTTPAUTH (integer)

CURLAUTH_BASIC (integer)

CURLAUTH_DIGEST (integer)

CURLAUTH_GSSNEGOTIATE (integer)

CURLAUTH_NTLM (integer)

CURLAUTH_ANY (integer)

CURLAUTH_ANYSAFE (integer)

CURLOPT_PROXYAUTH (integer)

CURLCLOSEPOLICY_LEAST_RECENTLY_USED (integer)

CURLCLOSEPOLICY_LEAST_TRAFFIC (integer)

CURLCLOSEPOLICY_SLOWEST (integer)

CURLCLOSEPOLICY_CALLBACK (integer)

CURLCLOSEPOLICY_OLDEST (integer)

CURLINFO_EFFECTIVE_URL (integer)

CURLINFO_HTTP_CODE (integer)

CURLINFO_HEADER_SIZE (integer)

CURLINFO_REQUEST_SIZE (integer)

CURLINFO_TOTAL_TIME (integer)

CURLINFO_NAMELOOKUP_TIME (integer)

CURLINFO_CONNECT_TIME (integer)

CURLINFO_PRETRANSFER_TIME (integer)

CURLINFO_SIZE_UPLOAD (integer)

CURLINFO_SIZE_DOWNLOAD (integer)

CURLINFO_SPEED_DOWNLOAD (integer)

CURLINFO_SPEED_UPLOAD (integer)

CURLINFO_FILETIME (integer)

CURLINFO_SSL_VERIFYRESULT (integer)

CURLINFO_CONTENT_LENGTH_DOWNLOAD (integer)

CURLINFO_CONTENT_LENGTH_UPLOAD (integer)

CURLINFO_STARTTRANSFER_TIME (integer)

CURLINFO_CONTENT_TYPE (integer)

CURLINFO_REDIRECT_TIME (integer)

CURLINFO_REDIRECT_COUNT (integer)

CURL_VERSION_IPV6 (integer)

CURL_VERSION_KERBEROS4 (integer)

CURL_VERSION_SSL (integer)

CURL_VERSION_LIBZ (integer)

CURLVERSION_NOW (integer)

CURLE_OK (integer)

CURLE_UNSUPPORTED_PROTOCOL (integer)

CURLE_FAILED_INIT (integer)

CURLE_URL_MALFORMAT (integer)

CURLE_URL_MALFORMAT_USER (integer)

CURLE_COULDNT_RESOLVE_PROXY (integer)

CURLE_COULDNT_RESOLVE_HOST (integer)

CURLE_COULDNT_CONNECT (integer)

CURLE_FTP_WEIRD_SERVER_REPLY (integer)

CURLE_FTP_ACCESS_DENIED (integer)

CURLE_FTP_USER_PASSWORD_INCORRECT (integer)

CURLE_FTP_WEIRD_PASS_REPLY (integer)

CURLE_FTP_WEIRD_USER_REPLY (integer)

CURLE_FTP_WEIRD_PASV_REPLY (integer)

CURLE_FTP_WEIRD_227_FORMAT (integer)

CURLE_FTP_CANT_GET_HOST (integer)

CURLE_FTP_CANT_RECONNECT (integer)

CURLE_FTP_COULDNT_SET_BINARY (integer)

CURLE_PARTIAL_FILE (integer)

CURLE_FTP_COULDNT_RETR_FILE (integer)

CURLE_FTP_WRITE_ERROR (integer)

CURLE_FTP_QUOTE_ERROR (integer)

CURLE_HTTP_NOT_FOUND (integer)

CURLE_WRITE_ERROR (integer)

CURLE_MALFORMAT_USER (integer)

CURLE_FTP_COULDNT_STOR_FILE (integer)

CURLE_READ_ERROR (integer)

CURLE_OUT_OF_MEMORY (integer)

CURLE_OPERATION_TIMEOUTED (integer)

CURLE_FTP_COULDNT_SET_ASCII (integer)

CURLE_FTP_PORT_FAILED (integer)

CURLE_FTP_COULDNT_USE_REST (integer)

CURLE_FTP_COULDNT_GET_SIZE (integer)

CURLE_HTTP_RANGE_ERROR (integer)

CURLE_HTTP_POST_ERROR (integer)

CURLE_SSL_CONNECT_ERROR (integer)

CURLE_FTP_BAD_DOWNLOAD_RESUME (integer)

CURLE_FILE_COULDNT_READ_FILE (integer)

CURLE_LDAP_CANNOT_BIND (integer)

CURLE_LDAP_SEARCH_FAILED (integer)

CURLE_LIBRARY_NOT_FOUND (integer)

CURLE_FUNCTION_NOT_FOUND (integer)

CURLE_ABORTED_BY_CALLBACK (integer)

CURLE_BAD_FUNCTION_ARGUMENT (integer)

CURLE_BAD_CALLING_ORDER (integer)

CURLE_HTTP_PORT_FAILED (integer)

CURLE_BAD_PASSWORD_ENTERED (integer)

CURLE_TOO_MANY_REDIRECTS (integer)

CURLE_UNKNOWN_TELNET_OPTION (integer)

CURLE_TELNET_OPTION_SYNTAX (integer)

CURLE_OBSOLETE (integer)

CURLE_SSL_PEER_CERTIFICATE (integer)

CURLE_GOT_NOTHING (integer)

CURLE_SSL_ENGINE_NOTFOUND (integer)

CURLE_SSL_ENGINE_SETFAILED (integer)

CURLE_SEND_ERROR (integer)

CURLE_RECV_ERROR (integer)

CURLE_SHARE_IN_USE (integer)

CURLE_SSL_CERTPROBLEM (integer)

CURLE_SSL_CIPHER (integer)

CURLE_SSL_CACERT (integer)

CURLE_BAD_CONTENT_ENCODING (integer)

CURLE_LDAP_INVALID_URL (integer)

CURLE_FILESIZE_EXCEEDED (integer)

CURLE_FTP_SSL_FAILED (integer)

CURLFTPAUTH_DEFAULT (integer)

Available since PHP 5.1.0

CURLFTPAUTH_SSL (integer)

Available since PHP 5.1.0

CURLFTPAUTH_TLS (integer)

Available since PHP 5.1.0

CURLPROXY_HTTP (integer)

CURLPROXY_SOCKS5 (integer)

CURL_NETRC_OPTIONAL (integer)

CURL_NETRC_IGNORED (integer)

CURL_NETRC_REQUIRED (integer)

CURL_HTTP_VERSION_NONE (integer)

CURL_HTTP_VERSION_1_0 (integer)

CURL_HTTP_VERSION_1_1 (integer)

CURLM_CALL_MULTI_PERFORM (integer)

CURLM_OK (integer)

CURLM_BAD_HANDLE (integer)

CURLM_BAD_EASY_HANDLE (integer)

CURLM_OUT_OF_MEMORY (integer)

CURLM_INTERNAL_ERROR (integer)

CURLMSG_DONE (integer)

PHPをCURLサポート機能付きでコンパイルした場合、curl関数を使用可能 となります。CURL関数の基本的な使用法は、 curl_init()によりCURLセッションを初期化し、 続いてcurl_exec()により転送を実行し、 curl_close()によりセッションを終了するというも のになります。CURL関数を使用してPHPホームページをファイルに取得す る例を示します。

例 1. PHPのCURLモジュールを使用してexample.comのホームページを取得する

<?php

$ch
= curl_init ("http://www.php.net/");
$fp = fopen ("php_homepage.txt", "w");

curl_setopt ($ch, CURLOPT_FILE, $fp);
curl_setopt ($ch, CURLOPT_HEADER, 0);

curl_exec ($ch);
curl_close ($ch);
fclose ($fp);
?>

目次
curl_close -- CURLセッションを閉じる
curl_copy_handle --  Copy a cURL handle along with all of it's preferences
curl_errno -- 直近のエラー番号を意味する整数を返す
curl_error --  カレントのセッションに関して直近のエラー文字列を返す
curl_exec -- CURLセッションを実行する
curl_getinfo --  指定した伝送に関する情報を得る
curl_init -- CURLセッションを初期化
curl_multi_add_handle --  Add a normal cURL handle to a cURL multi handle
curl_multi_close --  Close a set of cURL handles
curl_multi_exec --  Run the sub-connections of the current cURL handle
curl_multi_getcontent --  Return the content of a cURL handle if CURLOPT_RETURNTRANSFER is set
curl_multi_info_read --  Get information about the current transfers
curl_multi_init --  Returns a new cURL multi handle
curl_multi_remove_handle --  Remove a multi handle from a set of cURL handles
curl_multi_select --  Get all the sockets associated with the cURL extension, which can then be "selected"
curl_setopt -- CURL転送用オプションを設定する
curl_version -- カレントのCURLのバージョンを返す


add a note add a note User Contributed Notes
CURL, Client URL Library 関数
lukas at MORESPAM dot jhu dot edu
05-Mar-2005 03:29
Here's a function that I needed and couldn't use CURL... it helps you if you want to send the POST data (instead of GET) from one form to 2 or more other PHP scripts.  Trust me... it's a life saver!!

     function HTTP_Post($URL,$data, $referrer="") {

       // parsing the given URL
       $URL_Info=parse_url($URL);

       // Building referrer
       if($referrer=="") // if not given use this script as referrer
         $referrer=$_SERVER["SCRIPT_URI"];

       // making string from $data
       foreach($data as $key=>$value)
         $values[]="$key=".urlencode($value);
       $data_string=implode("&",$values);

       // Find out which port is needed - if not given use standard (=80)
       if(!isset($URL_Info["port"]))
         $URL_Info["port"]=80;

       // building POST-request:
       $request.="POST ".$URL_Info["path"]." HTTP/1.1\n";
       $request.="Host: ".$URL_Info["host"]."\n";
       $request.="Referer: $referer\n";
       $request.="Content-type: application/x-www-form-urlencoded\n";
       $request.="Content-length: ".strlen($data_string)."\n";
       $request.="Connection: close\n";
       $request.="\n";
       $request.=$data_string."\n";

       $fp = fsockopen($URL_Info["host"],$URL_Info["port"]);
       fputs($fp, $request);
       while(!feof($fp)) {
           $result .= fgets($fp, 128);
       }
       fclose($fp);

       return $result;
     }

     $output1=HTTP_Post("",$_POST);
     $output2=HTTP_Post("",$_POST);
christian at webaxis dot com dot au
04-Mar-2005 02:38
I use CURL to get history of e-gold payment.

function get_history($egold, $pass, $batch, $counter, $s_date,
$s_month, $s_year, $e_date, $e_month, $e_year, $type){

   $defined_vars = get_defined_vars();

   $_url = 'https://www.e-gold.com/acct/historycsv.asp';

   $params =    "AccountID=$egold&PassPhrase=$pass&" .
       "startmonth=".$s_month."&startday=".
$s_date."&startyear=".$s_year."&" .
       "endmonth=".$e_month."&endday=".
$e_date."&endyear=".$e_year."&";

if($type == "0"){
   $params .=    "paymentsmade=1&";
}
if($type == "1"){
   $params .=    "paymentsreceived=1&";
}

   if(!empty($batch)){
   $params .=    "batchfilter=$batch&";
   }

   if(!empty($counter)){
   $params .=    "counterfilter=$counter&";
   }

   $params .=    "metalfilter=1&";

   $ch = curl_init();

   curl_setopt($ch, CURLOPT_POST,1);
   curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
   curl_setopt($ch, CURLOPT_URL,$_url);
   curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
   curl_setopt($ch, CURLOPT_USERAGENT, $defined_vars['HTTP_USER_AGENT']);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
   $content = curl_exec ($ch);

   curl_close ($ch);

   return $content;

}
Sebastian [Jegeras {snabel-a} gmail.com]
27-Jan-2005 08:51
I did this script to login to a community X number of times

<?php

$url
= [the script that handle logins];

if (empty(
$_GET[�u�]) && empty($_GET[�p�]) && empty($_GET[�x�])){
       echo
"<FORM NAME=�make� METHOD=�get� ACTION=�".$_SERVER[�PHP_SELF�]."�>
   "
.$url."?u=�<INPUT TYPE=�text� NAME=�u� value=�nickname�>�
   &p=�<INPUT TYPE=�text� NAME=�p� value=�password�>�
   &x=�<INPUT TYPE=�text� NAME=�x� value=�times�>�
   <input type=�submit�>"
;
}else{   
   if(!
intval($_GET[�x�])) $_GET[�x�] = 10;
  
$time=time();
  
$params = "username=".$_GET[�u�]."&userpass=".urldecode($_GET[�p�]);
  
$count = login($url,$params);
   echo
$count."/".$_GET[�x�]." logins tog ".(time() - $time)." sekunder";
}

//
function login($url,$params){   
  
$user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
  
  
$ch = curl_init();
  
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  
curl_setopt ($ch, CURLOPT_HEADER, 0);
  
curl_setopt($ch, CURLOPT_POST,1);
  
curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
  
curl_setopt($ch, CURLOPT_URL,$url);
  
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
  
  
$cnt=0;
   for(
$i=0;$i<($_GET[�x�]);$i++){
       if(
curl_exec ($ch))$cnt++;
   }
  
curl_close ($ch);
   return
$cnt;
}
?>
php dot net at lunchclub dot com
12-Jan-2005 08:34
Additional PHP curl documenation is at:
josh,endquote,com
24-Dec-2004 04:58
If you're trying to post forms to an ASP.NET server, you will need to include the value of the __VIEWSTATE hidden field in your post. You can get it like this:

$html = curl_exec($ch);
$matches = array();
preg_match('/<input type="hidden" name="__VIEWSTATE" value="([^"]*?)" \/>/', $html, $matches);
$viewstate = $matches[1];
$viewstate = urlencode($viewstate);

That last urlencode() is the bit that drove me crazy for a while.
php at cobalthorizons dot com
14-Nov-2004 06:44
btw, CURLOPT_HTTPHEADER expects an array:

$myHeader = array(
   "MIME-Version: 1.0",
   "Content-type: text/html; charset=iso-8859-1",
   "Content-transfer-encoding: text",
);
  
curl_setopt($ch, CURLOPT_HTTPHEADER, $myHeader);

(hope this saves someone several hours of...)
mikeb[at]xamo[dot]com
14-Oct-2004 08:11
A note of warning for PHP 5 users: if you try to fetch the CURLINFO_CONTENT_TYPE using curl_getinfo when there is a connect error, you will core dump PHP. I have informed the Curl team about this, so it will hopefully be fixed soon. Just make sure you check for an error before you look for this data.
alidrus at langkah dot com
14-Sep-2004 09:31
In recent versions of php, CURLOPT_MUTE has (probably) been deprecated. Any attempt of using curl_setopt() to set CURLOPT_MUTE will give you a warning like this:

PHP Notice:  Use of undefined constant CURLOPT_MUTE - assumed 'CURLOPT_MUTE' in ....

If you wish tu silence the curl output, use the following instead:

   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

And then,

   $curl_output=curl_exec($ch);

The output of the curl operation will be stored as a string in $curl_output while the operation remains totally silent.
gordon_e_rouse at yahoo dot com dot au
09-Sep-2004 03:08
Multipart form uploads (ie simulating the upload of files from a browser form) is surprisingly easy, yet hard to find information on.... hope this changes that.

$postData = array();

//simulates <input type="file" name="file_name">
$postData[ 'file_name' ] = "@test.txt";
$postData[ 'submit' ] = "UPLOAD";
      
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url );       
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST, 1 );

//seems no need to tell it enctype='multipart/data' it already knows
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData );

$response = curl_exec( $ch );

//where test.txt is a file in the same directory!
rich @ richud dot com
11-Jun-2004 02:12
If you have upgraded to using thread safe PHP (with apache 2 MPM=worker) note that
CURLOPT_COOKIEJAR / CURLOPT_COOKIEFILE both need an absolute path set for the cookie file location and no longer take a relative path.
(As before, also remember to have set correct permissions to allow a writeable cookie file/dir by apache)
[php 4.3.7/apache v2.0.49]
simon [at] vhostdirect [dot] co [dot] uk
04-Mar-2004 12:53
It took me quite some to to figure out how to get Curl (with SSL), OpenSSL and PHP to play nicely together.

After reinstalling MS-VC7 and compiling OpenSSL to finally realise this was'nt nesscary.

If your like me and like *Nix systems more than Windows then you'll most probly have similar problems.

I came across this, on a simple google with the right keywords.



I read thru that and found my mistake.

Its just a small list of notes, I found them to be the best I've found on the subject and the most simplist.

Dont forget to add a simple line like this into your scripts to get them working on Win32.

<?php
if($WINDIR) curl_setopt($curl, CURLOPT_CAINFO, "c:\\windows\\ca-bundle.crt");
?>

Last note: ca-bundle.crt file is located in the Curl download. I stored mine in the windows directory and apache/php can access it fine.

All the best and I hope this helps.

Simon Lightfoot
vHost Direct Limited
hraefn at hraefn dot net
03-Feb-2004 09:15
You can request (and have deflated for you) compressed http (from mod_gzip or ob_gzhandler, for instance) by using the following:

curl_setopt($ch,CURLOPT_ENCODING , "gzip");

This seems to work fine with the latest php (4.3.4) curl (7.11.0) and zlib (1.1.4) under linux.  This is much more elegant than forcing the Accept-Encode header and using gzdeflate on an edited result.
mmm at turkmenweb dot com
15-Dec-2003 12:49
I know that many people suffer from cURL when trying to send XML data. I solved this problem and want to share it with you:

I was trying to send SMS messages with a SMS service provider. They gave me the following ASP code, where you send data as XML and receive a response in XML as well:

url = "";
xmldata ="<XMLtag1>many other tags, data here</XMLtag1>";
Set ob = Server.CreateObject("Msxml2.XMLHTTP")
ob.Open "POST",url,false
ob.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
ob.Send xmldata
...then recieve code was here...

This is done in PHP using cURL library in this way:

<?php
$XPost
= "<XMLcontent>sameas above</XMLcontent>"
$url = "..same URL as above..";
$ch = curl_init();    // initialize curl handle
curl_setopt($ch, CURLOPT_URL,$url); // set url to post to
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($ch, CURLOPT_TIMEOUT, 4); // times out after 4s
curl_setopt($ch, CURLOPT_POSTFIELDS, $XPost); // add POST fields
$result = curl_exec($ch); // run the whole process
echo $result; //contains response from server
?>

Some say that you should use CURLOPT_CUSTOMREQUEST, CURLOPT_HTTPHEADER and etc. to perform this task , as you can see you don't need those at all.

Hope it will help those who tries to simulate Msxml2.XMLHTTP or simply try sending XML data over PHP.

Regards,
Muhammed Mamedov
rodneywa at yahoo dot com
24-Oct-2003 08:47
Here's how I was able to post arrays using Curl:

$array_of_vars[]="var1";
$array_of_vars[]="var2";
$array_of_vars[]="var3";
$array_of_vars[]="var4";

$submit_url = "";
$formvars["feild1"] = "feild1";

for ($i=0;$i<sizeof($array_of_vars);$i++)
   $formvars["array_of_vars[$i]"] = $dna[$i];

  
// init curl handle
$ch = curl_init($submit_url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $formvars);

// perform post
$rr=curl_exec($ch);
echo $rr;
curl_close($ch);
da at canaan dot co dot il
08-Oct-2003 02:42
i had problems with receiving all of the data that was replied to me about a transaction, atlast after way too much time spent i found out that all i missed was:
curl_setopt($ch, CURLOPT_HEADER, 1);

hope it helped anyone
alwong at 123infosys dot com
21-Sep-2003 02:35
The following scripts show how to post form to a web server via https.  I modified rodrigo posting because it did not work for me on IIS5.
<?php
   $url
= 'https://www.yourserver.com/yourrequest;
   $params = "name=your_name&[email protected]";
   $user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";

   $ch = curl_init();
   curl_setopt($ch, CURLOPT_POST,1);
   curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
   curl_setopt($ch, CURLOPT_URL,$url);
   curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
   curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);  // this line makes it work under https

   $result=curl_exec ($ch);
   curl_close ($ch);

   echo("Results: <br>".$result);
?>
rodrigo dot nakahodo at dhweb dot com dot br
18-Sep-2003 07:57
The script shows how to get a simple response of a SSL Server. Enjoy it!

<?php

   $defined_vars
= get_defined_vars();

  
$_url = 'https://www.example.com.br';
  
$_VAR001 = 'nono';
  
$_VAR002 = 'nonono';

  
$params = "VAR001=$_VAR001&VAR002=$_VAR002&";

  
$ch = curl_init();
  
curl_setopt($ch, CURLOPT_POST,1);
  
curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
  
curl_setopt($ch, CURLOPT_URL,$_url);
  
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST2);
  
curl_setopt($ch, CURLOPT_USERAGENT, $defined_vars['HTTP_USER_AGENT']);
  
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  
$result=curl_exec ($ch);
  
curl_close ($ch);

   echo(
"Output: ".$result);

?>
helvecio_oliveira at yahoo dot com dot br
21-Aug-2003 04:12
CURL install steps in Mandrake 9.1:
==========================================================
cp -r /usr/src/php-devel/extensions/curl /tmp/curl
cd /tmp/curl
phpize
./configure
make install
echo "extension = curl.so" > /etc/php/90_curl.ini

Restart apache web server.
==========================================================
curl.so is in:
/usr/lib/php/extensions/

look in phpinfo, the string:
/etc/php/90_curl.ini

Needs these  tools:
autoconf
automake
libtool
m4
php430-devel-430-11mdk.rpm

Some docs:
/usr/share/doc/php430-devel-430/SELF-CONTAINED-EXTENSIONS
eric at imap dot ch
07-Jul-2003 04:38
I managed to use curl to retrieve information from severs on ports other than 80 or 443 (for https) on some installations but not on all.
If you get an "CURLE_COULDNT_CONNECT /* 7 */" error, try adding the port : (for example)
curl_setopt($ch, CURLOPT_PORT, $_SERVER['SERVER_PORT']);
php at mechintosh dot com
03-Jul-2003 03:51
IMHO the example code is somewhat confusing; the fopen() and fclose() are not needed at all, AFAICT.

By default the resulting data/page of the external server is just printed out. To assign the result of the curl session to a variable, one can use
- output buffering (see ob_start(), ob_get_contents())
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
With the latter, the result of curl_exec is the actual data instead of a boolean.
sorin
09-Apr-2003 08:44
function curl_string ($url,$user_agent,$proxy){

       $ch = curl_init();
       curl_setopt ($ch, CURLOPT_PROXY, $proxy);
       curl_setopt ($ch, CURLOPT_URL, $url);
       curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
       curl_setopt ($ch, CURLOPT_COOKIEJAR, "c:\cookie.txt");
       curl_setopt ($ch, CURLOPT_HEADER, 1);
       curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
       curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
       curl_setopt ($ch, CURLOPT_TIMEOUT, 120);
       $result = curl_exec ($ch);
       curl_close($ch);
       return $result;
  
}

$url_page = "";
$user_agent = "Mozilla/4.0";
$proxy = "";
$string = curl_string($url_page,$user_agent,$proxy);
echo $string;

this could hep someone
Sorin
thomas at NOSPAM dot gutschke dot com
07-Apr-2003 12:00
Here's a little code snippet if you need the last-modified-time for a remote file as filemtime() does not work on those. :)

$modified = "";
function read_header($ch, $header)
{
   global $modified;
   $length = strlen($header);
   if(strstr($header, "Last-Modified:"))
   {
       $modified = substr($header, 15);
   }
   return $length;
}

function last_mod($remote_file)
{
   global $modified;
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $remote_file);
   curl_setopt($ch, CURLOPT_HEADER, 1);
   curl_setopt($ch, CURLOPT_NOBODY, 1);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');

   $headers = curl_exec ($ch);
   curl_close ($ch);
   return $modified;
}

use something like
   echo strtotime(last_mod(""));
to get the time as an Unix timestamp or
   echo last_mod("");
to get the gmdate()-like time.
dweingart at pobox dot com
02-Apr-2003 11:08
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:

curl_setopt($ch, CURLOPT_COOKIEJAR, '-');

'-' means stdout

-dw
diana dot castillo at nvtechnologies dot com
25-Mar-2003 06:30
if you need to install curl on a windows system, this has the best explanation:
bharris at spro dot net
26-Feb-2003 12:58
For Win2000: To get the 4.3.1 curl dll to work with https you now need to download the latest win32 curl library from and snag the ca-bundle.crt file from the lib directory.  Place this somewhere handy on your webserver.

Then in your PHP script, add the following setopt line to the rest of your curl_setopt commands:

curl_setopt($ch, CURLOPT_CAFILE, 'C:\pathto\ca-bundle.crt')

This worked for me and allowed me to discontinue using the CURLOPT_SSL_VERIFYPEER set to zero hack.
retro at nospam dot enx dot org
12-Jan-2003 01:42
When using cURL to do posts, make sure you actually specify the CURLOPT_POSTFIELDS option. Not doing so seems to make PHP core (and apache as well if you're running it through there).
asi at neo dot ee
13-Dec-2002 03:28
If anybody has problems with getting new curl working with older version of php then ...

I got stuck with installing curl-7.10.2+php 4.0.6 or better said configuring PHP with mentioned version of curl.

After some hopeless attempts to configure curl support into PHP I saw an error message:

"checking for cURL greater than or equal to 7.8... ./configure: line 11725: test: 070a02: integer expression expected"

after which configure said: "configure: error: cURL version 7.8 or later is required to compile php with cURL support"

On the mentioned line "test" is used to compare verion from curl-sonfig with preset value 70800 but ... test expects both values to be integer. From verson 7.8 curl outputs its version in hex...

I did'nt had the time to think about fixing this - I just deleted lines responsible and got curl working :)
jerome at blaster dot dyndns dot org
10-Oct-2002 02:23
I wanted to import an https Web pages thanks to fopen("https://xxx", "r") or fsockopen but as far as I read, it was only possible with cURL extension (to get these functions worked with https, we have to wait the PHP 4.3 release, TBC) , so I installed it and it worked. It is quite easy, but I spend to much time on it:
First of all, here is the most important: WE DON'T NEED to compile anything to get curl worked. All the extensions that we need are already in the PHP 4.2.3 zip. So here are the instructions and links to get Apache + PHP + CURL work properly:

Downloads:
=> Apache Web server with Openssl included:
   Apache_1.3.26-Mod_SSL_2.8.9-OpenSSL_0.9.6d-WIN32.zip

=> PHP 4.2.3 for Windows:
  

=> Optional: Openssl zip to get libeay32.dll and ssleay32.dll (I didn't need them because I used those stored in PHP zip)
OpenSSL-0.9.6d-win32_RAR3_Archive_.rar

Installs:
=> Unzip Apache in Program Files folder(there should be no problem with the blank in the path):

=> Follow those instructions: . I didn't exactly do what they wrote but it helped a lot. I created the test certificat thanks to this link in order to test my Apache server with SSL.

=> Configure httpd (easy to find on the Web) and test Apache before going on, it avoids problems.
   I just changed the Port from 80 (http) to 443 (https) in httpd, I only wanted to perform https.
   I didn't try to get the both working together.

=> Unzip PHP in C:\Php path (don't install it in Program Files folder, you would have troubles because of the blank this time...)

=> Configure PHP : #install.windows.manual
   WARNING: php.ini MUST be stored into the c:\winnt folder and the dlls must be stored into the c:\winnt\system32 folder.

=> Test it with Apache and phpinfo() function.

cURL:
To add cURL extension, YOU DON'T NEED to compile anything.
All the extensions that you need are in the C:\php\extensions folder. To add cURL or any other extensions:

=> Have the libeay32.dll and ssleay32.dll in the system32 folder (already done if you installed PHP)

=> Copy all dlls from php/dll/ folder to winnt/system32/ (libeay32.dll and ssleay32.dll are already there if you followed
   the previous steps)

=> Remove the ";" in front of extension=php_curl.dll in php.ini.

Those instructions come from: #install.windows.extensions

And it should work, just test it with curl functions into your php script.
hamannDOTw at tDOTonline dot de
02-Jul-2002 04:18
Using the customrequest for a complete post is wrong. Libcurl will add a partial url, the http version and the standard headers after the post data - while this works with a non-persistent connection and an apache web server, it may fail under different conditions
smclean at scoreinfo dot tv
22-Jun-2002 01:46
For an exaplanation of those Predefined Constants listed above see the following URL:

 
marco dot mcc at inwind dot it
27-May-2001 06:50
If you want to write your entire HTTP request without use any other CURL functions like CURLOPT_POST specify it within a curl_setopt ($ch,CURLOPT_CUSTOMREQUEST , $req) line;
Where $req looks like (let me imagine a POST request...):

POST /destination/script HTTP/1.1
Content-length: xxx
Content-type: text/xml
host: yourhost
accept: */*
accept-encoding: gzip, deflate
accept-language: en-us
connection: close; Keep-Alive
...
your POST data
...


CURLOPT_CUSTOMREQUEST is not documented?!? but it's useful and amazing!

marcomcc (Roma)
dan dot polansky at seznam dot cz
17-Jan-2001 10:31
I used to download www pages to my script and one of the pages was different in MS explorer and different, when I downloaded it. Namely, information, I was really interested in was missing. That was because the server on the other bank of the river was looking at who is downloading the page. Everything got fixed when I pretended I was MSIE. It is done with curl. Here is a function, that you may use in similar situation

function download_pretending($url,$user_agent) {
   $ch = curl_init();
   curl_setopt ($ch, CURLOPT_URL, $url);
   curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
   curl_setopt ($ch, CURLOPT_HEADER, 0);
   curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
   $result = curl_exec ($ch);
   curl_close ($ch);
   return $result;
}

<crack_opendictcurl_close>
 Last updated: Tue, 21 Dec 2004
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: /
Last updated: Mon Mar 14 08:13:06 2005 Local time zone must be set--see zic manual page