config.php
<?php
namespace app\src;
use ArrayAccess;
/**
* Config
*
* @package
* @author QQ43599939
* @copyright QQ43599939
* @version 2017
* @access public
*/
class config implements ArrayAccess
{
private $configData;
public function __construct(array $config = [])
{
$this->configData = $config;
}
/**
* Config::offsetExists()
*
* @param mixed $key
* @return
*/
public function offsetExists($key)
{
return isset($this->configData[$key]);
}
/**
* Config::offsetSet()
*
* @param mixed $key
* @param mixed $value
* @return
*/
public function offsetSet($key, $value)
{
$this->configData[$key] = $value;
}
/**
* Config::offsetGet()
*
* @param mixed $key
* @return
*/
public function offsetGet($key)
{
return $this->configData[$key];
}
/**
* Config::offsetUnset()
*
* @param mixed $key
* @return
*/
public function offsetUnset($key)
{
unset($this->configData[$key]);
}
}
factory.php
<?php
namespace app\src;
include __DIR__.DIRECTORY_SEPARATOR.'config.php';
include __DIR__.DIRECTORY_SEPARATOR.'myhttpRequest.php';
include __DIR__.DIRECTORY_SEPARATOR.'myException.php';
/**
* factory
*
* @package
* @author QQ43599939
* @copyright QQ43599939
* @version 2017
* @access public
*/
class factory
{
use myhttpRequest;
private $config;
private $city;
/**
* factory::__construct()
*
* @param mixed $config
* @return
*/
public function __construct(array $config = [])
{
$this->config = new config($config);
}
/**
* factory::city()
*
* @param mixed $city
* @return
*/
public function city($city = '重庆')
{
$this->city = $city;
return $this;
}
/**
* factory::build()
*
* @return
*/
public function build()
{
$headers = array();
if($this->config['AppCode']=='' || !$this->config['AppCode'])
throw new myException("AppCode is null!", -1);
if($this->config['Url']=='' || !$this->config['Url'])
throw new myException("Url is null!", -1);
array_push($headers, "Authorization:APPCODE " . $this->config['AppCode']);
return $this->getCurl($this->config['Url'].'?city='.$this->city, $headers);
}
}
myhttpRequest.php
<?php
namespace app\src;
trait myhttpRequest
{
/**
* getCurl()
*
* @param mixed $url
* @param mixed $headers
* @return
*/
public function getCurl($url,$headers){
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, false);
if (1 == strpos("$".$url, "https://"))
{
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
return curl_exec($curl);
}
}
myException.php
<?php
namespace app\src;
/**
* myException
*
* @package
* @author QQ43599939
* @copyright QQ43599939
* @version 2017
* @access public
*/
class myException extends \Exception
{
/**
* myException::__construct()
*
* @param mixed $errorMessage
* @param mixed $errorCode
* @return
*/
function __construct($errorMessage, $errorCode)
{
parent::__construct($errorMessage);
$this->errorMessage = $errorMessage;
$this->errorCode = $errorCode;
$this->setErrorType("my");
}
private $errorCode;
private $errorMessage;
private $errorType;
/**
* myException::getErrorCode()
*
* @return
*/
public function getErrorCode()
{
return $this->errorCode;
}
/**
* myException::setErrorCode()
*
* @param mixed $errorCode
* @return
*/
public function setErrorCode($errorCode)
{
$this->errorCode = $errorCode;
}
/**
* myException::getErrorMessage()
*
* @return
*/
public function getErrorMessage()
{
return $this->errorMessage;
}
/**
* myException::setErrorMessage()
*
* @param mixed $errorMessage
* @return
*/
public function setErrorMessage($errorMessage)
{
$this->errorMessage = $errorMessage;
}
/**
* myException::getErrorType()
*
* @return
*/
public function getErrorType()
{
return $this->errorType;
}
/**
* myException::setErrorType()
*
* @param mixed $errorType
* @return
*/
public function setErrorType($errorType)
{
$this->errorType = $errorType;
}
}
test.php
<?php
// +----------------------------------------------------------------------
// | Author: qq43599939 <43599939@qq.com-->
// +----------------------------------------------------------------------
namespace app\src;
include __DIR__.DIRECTORY_SEPARATOR.'factory.php';
$params = array(
'Url' => 'http://jisutqybmf.market.alicloudapi.com/weather/query',
'AppCode' => 'democode',
);
$factory = new factory($params);
$result_json = $factory->city('重庆')->build();
var_dump(json_decode($result_json,true));