PHP使用cURL:无法将Cookie传递给Iframe

时间:2018-11-22 13:58:57

标签: php curl iframe session-cookies

寻找了一段时间没有成功的解决方案。另外,我真的没有尝试过php和REST API,因此对任何新手错误都表示歉意。

我需要 1)将POST请求发送到服务器,并获取与我的登录名关联的cookie 2)重复使用这些cookie和基于同一服务器的url,该服务器将运行报告。

要执行此操作,我使用php和cURL。下面的脚本可以正常工作:

    <?php
$homeurl = "http://localhost:8080/pentaho/j_spring_security_check?j_username=admin&j_password=admin&locale=en_US";
$reporturl = 'http://localhost:8080/pentaho/api/repos/:public:IframeDossier:IframeTest.prpt/viewer?label=fin';


$cookie_jar = tempnam('/Users/pierre/Vrac','cookie');
    if (!file_exists(realpath($cookie_jar))) touch($cookie_jar);

//login and get cookies
$ch = curl_init($homeurl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$return = curl_exec($ch);

// run report url using cookies
echo "execution report";
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
curl_setopt($ch, CURLOPT_URL, $reporturl);
$page = curl_exec($ch);
echo $page;


curl_close($ch);

?>

我得到了预期的回报:

HTTP / 1.1 100 HTTP / 1.1 415 Set-Cookie:session-expiry = 1542901561740; Path = / Set-Cookie:server-time = 1542894361740; Path = / Content-Length:0 Date:Thu, 2018年11月22日13:46:01 GMT连接:close

现在,我想将$ reporturl嵌入到iframe中。我建立了以下Iframe:

<iframe id="inlineFrameExample"
    title="Inline Frame Example"
    width="1000"
    height="2000" src="http://localhost:8080/pentaho/api/repos/:public:IframeDossier:IframeTest.prpt/viewer?label=fin">
    </iframe>

当我调用其网址时,我的iframe可以正常运行。

我现在在代码中调用iframe网址。所以

curl_setopt($ch, CURLOPT_URL, $reporturl);

成为

curl_setopt($ch, CURLOPT_URL, http://localhost:8080/IframeTest.html);

问题在于,现在,我的浏览器要求进行身份验证,而我的Iframe应该已经使用在代码第一步中获取的Cookie进行了身份验证。我一定在做错事,但无法弄清楚……任何帮助,我都感激不尽。

谢谢

0 个答案:

没有答案
相关问题