确认是否安装了CURL扩展

Linux下命令:

[root@fengniu020 ~]# php -i | grep -i curl
Additional .ini files parsed => /etc/php.d/curl.ini,
curl
cURL support => enabled
cURL Information => 7.19.7

curl操作步骤解析:

浅谈PHP中的数据传输CURL

CURL实例

1.一个简单的curl,抓取百度首页

2.下载一个网页并把内容中的“百度”替换为“摆渡”之后输出

3.调用WebService

一个简单的curl,抓取百度首页

<"htmlcode">
<"http://www.baidu.com");    // 设置访问网页的URL
curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, true);      // 执行之后不直接打印出来
$output=curl_exec($curlobj); // 执行
curl_close($curlobj);     // 关闭cURL
echo str_replace("百度","摆渡",$output);
"htmlcode">
<"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName"); 
curl_setopt($curlobj, CURLOPT_HEADER, 0); 
curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curlobj, CURLOPT_POST, 1); //POST方式
curl_setopt($curlobj, CURLOPT_POSTFIELDS, $data); 
curl_setopt($curlobj, CURLOPT_HTTPHEADER, array("application/x-www-form-urlencoded; charset=utf-8", 
  "Content-length: ".strlen($data)
  )); //HTTP请求头
curl_setopt ($curlobj, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
$rtn = curl_exec($curlobj);  
if(!curl_errno($curlobj)){
  // $info = curl_getinfo($curlobj); 
  // print_r($info);
  echo $rtn; 
} else {
 echo 'Curl error: ' . curl_error($curlobj);
}
curl_close($curlobj);
"htmlcode">
<"ftp://192.168.1.100/downloaddemo.txt"); 
curl_setopt($curlobj, CURLOPT_HEADER, 0); 
curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curlobj, CURLOPT_TIMEOUT, 300); // times out after 300s
curl_setopt($curlobj, CURLOPT_USERPWD, "peter.zhou:123456");//FTP用户名:密码
// Sets up the output file
$outfile = fopen('dest.txt', 'wb');//保存到本地的文件名
curl_setopt($curlobj, CURLOPT_FILE, $outfile);

$rtn = curl_exec($curlobj); 
fclose($outfile); 
if(!curl_errno($curlobj)){
  // $info = curl_getinfo($curlobj); 
  // print_r($info);
  echo "RETURN: " . $rtn; 
} else {
 echo 'Curl error: ' . curl_error($curlobj);
}
curl_close($curlobj);
"htmlcode">
<"ftp://192.168.1.100/ftp01_uploaded.php");//上传后保存的文件名
curl_setopt($curlobj, CURLOPT_HEADER, 0); 
curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curlobj, CURLOPT_TIMEOUT, 300); // times out after 300s
curl_setopt($curlobj, CURLOPT_USERPWD, "peter.zhou:123456");//FTP用户名:密码

curl_setopt($curlobj, CURLOPT_UPLOAD, 1);
curl_setopt($curlobj, CURLOPT_INFILE, $fp);//传输打开的文件
curl_setopt($curlobj, CURLOPT_INFILESIZE, filesize($localfile));//上传的文件大小
$rtn = curl_exec($curlobj); 
fclose($fp); 
if(!curl_errno($curlobj)){
  echo "Uploaded successfully."; 
} else {
 echo 'Curl error: ' . curl_error($curlobj);
}
curl_close($curlobj);
"htmlcode">
<"https://ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/jquery.validate.js");    // 设置访问网页的URL
curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, true);      // 执行之后不直接打印出来

// 设置HTTPS支持
date_default_timezone_set('PRC'); // 使用Cookie时,必须先设置时区
curl_setopt($curlobj, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查从证书中检查SSL加密算法是否存在,设置为0就是终止从服务器端进行验证
curl_setopt($curlobj, CURLOPT_SSL_VERIFYHOST, 2); // 

$output=curl_exec($curlobj); // 执行
curl_close($curlobj);     // 关闭cURL
echo $output;
?>

以上这篇浅谈PHP中的数据传输CURL就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com