PHP using cURL: unable to pass cookies to Iframe
up vote
0
down vote
favorite
Been searching for a while without sucess for a solution. Also, I'm really not experimented in php and REST API, so apologies for any newbie mistakes.
I need to
1) send a POST request to a server and get cookies associated to my login
2) reuse these cookies with url based on that same server, which runs a report.
To perform this I use php and cURL. The below script works fine:
<?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);
?>
I get the expected return:
"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, 22 Nov 2018 13:46:01 GMT Connection: close"
now instead I would like to embed $reporturl into an Iframe. I've built the following 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>
My Iframe runs correctly when I call its url.
I am now calling the Iframe url in my code. so
curl_setopt($ch, CURLOPT_URL, $reporturl);
becomes
curl_setopt($ch, CURLOPT_URL, http://localhost:8080/IframeTest.html);
the problem is that now, my browser asks for authentication, while my Iframe should already have this authentication using cookies retrieved in the first step of the code. I must be doing something wrong, but cant' figure out what... any help much appreciated.
thanks
php curl iframe session-cookies
add a comment |
up vote
0
down vote
favorite
Been searching for a while without sucess for a solution. Also, I'm really not experimented in php and REST API, so apologies for any newbie mistakes.
I need to
1) send a POST request to a server and get cookies associated to my login
2) reuse these cookies with url based on that same server, which runs a report.
To perform this I use php and cURL. The below script works fine:
<?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);
?>
I get the expected return:
"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, 22 Nov 2018 13:46:01 GMT Connection: close"
now instead I would like to embed $reporturl into an Iframe. I've built the following 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>
My Iframe runs correctly when I call its url.
I am now calling the Iframe url in my code. so
curl_setopt($ch, CURLOPT_URL, $reporturl);
becomes
curl_setopt($ch, CURLOPT_URL, http://localhost:8080/IframeTest.html);
the problem is that now, my browser asks for authentication, while my Iframe should already have this authentication using cookies retrieved in the first step of the code. I must be doing something wrong, but cant' figure out what... any help much appreciated.
thanks
php curl iframe session-cookies
why you have 415 Unsupported Media Type in curl answer? AND stackoverflow.com/questions/3757071/php-debugging-curl
– bato3
Nov 22 at 14:51
thanks for your comment. using CURLOPT_VERBOSE, I see that cookies are correctly added and set when calling initial value of $reporturl is used. when changing the value of $report_url to localhost:8080/IframeTest.html, cookies are not retrieved.
– Petethet
Nov 26 at 10:01
Did you check what is incookie_jar
file? Cookies have rightpath
? And it probably means you're using the same CURL instance and you're re-adding cookie_jar. (and CURLOPT_RETURNTRANSFER)
– bato3
Nov 29 at 10:28
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Been searching for a while without sucess for a solution. Also, I'm really not experimented in php and REST API, so apologies for any newbie mistakes.
I need to
1) send a POST request to a server and get cookies associated to my login
2) reuse these cookies with url based on that same server, which runs a report.
To perform this I use php and cURL. The below script works fine:
<?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);
?>
I get the expected return:
"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, 22 Nov 2018 13:46:01 GMT Connection: close"
now instead I would like to embed $reporturl into an Iframe. I've built the following 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>
My Iframe runs correctly when I call its url.
I am now calling the Iframe url in my code. so
curl_setopt($ch, CURLOPT_URL, $reporturl);
becomes
curl_setopt($ch, CURLOPT_URL, http://localhost:8080/IframeTest.html);
the problem is that now, my browser asks for authentication, while my Iframe should already have this authentication using cookies retrieved in the first step of the code. I must be doing something wrong, but cant' figure out what... any help much appreciated.
thanks
php curl iframe session-cookies
Been searching for a while without sucess for a solution. Also, I'm really not experimented in php and REST API, so apologies for any newbie mistakes.
I need to
1) send a POST request to a server and get cookies associated to my login
2) reuse these cookies with url based on that same server, which runs a report.
To perform this I use php and cURL. The below script works fine:
<?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);
?>
I get the expected return:
"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, 22 Nov 2018 13:46:01 GMT Connection: close"
now instead I would like to embed $reporturl into an Iframe. I've built the following 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>
My Iframe runs correctly when I call its url.
I am now calling the Iframe url in my code. so
curl_setopt($ch, CURLOPT_URL, $reporturl);
becomes
curl_setopt($ch, CURLOPT_URL, http://localhost:8080/IframeTest.html);
the problem is that now, my browser asks for authentication, while my Iframe should already have this authentication using cookies retrieved in the first step of the code. I must be doing something wrong, but cant' figure out what... any help much appreciated.
thanks
php curl iframe session-cookies
php curl iframe session-cookies
asked Nov 22 at 13:58
Petethet
212
212
why you have 415 Unsupported Media Type in curl answer? AND stackoverflow.com/questions/3757071/php-debugging-curl
– bato3
Nov 22 at 14:51
thanks for your comment. using CURLOPT_VERBOSE, I see that cookies are correctly added and set when calling initial value of $reporturl is used. when changing the value of $report_url to localhost:8080/IframeTest.html, cookies are not retrieved.
– Petethet
Nov 26 at 10:01
Did you check what is incookie_jar
file? Cookies have rightpath
? And it probably means you're using the same CURL instance and you're re-adding cookie_jar. (and CURLOPT_RETURNTRANSFER)
– bato3
Nov 29 at 10:28
add a comment |
why you have 415 Unsupported Media Type in curl answer? AND stackoverflow.com/questions/3757071/php-debugging-curl
– bato3
Nov 22 at 14:51
thanks for your comment. using CURLOPT_VERBOSE, I see that cookies are correctly added and set when calling initial value of $reporturl is used. when changing the value of $report_url to localhost:8080/IframeTest.html, cookies are not retrieved.
– Petethet
Nov 26 at 10:01
Did you check what is incookie_jar
file? Cookies have rightpath
? And it probably means you're using the same CURL instance and you're re-adding cookie_jar. (and CURLOPT_RETURNTRANSFER)
– bato3
Nov 29 at 10:28
why you have 415 Unsupported Media Type in curl answer? AND stackoverflow.com/questions/3757071/php-debugging-curl
– bato3
Nov 22 at 14:51
why you have 415 Unsupported Media Type in curl answer? AND stackoverflow.com/questions/3757071/php-debugging-curl
– bato3
Nov 22 at 14:51
thanks for your comment. using CURLOPT_VERBOSE, I see that cookies are correctly added and set when calling initial value of $reporturl is used. when changing the value of $report_url to localhost:8080/IframeTest.html, cookies are not retrieved.
– Petethet
Nov 26 at 10:01
thanks for your comment. using CURLOPT_VERBOSE, I see that cookies are correctly added and set when calling initial value of $reporturl is used. when changing the value of $report_url to localhost:8080/IframeTest.html, cookies are not retrieved.
– Petethet
Nov 26 at 10:01
Did you check what is in
cookie_jar
file? Cookies have right path
? And it probably means you're using the same CURL instance and you're re-adding cookie_jar. (and CURLOPT_RETURNTRANSFER)– bato3
Nov 29 at 10:28
Did you check what is in
cookie_jar
file? Cookies have right path
? And it probably means you're using the same CURL instance and you're re-adding cookie_jar. (and CURLOPT_RETURNTRANSFER)– bato3
Nov 29 at 10:28
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432605%2fphp-using-curl-unable-to-pass-cookies-to-iframe%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
why you have 415 Unsupported Media Type in curl answer? AND stackoverflow.com/questions/3757071/php-debugging-curl
– bato3
Nov 22 at 14:51
thanks for your comment. using CURLOPT_VERBOSE, I see that cookies are correctly added and set when calling initial value of $reporturl is used. when changing the value of $report_url to localhost:8080/IframeTest.html, cookies are not retrieved.
– Petethet
Nov 26 at 10:01
Did you check what is in
cookie_jar
file? Cookies have rightpath
? And it probably means you're using the same CURL instance and you're re-adding cookie_jar. (and CURLOPT_RETURNTRANSFER)– bato3
Nov 29 at 10:28