PHP到Python XML API
我有一个用PHP编写的API,我想用Python编写它,但我甚至不能登录,也不能使用该服务 PHP API使用curl处理请求,在python中,我将使用请求模块:PHP到Python XML API,php,python,curl,Php,Python,Curl,我有一个用PHP编写的API,我想用Python编写它,但我甚至不能登录,也不能使用该服务 PHP API使用curl处理请求,在python中,我将使用请求模块: class CURL { function doRequest($method, $url, $vars) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_FOL
class CURL { function doRequest($method, $url, $vars) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if ($method == 'POST') {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
}
$data = curl_exec($ch);
curl_close($ch);
if ($data) {
return $data;
} else {
return curl_error($ch);
} } function post($url, $vars) {
return $this->doRequest('POST', $url, $vars); } }
正如我提出的要求,事情简单明了:
import requests
response = request.get(url)
response.content
private $methods = array(
'Login'=>'/Login',
'Logout'=>'/Logout',
'Synchronize'=>'/Synchronize',
);
当我尝试从PHP使用/Login方法时,它会说:
loginMethod = url + '/Login'
response = requests.get(loginMethod)
response.content
它说:
> '\xef\xbb\xbf<?xml version="1.0" encoding="utf-8"?>\r\n<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\r\n<html
> xmlns="http://www.w3.org/1999/xhtml">\r\n <head>\r\n
> <title>Service</title>\r\n <style>BODY { color: #000000;
> background-color: white; font-family: Verdana; margin-left: 0px;
> margin-top: 0px; } #content { margin-left: 30px; font-size: .70em;
> padding-bottom: 2em; } A:link { color: #336699; font-weight: bold;
> text-decoration: underline; } A:visited { color: #6699cc; font-weight:
> bold; text-decoration: underline; } A:active { color: #336699;
> font-weight: bold; text-decoration: underline; } .heading1 {
> background-color: #003366; border-bottom: #336699 6px solid; color:
> #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left:
> 30px;padding-top: 16px;} pre { font-size:small; background-color:
> #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space:
> -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right:
> 2px white solid; border-bottom: 2px white solid; font-weight: bold;
> background-color: #cecf9c;} table td { border-right: 2px white solid;
> border-bottom: 2px white solid; background-color:
> #e5e5cc;}</style>\r\n </head>\r\n <body>\r\n <div id="content">\r\n <p class="heading1">Service</p>\r\n
>
> <p>Method not allowed.</p>\r\n </div>\r\n </body>\r\n</html>'
'\xef\xbb\xbf\r\n“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>\r\n xmlns=”http://www.w3.org/1999/xhtml“>\r\n\r\n
>服务\r\n正文{color:#000000;
>背景色:白色;字体系列:Verdana;左边距:0px;
>页边空白顶部:0px;}#内容{页边空白左侧:30px;字体大小:.70em;
>填充底部:2em;}A:link{color:#336699;字体大小:粗体;
>文本装饰:下划线;}A:已访问{颜色:#6699cc;字体重量:
>粗体;文本装饰:下划线;}A:活动{颜色:#336699;
>字体大小:粗体;文本装饰:下划线;}.heading1{
>背景色:#003366;边框底部:#336699 6px纯色;颜色:
>#ffffff;字体系列:Tahoma;字体大小:26px;字体重量:正常;边距:0em 0em 10px-20px;底部填充:8px;左侧填充:
>30px;填充顶部:16px;}pre{字体大小:小;背景色:
>#e5cc;填充:5px;字体系列:Courier New;页边距顶部:0px;边框:1px#f0f0e0实心;空白:预换行;空白:
>-预换行;换行:断开单词;}表{边框折叠:折叠;边框间距:0px;字体系列:Verdana;}表{边框右侧:
>2px白色实心;边框底部:2px白色实心;字体大小:粗体;
>背景色:#cecf9c;}表td{右边框:2px白色固体;
>边框底部:2倍白色纯色;背景色:
>\r\n\r\n\r\n\r\n服务
>
>不允许使用方法。\r\n\r\n\r\n'
此外,dosn`t甚至不允许met访问登录页面,但它可以在PHP中工作
/**************************************/
# doRequest(String:$var,String:$method)#
# $var request string #
# $method servise method #
/**************************************/
private function doRequest($var,$method='')
{
$curl= new CURL();
$return = $curl->post($this->URL.$method,$var);
if ($this->verbose){
echo "\n--\nRequest: $this->URL$method\n",$var,"\nResponse:\n",$return,"\n";
}
return $return;
}
/******************************************/
# Login() #
# makes Login request and save sesion vars #
# return bool #
/******************************************/
public function Login()
{
if($this->UserSession)
{
$this->Logout();
}
$User = new SimpleXMLElement('<User></User>');
$User->addChild('Name',$this->User);
$User->addChild('Hash',$this->Password);
try
{
$data=$this->doRequest($User->asXML(),$this->methods['Login']);
$Session= new SimpleXMLElement($data);
$this->UserSession=$Session->UserSession;
$this->UserToken=$Session->UserToken;
$Session = new SimpleXMLElement('<Session></Session>');
$Session->addChild('UserSession',$this->UserSession);
$Session->addChild('UserToken',$this->UserToken);
$this->SessionXML=$Session->asXML();
}
catch(Exception $e)
{
echo($e->getMessage());
$this->UserSession=false;
return false;
}
if(in_array($this->UserSession,$this->errors))
{
$this->errorCode = $this->UserSession;
$this->UserSession=false;
return false;
}
return true;
}
/**************************************/
#doRequest(字符串:$var,字符串:$method)#
#$var请求字符串#
#$method服务方法#
/**************************************/
私有函数doRequest($var,$method='')
{
$curl=新的curl();
$return=$curl->post($this->URL.$method,$var);
如果($this->verbose){
echo“\n--\n请求:$this->URL$method\n”、$var、“\n响应:\n”、$return、“\n”;
}
return$return;
}
/******************************************/
#登录()#
#发出登录请求并保存sesion变量#
#返回布尔#
/******************************************/
公共函数登录()
{
如果($this->UserSession)
{
$this->Logout();
}
$User=新的SimpleXMLElement(“”);
$User->addChild('Name',$this->User);
$User->addChild('Hash',$this->Password);
尝试
{
$data=$this->doRequest($User->asXML(),$this->methods['Login']);
$Session=新的simplexmlement($data);
$this->UserSession=$Session->UserSession;
$this->UserToken=$Session->UserToken;
$Session=新的SimpleXMLElement(“”);
$Session->addChild('UserSession',$this->UserSession);
$Session->addChild('UserToken',$this->UserToken);
$this->SessionXML=$Session->asXML();
}
捕获(例外$e)
{
echo($e->getMessage());
$this->UserSession=false;
返回false;
}
if(在数组中($this->UserSession,$this->errors))
{
$this->errorCode=$this->UserSession;
$this->UserSession=false;
返回false;
}
返回true;
}
我必须以某种方式发送一个以名称和哈希作为属性的请求来获取令牌,那么我如何将新的XML标记添加到URL请求中?嘿,我也在使用相同的脚本。你能找到自己问题的解决方案吗?我必须以某种方式发送一个以名称和哈希作为属性的请求来获取令牌,那么,我如何将新的XML标记添加到URL请求中呢?嘿,我也在使用同一个脚本。您是否能够找到自己问题的解决方案?