php中Curl函数常用的两个例子,登陆/soap
curl在php中在性能方法要比较其它相同功能函数要高得多,下面我给大家介绍我自己常用的函数一个使用PHP curl 来做登陆后的抓取时使用,一个使用PHP curl来实现soap;我想你会感兴趣.
例子,模拟登陆,代码如下:
-
-
-
-
-
-
-
-
-
- function vcurl($url, $post = '', $cookie = '', $cookiejar = '', $referer = ''){
- $tmpInfo = '';
-
-
- $curl = curl_init();
-
- curl_setopt($curl, CURLOPT_URL, $url);
-
- curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
-
-
- if($referer) {
- curl_setopt($curl, CURLOPT_REFERER, $referer);
- } else {
- curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
- }
-
- if($post) {
-
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
- }
-
- if($cookie) {
- curl_setopt($curl, CURLOPT_COOKIE, $cookie);
- }
-
- if($cookiejar) {
- curl_setopt($curl, CURLOPT_COOKIEJAR, $cookiejar);
- curl_setopt($curl, CURLOPT_COOKIEFILE, $cookiejar);
- }
-
- curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
-
- curl_setopt($curl, CURLOPT_TIMEOUT, 100);
-
- curl_setopt($curl, CURLOPT_HEADER, 0);
-
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
-
- $tmpInfo = curl_exec($curl);
-
- if (curl_errno($curl)) {
- $tmpInfo = '<pre><b>错误:</b><br />'.curl_error($curl);
- }
-
- curl_close($curl);
-
- return $tmpInfo;
- }
例子,soap代码如下:
- function vcurlsoap($url, $SoapRequest, $SoapAction) {
- $ch = curl_init ();
- curl_setopt ( $ch, CURLOPT_URL, $url );
- curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
- curl_setopt ( $ch, CURLOPT_HEADER, 0 );
- curl_setopt ( $ch, CURLOPT_POST, 1 );
- curl_setopt ( $ch, CURLOPT_POSTFIELDS, $SoapRequest );
- curl_setopt ( $ch, CURLOPT_TIMEOUT, 60 );
- curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
- $header = array ("Content-Type: text/xml" );
- $header [] = "Content-Length: ".strlen($SoapRequest);
- if (! is_null ( $SoapAction ))
- $header [] = 'SOAPAction: "' . $SoapAction . '"';
- curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header );
- $xmlResponse = curl_exec ( $ch );
- curl_close ( $ch );
- return $xmlResponse;
- }