跟上次的大致相同、新增了每日新闻、国外天气,修改了一部分代码
一、注册易客云
地址:http://www.yiketianqi.com/user/register
复制下面的代码到百度云函数代码编辑页
<span class="token delimiter important"><?php</span>
<span class="token variable">$appId</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'2323232'</span><span class="token punctuation">;</span> <span class="token comment">//对应自己的appId</span>
<span class="token variable">$appSecret</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'343444453'</span><span class="token punctuation">;</span> <span class="token comment">//对应自己的appSecret</span>
<span class="token variable">$wxgzhurl</span> <span class="token operator">=</span> <span class="token string double-quoted-string">"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="</span> <span class="token operator">.</span> <span class="token variable">$appId</span> <span class="token operator">.</span> <span class="token string double-quoted-string">"&secret="</span> <span class="token operator">.</span> <span class="token variable">$appSecret</span><span class="token punctuation">;</span>
<span class="token variable">$access_token_Arr</span> <span class="token operator">=</span> <span class="token function">https_request</span><span class="token punctuation">(</span><span class="token variable">$wxgzhurl</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$access_token</span> <span class="token operator">=</span> <span class="token function">json_decode</span><span class="token punctuation">(</span><span class="token variable">$access_token_Arr</span><span class="token punctuation">,</span> <span class="token constant boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$ACCESS_TOKEN</span> <span class="token operator">=</span> <span class="token variable">$access_token</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'access_token'</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">//ACCESS_TOKEN</span>
<span class="token comment">// 什么时候恋爱的(格式别错)</span>
<span class="token variable">$lovestart</span> <span class="token operator">=</span> <span class="token function">strtotime</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'2012-12-22'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$end</span> <span class="token operator">=</span> <span class="token function">time</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$love</span> <span class="token operator">=</span> <span class="token function">ceil</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token variable">$end</span> <span class="token operator">-</span> <span class="token variable">$lovestart</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">86400</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 下一个生日是哪一天(格式别错)</span>
<span class="token variable">$birthdaystart</span> <span class="token operator">=</span> <span class="token function">strtotime</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'2023-12-02'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$end</span> <span class="token operator">=</span> <span class="token function">time</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$diff_days</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token variable">$birthdaystart</span> <span class="token operator">-</span> <span class="token variable">$end</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$birthday</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword type-casting">int</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token variable">$diff_days</span><span class="token operator">/</span><span class="token number">86400</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$birthday</span> <span class="token operator">=</span> <span class="token function">str_replace</span><span class="token punctuation">(</span><span class="token string double-quoted-string">"-"</span><span class="token punctuation">,</span> <span class="token string double-quoted-string">""</span><span class="token punctuation">,</span> <span class="token variable">$birthday</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$tianqiurl</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'https://yiketianqi.com/api?version=v5&appid=12345678&appsecret=12345678&city=堪培拉'</span><span class="token punctuation">;</span> <span class="token comment">//修改为自己的测试</span>
<span class="token variable">$tianqiapi</span> <span class="token operator">=</span> <span class="token function">https_request</span><span class="token punctuation">(</span><span class="token variable">$tianqiurl</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$tianqi</span> <span class="token operator">=</span> <span class="token function">json_decode</span><span class="token punctuation">(</span><span class="token variable">$tianqiapi</span><span class="token punctuation">,</span> <span class="token constant boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$qinghuaqiurl</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'https://api.youguo56.com/api/chp/'</span><span class="token punctuation">;</span>
<span class="token variable">$qinghuaapi</span> <span class="token operator">=</span> <span class="token function">https_request</span><span class="token punctuation">(</span><span class="token variable">$qinghuaqiurl</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">#$qinghua = json_decode($qinghuaapi, true);</span>
<span class="token variable">$yjh</span> <span class="token operator">=</span> <span class="token string single-quoted-string">''</span><span class="token punctuation">;</span>
<span class="token variable">$touser</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'vfvfvfv'</span><span class="token punctuation">;</span> <span class="token comment">//这个填你女朋友的openid</span>
<span class="token variable">$data</span> <span class="token operator">=</span> <span class="token keyword">array</span><span class="token punctuation">(</span>
<span class="token string single-quoted-string">'touser'</span> <span class="token operator">=></span> <span class="token variable">$touser</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'template_id'</span> <span class="token operator">=></span> <span class="token string double-quoted-string">"regrgagff"</span><span class="token punctuation">,</span> <span class="token comment">//改成自己的模板id,在微信后台模板消息里查看</span>
<span class="token string single-quoted-string">'url'</span><span class="token operator">=></span><span class="token string double-quoted-string">"https://api.youguo56.com/api/60s/"</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'data'</span> <span class="token operator">=></span> <span class="token keyword">array</span><span class="token punctuation">(</span>
<span class="token string single-quoted-string">'first'</span> <span class="token operator">=></span> <span class="token keyword">array</span><span class="token punctuation">(</span>
<span class="token string single-quoted-string">'value'</span> <span class="token operator">=></span> <span class="token variable">$yjh</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'color'</span> <span class="token operator">=></span> <span class="token string double-quoted-string">"#00BFFF"</span>
<span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'keyword1'</span> <span class="token operator">=></span> <span class="token keyword">array</span><span class="token punctuation">(</span>
<span class="token string single-quoted-string">'value'</span> <span class="token operator">=></span> <span class="token variable">$tianqi</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'update_time'</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token comment">//时间</span>
<span class="token string single-quoted-string">'color'</span> <span class="token operator">=></span> <span class="token string double-quoted-string">"#1E90FF"</span>
<span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'keyword2'</span> <span class="token operator">=></span> <span class="token keyword">array</span><span class="token punctuation">(</span>
<span class="token string single-quoted-string">'value'</span> <span class="token operator">=></span> <span class="token variable">$tianqi</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'country'</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token comment">//国家</span>
<span class="token string single-quoted-string">'color'</span> <span class="token operator">=></span> <span class="token string double-quoted-string">"#7B68EE"</span>
<span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'keyword3'</span> <span class="token operator">=></span> <span class="token keyword">array</span><span class="token punctuation">(</span>
<span class="token string single-quoted-string">'value'</span> <span class="token operator">=></span> <span class="token variable">$tianqi</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'city'</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token comment">//城市</span>
<span class="token string single-quoted-string">'color'</span> <span class="token operator">=></span> <span class="token string double-quoted-string">"#7B68EE"</span>
<span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'keyword4'</span> <span class="token operator">=></span> <span class="token keyword">array</span><span class="token punctuation">(</span>
<span class="token string single-quoted-string">'value'</span> <span class="token operator">=></span> <span class="token variable">$tianqi</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'data'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'0'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'wea'</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token comment">//天气</span>
<span class="token string single-quoted-string">'color'</span> <span class="token operator">=></span> <span class="token string double-quoted-string">"#FFA07A"</span>
<span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'keyword5'</span> <span class="token operator">=></span> <span class="token keyword">array</span><span class="token punctuation">(</span>
<span class="token string single-quoted-string">'value'</span> <span class="token operator">=></span> <span class="token variable">$tianqi</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'data'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'0'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'tem1'</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token comment">//高温</span>
<span class="token string single-quoted-string">'color'</span> <span class="token operator">=></span> <span class="token string double-quoted-string">"#98FB98"</span>
<span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'keyword6'</span> <span class="token operator">=></span> <span class="token keyword">array</span><span class="token punctuation">(</span>
<span class="token string single-quoted-string">'value'</span> <span class="token operator">=></span> <span class="token variable">$tianqi</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'data'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'0'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'tem2'</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token comment">//低温</span>
<span class="token string single-quoted-string">'color'</span> <span class="token operator">=></span> <span class="token string double-quoted-string">"#98FB98"</span>
<span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'keyword8'</span> <span class="token operator">=></span> <span class="token keyword">array</span><span class="token punctuation">(</span>
<span class="token string single-quoted-string">'value'</span> <span class="token operator">=></span> <span class="token variable">$love</span> <span class="token operator">.</span> <span class="token string single-quoted-string">'天'</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'color'</span> <span class="token operator">=></span> <span class="token string double-quoted-string">"#FA8072"</span>
<span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'keyword9'</span> <span class="token operator">=></span> <span class="token keyword">array</span><span class="token punctuation">(</span>
<span class="token string single-quoted-string">'value'</span> <span class="token operator">=></span> <span class="token variable">$birthday</span> <span class="token operator">.</span> <span class="token string single-quoted-string">'天'</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'color'</span> <span class="token operator">=></span> <span class="token string double-quoted-string">"#F5DEB3"</span>
<span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'remark'</span> <span class="token operator">=></span> <span class="token keyword">array</span><span class="token punctuation">(</span>
<span class="token string single-quoted-string">'value'</span> <span class="token operator">=></span> <span class="token variable">$qinghuaapi</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'color'</span> <span class="token operator">=></span> <span class="token string double-quoted-string">"#6495ED"</span>
<span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">)</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$json_data</span> <span class="token operator">=</span> <span class="token function">json_encode</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$url</span> <span class="token operator">=</span> <span class="token string double-quoted-string">"https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="</span> <span class="token operator">.</span> <span class="token variable">$ACCESS_TOKEN</span><span class="token punctuation">;</span>
<span class="token variable">$res</span> <span class="token operator">=</span> <span class="token function">https_request</span><span class="token punctuation">(</span><span class="token variable">$url</span><span class="token punctuation">,</span> <span class="token function">urldecode</span><span class="token punctuation">(</span><span class="token variable">$json_data</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$res</span> <span class="token operator">=</span> <span class="token function">json_decode</span><span class="token punctuation">(</span><span class="token variable">$res</span><span class="token punctuation">,</span> <span class="token constant boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$res</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'errcode'</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token operator">&&</span> <span class="token variable">$res</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'errcode'</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token string double-quoted-string">"ok"</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">echo</span> <span class="token string double-quoted-string">"发送成功!<br/>"</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token punctuation">{</span>
<span class="token keyword">echo</span> <span class="token string double-quoted-string">"发送失败!请检查代码!!!<br/>"</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">function</span> <span class="token function-definition function">https_request</span><span class="token punctuation">(</span><span class="token variable">$url</span><span class="token punctuation">,</span> <span class="token variable">$data</span> <span class="token operator">=</span> <span class="token constant">null</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token variable">$curl</span> <span class="token operator">=</span> <span class="token function">curl_init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">curl_setopt</span><span class="token punctuation">(</span><span class="token variable">$curl</span><span class="token punctuation">,</span> <span class="token constant">CURLOPT_URL</span><span class="token punctuation">,</span> <span class="token variable">$url</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">curl_setopt</span><span class="token punctuation">(</span><span class="token variable">$curl</span><span class="token punctuation">,</span> <span class="token constant">CURLOPT_SSL_VERIFYPEER</span><span class="token punctuation">,</span> <span class="token constant boolean">FALSE</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">curl_setopt</span><span class="token punctuation">(</span><span class="token variable">$curl</span><span class="token punctuation">,</span> <span class="token constant">CURLOPT_SSL_VERIFYHOST</span><span class="token punctuation">,</span> <span class="token constant boolean">FALSE</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token keyword">empty</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token function">curl_setopt</span><span class="token punctuation">(</span><span class="token variable">$curl</span><span class="token punctuation">,</span> <span class="token constant">CURLOPT_POST</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">curl_setopt</span><span class="token punctuation">(</span><span class="token variable">$curl</span><span class="token punctuation">,</span> <span class="token constant">CURLOPT_POSTFIELDS</span><span class="token punctuation">,</span> <span class="token variable">$data</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token function">curl_setopt</span><span class="token punctuation">(</span><span class="token variable">$curl</span><span class="token punctuation">,</span> <span class="token constant">CURLOPT_RETURNTRANSFER</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$output</span> <span class="token operator">=</span> <span class="token function">curl_exec</span><span class="token punctuation">(</span><span class="token variable">$curl</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">curl_close</span><span class="token punctuation">(</span><span class="token variable">$curl</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> <span class="token variable">$output</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">function</span> <span class="token function-definition function">handler</span><span class="token punctuation">(</span><span class="token variable">$context</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment"># 啥都没有 就是为了填执行失败的坑</span>
<span class="token comment"># return $ret;</span>
<span class="token punctuation">}</span>
复制自己的appid和 appsecret 换成自己的appid、appsecret、城市
新模板内容
现在是<span class="token punctuation">{</span><span class="token punctuation">{</span>keyword1<span class="token operator">.</span><span class="token constant">DATA</span><span class="token punctuation">}</span><span class="token punctuation">}</span>
国家:<span class="token punctuation">{</span><span class="token punctuation">{</span>keyword2<span class="token operator">.</span><span class="token constant">DATA</span><span class="token punctuation">}</span><span class="token punctuation">}</span>
城市:<span class="token punctuation">{</span><span class="token punctuation">{</span>keyword3<span class="token operator">.</span><span class="token constant">DATA</span><span class="token punctuation">}</span><span class="token punctuation">}</span>
天气:<span class="token punctuation">{</span><span class="token punctuation">{</span>keyword4<span class="token operator">.</span><span class="token constant">DATA</span><span class="token punctuation">}</span><span class="token punctuation">}</span>
最高温度:<span class="token punctuation">{</span><span class="token punctuation">{</span>keyword5<span class="token operator">.</span><span class="token constant">DATA</span><span class="token punctuation">}</span><span class="token punctuation">}</span> ℃
最低温度:<span class="token punctuation">{</span><span class="token punctuation">{</span>keyword6<span class="token operator">.</span><span class="token constant">DATA</span><span class="token punctuation">}</span><span class="token punctuation">}</span> ℃
今天是我们在一起的第<span class="token punctuation">{</span><span class="token punctuation">{</span>keyword8<span class="token operator">.</span><span class="token constant">DATA</span><span class="token punctuation">}</span><span class="token punctuation">}</span>
距离宝贝的生日还有<span class="token punctuation">{</span><span class="token punctuation">{</span>keyword9<span class="token operator">.</span><span class="token constant">DATA</span><span class="token punctuation">}</span><span class="token punctuation">}</span>
每日一句<span class="token punctuation">:</span> <span class="token punctuation">{</span><span class="token punctuation">{</span>remark<span class="token operator">.</span><span class="token constant">DATA</span><span class="token punctuation">}</span><span class="token punctuation">}</span>
在这里新增了每日新闻,如果不想要可以删掉这代码。
测试成功
期待下次的分享,别忘了三连支持博主呀~
我是 念舒_C.ying ,期待你的关注~💪💪💪
文章评论