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










share|improve this question






















  • 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 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















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










share|improve this question






















  • 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 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













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










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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


















  • 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 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
















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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

Alexandru Averescu

Trompette piccolo