我有下面的类:PHP的设计问题
class Apiconnect {
const URL = 'https://someurl.com/api.php';
const USERNAME = 'user';
const PASSWORD = 'pass';
/**
*
* @param <array> $postFields
* @return SimpleXMLElement
* @desc this connects but also sends and retrieves the information returned in XML
*/
public function Apiconnect($postFields)
{
$postFields["username"] = self::USERNAME;
$postFields["password"] = md5(self::PASSWORD);
$postFields["responsetype"] = 'xml';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, self::URL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
$data = curl_exec($ch);
curl_close($ch);
$xml = new SimpleXMLElement($data);
if($xml->result == "success")
{
return $xml;
}
else
{
return $xml->message;
}
}
}
现在我想使用此连接类上,将使用其他类的其他方法。
所以,我想这样做一个类(不知道抽象为宜这里),像这样:
abstract class ApiSomething
{
protected $_connection;
protected $_postFields = array();
/**
* @desc - Composition.
*/
public function __construct()
{
require_once("apiconnect.php");
$this->_connection = new Apiconnect($this->_postFields);
}
public function getStuff()
{
//this is the necessary field that needs to be send.
//Containing the action that the API should perform.
$this->_postFields["action"] = "dosomething";
...
}
}
我需要使用上getStuff()方法的属性$ _connection从而使“行动”发送到API。但不知道,那怎么能成就呢。
请帮忙吗?
您错误地使用了md5。 – rook 2010-10-15 17:11:53
@Rook - 如果我这样做,这个API似乎只能工作。但是你有什么建议? – MEM 2010-10-15 17:58:00
那么,https是最适合正在使用的交通工具。像这样使用md5的真正问题是,现在md5散列是你的密码,所以如果有人得到md5散列,那么他们可以立即登录。密码存储在数据库中很可能是这样的,然后可以使用SQL注入来立即访问,从而破坏散列密码的目的。 – rook 2010-10-15 18:01:02