i漂泊

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 4078|回复: 0

PHP API获取天气预报,以及使用飞信API,给好友发

[复制链接]
TA的礼物信息
  • 收到:0
  • 送出:2
发表于 2013-2-23 21:33:48 | 显示全部楼层 |阅读模式
API获取天气预报,以及使用飞信API,给好友发送短信
加上计划任务,就可以持续每天自动发送天气预报给自己和亲人啦。。。
  1. <?php
  2. ##########
  3. # jiam 2012/12/5
  4. # 1113160783@qq.com
  5. #自动发送飞信
  6. #中间涉及到飞信接口
  7. #天气预报API
  8. #以及城市代码
  9. ##########
  10. #101180101是郑州城市代码
  11. #101180301是新乡城市代码
  12. $url='http://m.weather.com.cn/data/101180101.html';
  13. $xinxiang='http://m.weather.com.cn/data/101180301.html';
  14. $w=curl($url);
  15. $weather=json_decode($w);
  16. $weatherinfo=object_to_array($weather);
  17. $info=$weatherinfo['weatherinfo'];
  18. $str=$info['date_y'].','.$info['week'].'。'.$info['city'].'今天气温:'.$info['temp1'].',天气:'.$info['weather1'].'有'.$info['wind1'].',风力:'.$info['fx1'].',穿衣建议:'.$info['index_d'].$info['index48_d'];

  19. $feixin='http://wjima.a173.cnaaa4.com/feixin/str.php';
  20. $postArray['userName']='手机号';//飞信账号
  21. $postArray['password']='密码';//飞信密码
  22. $postArray['content']=$str;//发送内容
  23. $postArray['other']='接收方手机号';//接收方手机号,无此参数,默认给自己发送

  24. $send=curl($feixin,$postArray);

  25. print_r($send);die();

  26. function curl($url, $postFields = null)
  27. {
  28. $ch = curl_init();
  29. curl_setopt($ch, CURLOPT_URL, $url);
  30. curl_setopt($ch, CURLOPT_FAILONERROR, false);
  31. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  32. //https 请求
  33. if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) {
  34. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  35. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  36. }

  37. if (is_array($postFields) && 0 < count($postFields))
  38. {
  39. $postBodyString = "";
  40. $postMultipart = false;
  41. foreach ($postFields as $k => $v)
  42. {
  43. if("@" != substr($v, 0, 1))//判断是不是文件上传
  44. {
  45. $postBodyString .= "$k=" . urlencode($v) . "&";
  46. }
  47. else//文件上传用multipart/form-data,否则用www-form-urlencoded
  48. {
  49. $postMultipart = true;
  50. }
  51. }
  52. unset($k, $v);
  53. curl_setopt($ch, CURLOPT_POST, true);
  54. if ($postMultipart)
  55. {
  56. curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
  57. }
  58. else
  59. {
  60. curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1));
  61. }
  62. }
  63. $reponse = curl_exec($ch);

  64. if (curl_errno($ch))
  65. {
  66. throw new Exception(curl_error($ch),0);
  67. }
  68. else
  69. {
  70. $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  71. if (200 !== $httpStatusCode)
  72. {
  73. throw new Exception($reponse,$httpStatusCode);
  74. }
  75. }
  76. curl_close($ch);
  77. return $reponse;
  78. }
  79. function object_to_array($obj)
  80. {
  81. $_arr = is_object($obj) ? get_object_vars($obj) : $obj;
  82. foreach ($_arr as $key => $val)
  83. {
  84. $val = (is_array($val) || is_object($val)) ? object_to_array($val) : $val;
  85. $arr[$key] = $val;
  86. }
  87. return $arr;
  88. }
  89. ?>
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|手机版|IPiaoBo Inc. ( 渝ICP备17002826号 )

GMT+8, 2024-4-29 09:21 , Processed in 0.064611 second(s), 46 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表