doulu4534 2017-05-02 12:04
浏览 58
已采纳

PHP可以存储Cookie并使用这些cookie发送curl请求吗?

We have 2 application run in 2 different server, let say SERVER_X (PHP) and SERVER_Y (NODE).

In SERVER_X index.php

$url = 'http://10.10.10.2/login';
$poststring = 'username=admin&password=password';
$ch = curl_init ($url);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $poststring);
$output = curl_exec ($ch);
curl_close($ch);


$url = 'http://10.10.10.2/getdata';
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_POST, 1);
$output = curl_exec ($ch);
curl_close($ch);
echo json_decode($output);

enter image description here So what we want to achieve is by accessing SERVER_X/index.php, the CLIENT able to get the data from SERVER_Y where the authentication for SERVER_Y done in SERVER_X PHP code. Is this possible ?

  • 写回答

1条回答 默认 最新

  • dsavz66262 2017-05-16 07:47
    关注

    Manage to make it work with code below,

     define('_IP', '10.10.10.2');
    define('_USERNAME', $_GET['user']);
    define('_PASSWORD', $_GET['pass']);
    define('_ORCHESTRA_PORT', '11111');
    define('_NODE_PORT', '22222');
    
    $main_auth_url = 'http://' . _IP . ':' . _ORCHESTRA_PORT . '/v2/auth/login';
    $main_auth_credential = array("username" => _USERNAME, "password" => _PASSWORD);
    $main_auth_credential = json_encode($main_auth_credential);
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $main_auth_url);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $main_auth_credential);
    curl_setopt($ch, CURLOPT_HEADER, TRUE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    
    $response = curl_exec($ch);
    curl_close($ch);
    
    $user_id = explode('set-cookie: userid=', $response);
    $user_id = explode('; Max-Age', $user_id[1]);
    $user_id = $user_id[0];
    
    $token = explode('set-cookie: token=', $response);
    $token = explode('; Max-Age', $token[1]);
    $token = $token[0];
    
    $connect_sid = explode('set-cookie: connect.sid=', $response);
    $connect_sid = explode('; Path=/', $connect_sid[1]);
    $connect_sid = $connect_sid[0];
    
    $node_user_id = urldecode($user_id);
    $node_user_id = explode('"', $node_user_id);
    $node_user_id = $node_user_id[1];
    
    $node_auth_url = 'http://' . _IP . ':' . _NODE_PORT . '/v2/auth/login';
    $node_auth_credential = array("username" => $node_user_id, "password" => $token);
    $node_auth_credential = json_encode($node_auth_credential);
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $node_auth_url);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $node_auth_credential);
    curl_setopt($ch, CURLOPT_HEADER, TRUE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    
    $response = curl_exec($ch);
    $info = curl_getinfo($ch);
    curl_close($ch);
    
    $connect_sid = explode('set-cookie: connect.sid=', $response);
    $connect_sid = explode('; Path=/', $connect_sid[1]);
    $connect_sid = $connect_sid[0];
    
    $sub_url = 'http://' . _IP . ':' . _NODE_PORT . '/getdata';
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $sub_url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cookie: userid=" . $user_id . "; token=" . $token . "; connect.sid=" . $connect_sid));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    
    $response = curl_exec($ch);
    curl_close($ch);
    
    echo '<pre>';
    print_r($response);
    echo '</pre><hr>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ros的rviz仿真机器人
  • ¥15 关于#linux#的问题(输入输出错误):出现这个界面接着我重新装系统,又让修电脑的师傅帮我扫描硬盘(没有问题)用着用着又卡死(相关搜索:固态硬盘)
  • ¥15 cv::resize不同线程时间不同
  • ¥15 web课程,怎么做啊😭没好好听课 根本不知道怎么下手
  • ¥15 做一个关于单片机的比较难的代码,然后搞一个PPT进行解释
  • ¥15 python提取.csv文件中的链接会经常出现爬取失败
  • ¥15 数据结构中的数组地址问题
  • ¥15 maya的mel里,怎样先选择模型A,然后利用mel脚本自动选择有相同名字的模型B呢。
  • ¥15 Python题,根本不会啊
  • ¥15 会会信号与系统和python的来