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 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)