Home > Backend Development > PHP Tutorial > php curl使用例子

php curl使用例子

WBOY
Release: 2016-06-23 13:37:30
Original
855 people have browsed it

  PHP支持的由Daniel Stenberg创建的libcurl库允许你与各种的服务器使用各种类型的协议进行连接和通讯。libcurl目前支持http、https、ftp、gopher、telnet、dict、file和ldap协议,以下代码是 微信支付中,curl的使用代码		$ch = curl_init();		//超时时间		curl_setopt($ch,CURLOPT_TIMEOUT,$second);		//这里设置代理,如果有的话        //curl_setopt($ch,CURLOPT_PROXY, '8.8.8.8');        //curl_setopt($ch,CURLOPT_PROXYPORT, 8080);        curl_setopt($ch,CURLOPT_URL, $url);        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);		//设置header		curl_setopt($ch,CURLOPT_HEADER,FALSE);		//要求结果为字符串且输出到屏幕上		curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);		//设置证书		//使用证书:cert 与 key 分别属于两个.pem文件		//默认格式为PEM,可以注释		curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');		curl_setopt($ch,CURLOPT_SSLCERT, WxPayConf_pub::SSLCERT_PATH);		//默认格式为PEM,可以注释		curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');		curl_setopt($ch,CURLOPT_SSLKEY, WxPayConf_pub::SSLKEY_PATH);		//post提交方式		curl_setopt($ch,CURLOPT_POST, true);		curl_setopt($ch,CURLOPT_POSTFIELDS,$xml);		$data = curl_exec($ch);		//返回结果		if($data){			curl_close($ch);			return $data;		}		else { 			$error = curl_errno($ch);			echo "curl出错,错误码:$error"."<br>"; 			echo "<a href='http://curl.haxx.se/libcurl/c/libcurl-errors.html'>错误原因查询</a></br>";			curl_close($ch);			return false;          }
Copy after login

  

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template