开发者社区 问答 正文

我用开源中国的客户端,做了一个服务端登陆httpclient post老是301烦死了报错 

http://www.oschina.net/action/api/login_validate/ 和这个做了个一样的,但不知道他们的代码是什么我做出来post提交会301有什么办法可以不用更改客户端直接200提交

服务端是discuz

<?php

require '../config/config.php';

if (! function_exists ( 'uc_user_login' )) { loaducenter (); } $username = urldecode ( getgpc ( 'username' ) ); $password = getgpc ( 'pwd' ); $username = htmlspecialchars ( trim ( $username ) ); $password = htmlspecialchars ( trim ( $password ) );

if (strtoupper ( $_G ['charset'] ) != "UTF-8") { require_once libfile ( 'class/chinese' ); $chs = new Chinese ( 'utf-8', CHARSET ); $username = $chs->convert ( $username ); } // elseif(checkUid($username)){ // $result = uc_user_login($username, $password, 1, 1, '', ''); // } if (checkEmailLogin ( $username )) { $result = uc_user_login ( $username, $password, 2, 1, '', '' ); } else { $result = uc_user_login ( $username, $password, 0, 0, '', '' ); } $uid = $result ["0"]; $email = $result ["3"];

/* * 用PHP的DOM控件来创建XML输出 * 设置输出内容的类型为xml */ header ( 'Content-Type: text/xml;' ); // 创建新的xml文件 $dom = new DOMDocument ( '1.0', 'utf-8' );

// 建立<response>元素 $response = $dom->createElement ( 'oschina' ); $dom->appendChild ( $response );

// 建立<books>元素并将其作为<response>的子元素 $books = $dom->createElement ( 'result' ); $response->appendChild ( $books );

$errorCode = $dom->createElement ( 'errorCode' );

$books->appendChild ( $errorCode ); // 为book创建标题 if (! isset ( $uid ) || $uid <= 0) { $errorCodeText = $dom->createTextNode ( "0" ); $errorCode->appendChild ( $errorCodeText ); $title = $dom->createCDATASection ( "用户名或口令错" ); } else { $errorCodeText = $dom->createTextNode ( "1" ); $errorCode->appendChild ( $errorCodeText ); $title = $dom->createCDATASection ( "登录成功" ); }

// 为book创建isbn元素 /* * $isbn = $dom->createElement('isbn'); * $isbnText = $dom->createTextNode('1-21258986'); * $isbn->appendChild($isbnText); */

// 创建book元素 $errorMessage = $dom->createElement ( 'errorMessage' ); $errorMessage->appendChild ( $title );

// 将<book>作为<books>子元素 $books->appendChild ( $errorMessage ); if ($uid > 0) { $user = $dom->createElement ( 'user' ); $response->appendChild ( $user ); $uidElement = $dom->createElement ( 'uid' ); $uidText = $dom->createTextNode ( $uid ); $uidElement->appendChild ( $uidText ); $addressElement = $dom->createElement ( 'location' ); $addressdata = USER::getUserprofile ( $uid ); $addresstitle = $dom->createCDATASection ( $addressdata ["address"] ); $user->appendChild ( $uidElement ); $addressElement->appendChild ( $addresstitle ); $user->appendChild ( $addressElement ); $usernameElement = $dom->createElement ( 'name' ); $usernameText = $dom->createCDATASection ( $result[1] ); $usernameElement->appendChild ( $usernameText ); $user->appendChild ( $usernameElement ); $flowers=$dom->createElement("followers"); $flowersText=$dom->createTextNode("0"); $flowers->appendChild($flowersText); $user->appendChild($flowers); $fans=$dom->createElement("fans"); $fansText=$dom->createTextNode("10"); $fans->appendChild($fansText); $user->appendChild($fans); $score=$dom->createElement("score"); $scoreText=$dom->createTextNode("0"); $score->appendChild($scoreText); $user->appendChild($score); $portrait=$dom->createElement("portrait"); $user->appendChild($portrait); $favoritecount = $dom->createElement ( "favoritecount" ); $favoritecountText = $dom->createTextNode ( "1" ); $favoritecount->appendChild ( $favoritecountText ); $user->appendChild ( $favoritecount ); $gender = $dom->createElement ( "gender" ); $genderText = $dom->createTextNode ( USER::getUserprofile ( $uid )["gender"] ); $gender->appendChild ( $genderText ); $user->appendChild ( $gender ); } $notice=$dom->createElement("notice"); $response->appendChild($notice); $atmeCount=$dom->createElement("atmeCount"); $atmeCountText=$dom->createTextNode("0"); $atmeCount->appendChild($atmeCountText); $notice->appendChild($atmeCount); $msgCount=$dom->createElement("msgCount"); $msgCountText=$dom->createTextNode("0"); $msgCount->appendChild($msgCountText); $notice->appendChild($msgCount); $reviewCount=$dom->createElement("reviewCount"); $reviewCountText=$dom->createTextNode("0"); $reviewCount->appendChild($reviewCountText); $notice->appendChild($reviewCount); $newFansCount=$dom->createElement("newFansCount"); $newFansCountText=$dom->createTextNode("0"); $newFansCount->appendChild($newFansCountText); $notice->appendChild($newFansCount); // 在一字符串变量中建立XML结构 $xmlString = $dom->saveXML ();

// 输出XML字符串 echo $xmlString;

?>

展开
收起
kun坤 2020-06-10 09:58:51 527 分享 版权
阿里云 AI 助理回答

根据您提供的代码片段,这段PHP脚本是用于处理登录验证的逻辑,并返回一个XML格式的响应。当用户通过POST请求提交用户名和密码时,该脚本会使用UCenter(Discuz! 的用户中心组件)进行登录验证,然后根据验证结果构建并返回一个XML结构。

关于您提到的301重定向问题,这通常发生在Web服务器层面,而不是由PHP脚本直接引起的。301状态码表示永久重定向,意味着服务器希望客户端使用另一个URL来访问请求的资源。在您的场景中,可能的原因包括但不限于:

  1. URL重写规则:服务器上的.htaccess文件或Nginx配置可能有重写规则,将原始请求URL重定向到了另一个地址。
  2. 域名或路径变更:如果您的应用程序之前有过迁移或者路径结构调整,旧的链接可能会被设置为永久重定向到新的位置。
  3. 程序框架配置:某些应用框架或CMS(如Discuz!)本身可能有内置的重定向逻辑,比如安全中间件强制HTTPS或者登录后跳转等。

解决办法

  • 检查服务器配置:首先查看您的Web服务器(Apache、Nginx等)配置文件以及.htaccess文件,确认是否有导致301重定向的规则存在,并适当调整或注释掉这些规则。

  • Discuz! 设置:如果您是在Discuz! 环境下遇到这个问题,检查后台管理中的“全局”->“SEO设置”或“站点功能”->“域名设置”,确保没有误设重定向规则。

  • 代码审查:虽然从提供的代码片段看不出来直接导致301重定向的部分,但可以检查整个项目中是否有其他地方调用了重定向函数,如PHP的header("Location: ...")

  • 测试环境与生产环境一致性:确保开发和部署环境的配置一致,有时候差异化的配置也会导致这类问题。

如果确定不是代码层面的问题,而是服务器配置导致的301重定向,那么解决方法主要集中在调整服务器配置上,无需修改客户端代码。如果需要进一步的技术支持,建议提供更详细的错误信息或日志,以便更准确地定位问题所在。

有帮助
无帮助
AI 助理回答生成答案可能存在不准确,仅供参考
0 条回答
写回答
取消 提交回答
问答分类:
问答地址: