php 利用 curl 连接各种服务器
2008-10-17 11:01
288 查看
1.curl定义 curl是一个利用URL语法在命令行方式下工作的文件传输工具。 Client Url, allows you to connect and communicate to many different types of servers with
many different types of protocols. libcurl currently supports the http, https,
ftp, gopher, telnet, dict, file, and ldap protocols. 2.php安装curl 修改php.ini:配置好 extension_dir ,去掉 extension = php_curl.dll 前面的分号。(如果是xampp,则在apache/php.ini,不是php/php.ini文件) 拷贝PHP目录中的libeay32.dll 和 ssleay32.dll 两个文件到 windows/system32 目录。 重新启动Apache。3.示例代码
<?php$PostData = "foo=abc&bar=def"; $url="https://10.3.0.34/a3.php";
//init a curl session$ch = curl_init();// set the urlcurl_setopt($ch, CURLOPT_URL, $url);
// Set your login and password for authentication
curl_setopt($ch, CURLOPT_USERPWD, 'login:pasword');// CURLAUTH_ANY is an alias for CURLAUTH_BASIC | CURLAUTH_DIGEST | CURLAUTH_GSSNEGOTIATE | CURLAUTH_NTLM
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);//?#93;定伺服器憑證,要不要?#93;我忘了... 請自己 try 一下 //curl_setopt($ch , CURLOPT_CAPATH, "/certificate"); //curl_setopt($ch , CURLOPT_CAINFO, "/certificate/server.crt");
curl_setopt($ch, CURLOPT_USERAGENT, 'Opera/9.23 (Windows NT 5.1; U; en)');
//common name and also verify that it matches the hostname provided)curl_setopt($ch , CURLOPT_SSL_VERIFYPEER, false); //這行請參考 http://curl.haxx.se 的介紹 curl_setopt($ch , CURLOPT_SSL_VERIFYHOST, 2);
//不直接顯示回傳結果 ,Return the result instead of printing itcurl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
//post資料給指定網頁 curl_setopt($ch , CURLOPT_POST, 1); curl_setopt($ch , CURLOPT_POSTFIELDS, $PostData);
$Result = curl_exec($ch );
curl_close($ch );
参考:1.http://blog.taragana.com/index.php/archive/how-to-use-curl-in-php-for-authentication-and-ssl-communication/2.http://hi.baidu.com/freshcn/blog/item/88e80024ee436937c9955920.html
many different types of protocols. libcurl currently supports the http, https,
ftp, gopher, telnet, dict, file, and ldap protocols. 2.php安装curl 修改php.ini:配置好 extension_dir ,去掉 extension = php_curl.dll 前面的分号。(如果是xampp,则在apache/php.ini,不是php/php.ini文件) 拷贝PHP目录中的libeay32.dll 和 ssleay32.dll 两个文件到 windows/system32 目录。 重新启动Apache。3.示例代码
<?php$PostData = "foo=abc&bar=def"; $url="https://10.3.0.34/a3.php";
//init a curl session$ch = curl_init();// set the urlcurl_setopt($ch, CURLOPT_URL, $url);
// Set your login and password for authentication
curl_setopt($ch, CURLOPT_USERPWD, 'login:pasword');// CURLAUTH_ANY is an alias for CURLAUTH_BASIC | CURLAUTH_DIGEST | CURLAUTH_GSSNEGOTIATE | CURLAUTH_NTLM
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);//?#93;定伺服器憑證,要不要?#93;我忘了... 請自己 try 一下 //curl_setopt($ch , CURLOPT_CAPATH, "/certificate"); //curl_setopt($ch , CURLOPT_CAINFO, "/certificate/server.crt");
curl_setopt($ch, CURLOPT_USERAGENT, 'Opera/9.23 (Windows NT 5.1; U; en)');
//common name and also verify that it matches the hostname provided)curl_setopt($ch , CURLOPT_SSL_VERIFYPEER, false); //這行請參考 http://curl.haxx.se 的介紹 curl_setopt($ch , CURLOPT_SSL_VERIFYHOST, 2);
//不直接顯示回傳結果 ,Return the result instead of printing itcurl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
//post資料給指定網頁 curl_setopt($ch , CURLOPT_POST, 1); curl_setopt($ch , CURLOPT_POSTFIELDS, $PostData);
$Result = curl_exec($ch );
curl_close($ch );
参考:1.http://blog.taragana.com/index.php/archive/how-to-use-curl-in-php-for-authentication-and-ssl-communication/2.http://hi.baidu.com/freshcn/blog/item/88e80024ee436937c9955920.html
相关文章推荐
- php利用curl上传文件到服务器(支持文件夹创建)
- 小程序通过apache服务器利用php连接mySQL
- PHP memcached 利用远程终端连接、操作memcached服务器
- 利用php做服务器和web前端的界面进行交互
- 利用ssh反向代理以及autossh实现从外网连接内网服务器
- 利用ASP连接各种数据库
- PHP - 利用Curl、socket、file_get_contents POST数据
- php利用header函数下载各种文件
- Android网络客户端编程,HttpGet类和HttpPost类使用详解,连接php-mysql服务器
- php连接mysql服务器速度慢的原因和解决方法
- PHP 利用SMPT服务器发送邮件 无需自己配置服务器
- 利用PHP如何实现Socket服务器
- 利用PHP如何实现Socket服务器
- 【转载】如何利用TortoiseCVS连接sourceforge.net的cvs服务器
- php使用curl发送post连接
- Linux平台使用Freetds连接SQL Server服务器,兼容PHP和Laravel
- [PHP]利用XAMPP搭建本地服务器, 然后利用iOS客户端上传数据到本地服务器中(一.安装XAMPP)
- VB实现自动取得服务器IP并连接服务器(利用UDP广播)
- 测试PHP服务器时配置都已经好了,就是连接不上服务器
- python利用paramiko连接远程服务器执行命令