显示用户登录php Curl的值

时间:2017-04-19 09:01:22

标签: php rest api curl post

请在页面底部看到更新问题

我正在尝试使用用户详细信息登录,验证我的用户并在$var1中存储用户名称。

问题

如何在第二个代码示例中的http://localhost/app/api/user/末尾动态添加 uid

我拥有的

我有一个Curl,它首先记录用户并将会话cookie传递给文本文件,以便稍后验证用户

的login.php

<?php
$service_url = 'http://localhost/app/api/user/login.xml'; // .xml asks for xml data in response
$post_data   = array(
    'username' => 'something',
    'password' => 'something#',
);
$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 "RESPONSE:\n";
var_dump( $response );

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

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

然后,我可以进入验证用户凭据的页面。我需要此页面将与当前登录用户关联的值存储在变量中,以便我可以使用帖子中的值。

Authenticate.php

 <?php
    $service_url    = 'http://localhost/app/api/user/(userid).xml'; // I need to pass in the current users ID
    $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

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


  $xml=simplexml_load_file("$service_url") or die("Error: Cannot create 
  object");
  echo $xml->name . "<br>";

如果我导航到网址http://localhost/app/api/user/1,我会

<result>
<uid>1</uid>
<name>Tom</name>
<theme/>
<signature/>
<signature_format/>
<created>1473675394</created>
<access>1492591142</access>
<login>1492532484</login>
<status>1</status>
<timezone>Europe/London</timezone>
<language/>
<picture/>
<data/>
<uuid>c14e4770-3f55-43fa-af1c-b977454f29fd</uuid>
<roles is_array="true">
<item>authenticated user</item>
<item>administrator</item>
</roles>
</result>

更新

感谢@AlexSlipknot我现在已经在登录页面上打印了我的用户ID,现在我应该如何传递到我的 authentication.php 页面,(见下面的代码)。

我的更新问题

现在将此 $ user_id 传递到我的authentication.php页面然后在网址http://localhost/communicator-app/bensapi/user/?

中使用它的最佳方式是什么?

更新登录.PHP

<?php
$service_url = 'http://localhost/app/api/user/login.xml'; // .xml asks for xml data in response
$post_data   = array(
    'username' => 'user',
    'password' => 'pass',
);
$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 "RESPONSE:\n";
var_dump( $response );

// parse the response
$xml            = new SimpleXMLElement( $response );
$session_cookie = $xml->session_name . '=' . $xml->sessid;
$user_name = $xml->user->name . "<br>";
$user_id = $xml->user->uid . "<br>";
print "SESSION_COOKIE: $session_cookie";
echo "<br>";
print "UserName: $user_name";
print "UserID: $user_id";

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

0 个答案:

没有答案
相关问题