我是Drupal的新手。我的目标是将其用于员工管理自己的内容,然后通过REST从节点中检索其内容到现有的公共网站。

使用Drupal 7.10,服务3. Drupal位于子目录中,而不是站点根,如果很重要。为REST服务器创建了一个名为“ REST”的端点。匿名访问是不可取的,因此已为身份验证的访问请求创建了Drupal用户。

我正在建立一个基于PHP客户 Michaelcole的帖子 用于验证访问。似乎正在与REST服务器交谈,因为起初我在通用PHP页面中看到了Drupal站点主题以及Drupal错误。

目前,我只是想以我的服务用户登录,因此只有login.php代码在我的页面上运行。

PHP正在返回此:

响应:字符串(0)“”致命错误:未识别的异常'异常'带有消息'字符串无法解析为xml'在 /path to-my-page/test-drupal-rest.php:36 stack trace:# 0 /to My-page /test- drupal-rest.php(36):simplexmlelement-> __ construct(''')#1 {main}插入 /进入 /途径 - page /page /test-drupal-rest .php在第36行

第36行是:

$xml = new SimpleXMLElement($response);

如果我只是将完整的服务URL放入浏览器中( http://www.mydomain.com/drupal-folder/rest/user/login.xml ),我得到(XML与空间打破以供在此处显示):

<? XML版本=“ 1.0”编码=“ UTF-8”?> <result/>

我没有想法。你能帮我吗?

编辑/更新:我进入了管理»结构»服务,并发现“应用程序/x-www-form-urlenceded”正在禁用要求解析。我启用了这一点,我的要求开始通过。但是,即使它显然识别我的用户身份证,我仍然无法检索节点。

=====================

编辑/更新2012-01-16 15:10:这是我当前的代码,基于MichaelCole的代码,并为我的工作流和目标进行了修改。它不会检索节点内容。失败似乎是通过节点请求发送会话身份验证的某个地方。登录总是正常工作的,我总是会收到XML用户对象。

loginToDrupal();

$service_url = "http://mysite.com/drupal-folder/resttest/node/10.xml";

$session_cookie = file_get_contents('session_cookie.txt');

print "Ready to send cURL to $service_url";

// set up the request
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);  // have curl_exec return a string
curl_setopt($curl, CURLOPT_COOKIE, "$session_cookie"); // use the previously saved session
curl_setopt($curl, CURLOPT_HTTPGET, true); //do a GET ^^^^added by me...
curl_setopt($curl, CURLOPT_VERBOSE, true); // output to command line

$response = curl_exec($curl);
curl_close($curl);
print "\n<br />RESPONSE:\n";
var_dump($response);

logoutOfDrupal();

//=======================================================================


function loginToDrupal()
    {
    $service_url = 'http://www.mysite.com/drupal-folder/rest/user/login.xml'; // .xml asks for xml data in response
    $post_data = array(
          'username' => 'myusername',
          'password' => 'mypass',
        );
    $post_data = http_build_query($post_data, '', '&'); // Format post data as application/x-www-form-urlencoded

    // set up the request
    $curl = curl_init($service_url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);  // have curl_exec return a string
    curl_setopt($curl, CURLOPT_POST, true);             // do a POST
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data); // POST this data

    // make the request
    curl_setopt($curl, CURLOPT_VERBOSE, true); // output to command line
    $response = curl_exec($curl);
    curl_close($curl);
    print "\n<br />RESPONSE:\n";
    var_dump($response);

    // parse the response
    $xml = new SimpleXMLElement($response);
    $session_cookie = $xml->session_name .'='. $xml->sessid;

    file_put_contents('session_cookie.txt', $session_cookie);

    if($xml->sessid!="")
        {
        print "login successful, session $xml->sessid <br />";
        }
    else
        {
        print "Login appears to have failed.";
        }
    }

//========================================================================================

function logoutOfDrupal()
    {
    print "\n<br />Logging out...<br />";
    $service_url = 'http://mysite.com/drupal-folder/rest/user/logout';
    $session_cookie = file_get_contents('session_cookie.txt');

    // set up the request
    $curl = curl_init($service_url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);  // have curl_exec return a string
    curl_setopt($curl, CURLOPT_COOKIE, "$session_cookie"); // use the previously saved session
    curl_setopt($curl, CURLOPT_POST, true);             // do a POST
    curl_setopt($curl, CURLOPT_POSTFIELDS, ""); // POST this data

    // make the request
    curl_setopt($curl, CURLOPT_VERBOSE, true); // output to command line
    $response = curl_exec($curl);
    curl_close($curl);
    print "\n<br />RESPONSE:\n";
    var_dump($response);
    print "\n<br />reached end of function logoutOfDrupal()<br />";
    }

logIntoDrupal()函数后的调试打印调用总是像这样返回XML:

< ?xml version="1.0" encoding="utf-8"? >< result >< sessid >AU7yWNWPXxORI2DsdJVegdreg2F9rYQxy90dAB5px2s< /sessid >< session_name >SESSd9af11d86664849a8dcda2d6c4af3580< /session_name >< user >< uid >11< /uid >< name >myuser< /name >< mail >me@mysite.com< /mail >< theme >< /theme >< signature >< /signature >< signature_format >filtered_html< /signature_format >< created >1326401726< /created >< access >1326746687< /access >< login >1326748351< /login >< status >1< /status >< timezone >America/Denver< /timezone >< language >< /language >< picture/ >< init >me@mysite.com< /init >< data >< contact >1< /contact >< /data >< roles is_array="true" >< item >authenticated user< /item >< /roles >< rdf_mapping >< rdftype is_array="true" >< item >sioc:UserAccount< /item >< /rdftype >< name >< predicates is_array="true" >< item >foaf:name< /item >< /predicates >< /name >< homepage >< predicates is_array="true" >< item >foaf:page< /item >< /predicates >< type >rel< /type >< /homepage >< /rdf_mapping >< /user >< /result >

即使清楚地返回成功的用户对象,上面的代码每次尝试检索节点时都会返回一个空的XML字符串。

有帮助吗?

解决方案

今天,我发现了我的示例代码一个简单的问题:在节点检索中,URL是:

$service_url = "http://mysite.com/drupal-folder/resttest/node/10.xml";

而在登录功能中,则是:

$service_url = 'http://**www**.mysite.com/drupal-folder/rest/user/login.xml';

我更改了它,以便所有URL指向www.mysite.com,现在可以检索作为身份验证的用户的节点。

其他提示

因此,您是说您正确登录了用户。您正在使用哪种验证方法?会话身份验证?如果是这样,登录响应应返回一个用户对象,您需要从中返回SESSID,将其存储在应用程序/其他网站/客户端上,并在需要检索节点时使用它。您必须将SESSID作为节点检索请求中的另一个参数发送(是的,每次)。

许可以下: CC-BY-SA归因
scroll top