Maison php教程 PHP源码 API获取天气预报,以及使用飞信API,给好友发送短信

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

May 25, 2016 pm 05:09 PM

php代码

<?php
##########
# jiam 2012/12/5
# 1113160783@qq.com
#自动发送飞信
#中间涉及到飞信接口
#天气预报API
#以及城市代码
##########
#101180101是郑州城市代码
#101180301是新乡城市代码
$url=&#39;http://m.weather.com.cn/data/101180101.html&#39;;
$xinxiang=&#39;http://m.weather.com.cn/data/101180301.html&#39;;
$w=curl($url);
$weather=json_decode($w);
$weatherinfo=object_to_array($weather);
$info=$weatherinfo[&#39;weatherinfo&#39;];
$str=$info[&#39;date_y&#39;].&#39;,&#39;.$info[&#39;week&#39;].&#39;。&#39;.$info[&#39;city&#39;].&#39;今天气温:&#39;.$info[&#39;temp1&#39;].&#39;,天气:&#39;.$info[&#39;weather1&#39;].&#39;有&#39;.$info[&#39;wind1&#39;].&#39;,风力:&#39;.$info[&#39;fx1&#39;].&#39;,穿衣建议:&#39;.$info[&#39;index_d&#39;].$info[&#39;index48_d&#39;];

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

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

print_r($send);die();

function curl($url, $postFields = null)
{
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_FAILONERROR, false);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	//https 请求
	if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) {
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
	}

	if (is_array($postFields) && 0 < count($postFields))
	{
		$postBodyString = "";
		$postMultipart = false;
		foreach ($postFields as $k => $v)
		{
			if("@" != substr($v, 0, 1))//判断是不是文件上传
			{
				$postBodyString .= "$k=" . urlencode($v) . "&"; 
			}
			else//文件上传用multipart/form-data,否则用www-form-urlencoded
			{
				$postMultipart = true;
			}
		}
		unset($k, $v);
		curl_setopt($ch, CURLOPT_POST, true);
		if ($postMultipart)
		{
			curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
		}
		else
		{
			curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1));
		}
	}
	$reponse = curl_exec($ch);
	
	if (curl_errno($ch))
	{
		throw new Exception(curl_error($ch),0);
	}
	else
	{
		$httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		if (200 !== $httpStatusCode)
		{
			throw new Exception($reponse,$httpStatusCode);
		}
	}
	curl_close($ch);
	return $reponse;
}
function object_to_array($obj)
{
	$_arr = is_object($obj) ? get_object_vars($obj) : $obj;
	foreach ($_arr as $key => $val)
	{
		$val = (is_array($val) || is_object($val)) ? object_to_array($val) : $val;
		$arr[$key] = $val;
	}
	return $arr;
}
?>
Copier après la connexion
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn

Outils d'IA chauds

Undresser.AI Undress

Undresser.AI Undress

Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover

AI Clothes Remover

Outil d'IA en ligne pour supprimer les vêtements des photos.

Undress AI Tool

Undress AI Tool

Images de déshabillage gratuites

Clothoff.io

Clothoff.io

Dissolvant de vêtements AI

Video Face Swap

Video Face Swap

Échangez les visages dans n'importe quelle vidéo sans effort grâce à notre outil d'échange de visage AI entièrement gratuit !

Outils chauds

Bloc-notes++7.3.1

Bloc-notes++7.3.1

Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise

SublimeText3 version chinoise

Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1

Envoyer Studio 13.0.1

Puissant environnement de développement intégré PHP

Dreamweaver CS6

Dreamweaver CS6

Outils de développement Web visuel

SublimeText3 version Mac

SublimeText3 version Mac

Logiciel d'édition de code au niveau de Dieu (SublimeText3)