dongyunqin7307 2016-02-19 19:26
浏览 90
已采纳

file_get_contents api链接两个与会话的连接

I have two api pages, api1.php and api2.php. On 1 there is set an session and on 2 this session needs to be returned. Ofcourse there will be additional functions but my goal of this is to link those two api connections to one and eachother by using a session.

api1.php:

session_start();

$api_key = 'dfdsakdsfjdskfjdskfdsjfdfewfifjjsd';

$_SESSION['api_key'] = $api_key;
setcookie('api_key', $api_key);

api2.php:

session_start();

echo $_SESSION['api_key'];
echo $_COOKIE['api_key'];

test.php:

$url = 'http://example.com/api1.php';
$content1 = file_get_contents($url);

$url2 = 'http://example.com/api2.php'; 
$content2 = file_get_contents($url2);
echo $content2;

As you may have noticed, i'm visiting the page test.php to obtain a result. But no result is being returned. Can somebody tell me why this is not working and what may be an additional way of making all of this happen?

(Notice: the example.com are both the same site (mine))

  • 写回答

1条回答 默认 最新

  • doutui839638 2016-02-19 19:51
    关注

    You're code "links" correctly. The problem is actually in test.php! Instead of executing the code contained in both files, it retrieves the entire file. If you view source you will note the PHP tags and your code. A better solution to check if this is working is to go to api1.php and api2.php separately. With some code adjustments you could also just use the include() or require() functions. Which would look like this:

    api2.php

    echo $_SESSION['api_key'] . "
    <br/>
    ";
    echo $_COOKIE['api_key'];
    

    test.php

    include('api1.php');
    include('api2.php');
    

    It's worth noting the using the include and require functions executes the code in api1.php and api2.php as if that code were a part of test.php.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错