1.首先是下载PHPMailer
http://code.google.com/a/apache-extras.org/p/phpmailer/
2.解压
从中取出class.phpmailer.php 和 class.smtp.php 放到你的项目的文件夹,因为我们等下会引用到它们.
3.创建发送邮件的函数,其中你需要配置smtp服务器
//Author:Jiucool WebSite: http://www.jiucool.com
//$to 表示收件人地址 $subject 表示邮件标题 $body表示邮件正文
//error_reporting(E_ALL);
error_reporting(E_STRICT);
date_default_timezone_set('Asia/Shanghai');//设定时区东八区
require_once('class.phpmailer.php');
include('class.smtp.php');
$mail = new PHPMailer(); //new一个PHPMailer对象出来
$body = eregi_replace("[\]",'',$body); //对邮件内容进行必要的过滤
$mail->CharSet ="GBK";//设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置,否则乱码
$mail->IsSMTP(); // 设定使用SMTP服务
$mail->SMTPDebug = 1; // 启用SMTP调试功能
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // 启用 SMTP 验证功能
$mail->SMTPSecure = "ssl"; // 安全协议,可以注释掉
$mail->Host = 'stmp.163.com'; // SMTP 服务器
$mail->Port = 25; // SMTP服务器的端口号
$mail->Username = 'wangliang_198x'; // SMTP服务器用户名,PS:我乱打的
$mail->Password = 'password'; // SMTP服务器密码
$mail->SetFrom('xxx@xxx.xxx', 'who');
$mail->AddReplyTo('xxx@xxx.xxx','who');
$mail->Subject = $subject;
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional, comment out and test
$mail->MsgHTML($body);
$address = $to;
$mail->AddAddress($address, '');
//$mail->AddAttachment("images/phpmailer.gif"); // attachment
//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
// echo "Message sent!恭喜,邮件发送成功!";
}
}
4. 使用函数
本文链接
一个用于查询拍拍帐号信息的小程序,初学php写的比较烂。
2 include_once('./simple_html_dom.php');
3
4 $info_detail = array(); //卖家资料
5 $colligation_score = array(); //综合评分
6 $service_status = array(); //店铺近30天服务状况
7 $comp_rate = array(); //投诉率
8 $refund_rate = array(); //退款率
9
10 $is_display_credit = false; //是否显示信用模块
11 $credit_score = ''; //信用分数
12 $reputation_rate = ''; //好评率
13
14 $last_week = array(); //最近一周
15 $last_month = array(); //最近一月
16 $last_six_months = array(); //最近半年
17 $total = array();
18
19 $qq = isset($_POST['qq']) ? $_POST['qq'] : '';
20 if($qq != ''){
21 $info_detail['account'] = $qq;
22 $info_detail['detail link'] = "http://shop.paipai.com/cgi-bin/creditinfo/view?uin=" . $qq . "&flag=1";
23
24 /* $ch = curl_init();
25 curl_setopt($ch, CURLOPT_URL, $info_detail['detail link']);
26 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
27 curl_setopt($ch, CURLOPT_HEADER, 0);
28 $result = curl_exec($ch);
29 curl_close($ch);
30 */
31 $result = file_get_contents($info_detail['detail link']);
32
33 $html = new simple_html_dom();
34 $html->load($result);
35
36 //卖家资料
37 $ele = $html->find('div[);
38 if($ele == null){
39 echo "<script type=\"text/javascript\">location.href='/blog_article/index.html'</script>";
40 }
41
42 $info_detail['nick'] = $ele[0]->next_sibling()->plaintext;
43 $info_detail['area'] = $ele[1]->next_sibling()->plaintext;
44 $info_detail['time'] = $ele[2]->next_sibling()->plaintext;
45
46 //店铺综合评分
47 $colligation_score['score'] = $html->find('div[, 0)->plaintext;
48 $ele = $html->find('div[);
49 $colligation_score['conform'] = $ele[0]->children[1]->children[0]->plaintext;
50 $colligation_score['attitude'] = $ele[1]->children[1]->children[0]->plaintext;
51 $colligation_score['delivery speed'] = $ele[2]->children[1]->children[0]->plaintext;
52
53
54 //店铺近30天服务状况
55 $ele = $html->find('div[);
56 $service_status['backmoney speed'] = $ele[0]->children[1]->children[0]->plaintext;
57 $service_status['backmoney rate'] =
问题:用php读取文件中的json数据,怎么解析都是返回null。
读取文件,使用了file_get_contents函数。
var_dump(json_decode($json));
var_dump(json_decode($json, true));
如果直接在php中读取json字符串,是没有错的,怎么从文件读取就错了呢。
搜索得以下结果:
PHP: json_decode - Manual
http://www.php.net/manual/zh/function.json-decode.php
php使用json_decode返回NULL – 服务器运维与Web架构
http://www.nginx.cn/337.html
php json_decode null - toeasy - 博客园
http://www.cnblogs.com/Toeasy/archive/2012/04/09/2439688.html
json_decode() 得到null-夜色-Yes-PHPChina - Powered by Discuz!
http://bbs.phpchina.com/thread-267593-1-1.html
PHP5中file_get_contents函数获取带BOM的utf-8文件内容时需注意 - wanglianghuaihua的日志 - 网易博客
http://wanglianghuaihua.blog.163.com/blog/static/54251531201091915210555/
关键结果在是后面两个。造成json_decode() 解析null的原因是,json文件是UTF-8格式,带有BOM。
修正后代码如下,即可正常解析。
if(preg_match('/^\xEF\xBB\xBF/',$dmText))
{
$dmText = substr($dmText,3);
}
//trim
$dmText = t($dmText);
echo $dmText;
/* create array list from comments */
$dmList = json_decode($dmText,true); //当该参数为 TRUE 时,将返回 array 而非 object 。
var_dump($dmList);
显示结果:
"success": "true",
"total":"4",
"items": [
{"id":"1","c":"asdaEG","tb": "dm_suppliertype", "cn": "供应商类型","tips":"供应商类型"},
{"id":"2","c":"adsafR","tb": "suppliertype2", "cn": "供应商类型2","tips":"供应商类型"},
{"id":"3","c":"ada222","tb": "suppliertype3", "cn": "供应商类型3","tips":"供应商类型"},
{"id":"4","c":"23jetG","tb": "suppliertype4", "cn": "供应商类型4","tips":"供应商类型"}
]
}array(3) {
["success"]=>
string(4) "true"
["total"]=>
string(1) "4"
["items"]=>
array(4) {
[0]=>
array(5) {
["id"]=>
string(1) "1"
["c"]=>
string(6) "asdaEG"
["tb"]=>
string(15) "dm_suppliertype"
["cn"]=>
string(15) "供应商类型"
["tips"]=>
string(15) "供应商类型"
}
[1]=>
array(5) {
["id"]=>
string(1) "2"
["c"]=>
string(6) "adsafR"
["tb"]=>
string(13) "suppliertype2"
["cn"]=>
string(16) "供应商类型2"
["tips"]=>
string(15) "供应商类型"
}
[2]=>
array(5) {
["id"]=>
string(1) "3"
["c"]=>
string(6) "ada222"
["tb"]=>
string(13) "suppliertype3"
["cn"]=>
string(16) "供应商类型3"
["tips"]=>
string(15) "供应商类型"
}
[3]=>
array(5) {
["id"]=>
string(1) "4"
["c"]=>
string(6) "23jetG"
["tb"]=>
string(13) "suppliertype4"
["cn"]=>
string(16) "供应商类型4"
["tips"]=>
string(15) "供应商类型"
}
}
}
本文链接