开发者

How I can do HTTP post to a url called by an Iframe

I want to make a http post to an outside url using php. By outside url I mean the url i not hosted on my servers.The url is called in an iframe. I need to know if this is technically possible to do this.

I tried doing this using curl but curl crea开发者_如何学运维tes its own session with the remote server while I want to use the session which the browser has already created.

Please let me know your thoughts on this.

  <?php

php code to make http post.

        ?>


    <iframe src="outside url to be posted" height="100" width="100"/>

The outside url is google calender, so when I call it, if the user is already logged into google, his calender should display and I need to make a post to the calender using http post to save a calender event.

I hope this makes myself more clear on what am trying to achieve.


Update - Current Answer

After the update to your question, here's a different answer that I think addresses your issue more closely.

I think the question you are asking involves doing things with a user's credentials on another site. This is dancing dangerously close to Cross-site Request Forgery.

If you only do the POSTing when the user requests that you do it, it's a little better (I guess) but still inadvisable.

Why don't you use the Google Calendar API to do what you need?


Previous Answer

You need to tell cURL to use a particular session. Because PHP is managing the session, you'll also need to tell php to stop writing to the session while cURL uses it.

Try this:

$strCookie = 'PHPSESSID=' . $_COOKIE['PHPSESSID'] . '; path=/';
session_write_close();
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt( $ch, CURLOPT_COOKIE, $strCookie );
$response = curl_exec($ch);
curl_close($ch);

$_COOKIE['PHPSESSID'] will be the identifier for your PHP session, and $url will be the URL you've pulled out of the iframe.

This is taken virtually verbatim from this blog post. It was one of the first links on Google, so I didn't do a lot of extra digging.

I've done a bit of messing with cURL and PHP sessions, so this looks right based on what I remember.

Edit:

By the way, you should reference this SO question for the method to do POSTs with cURL. I assume you at least have some idea of how to do this, but there it is in case you need a refresher.

Also (in case it's not clear already), you can run as many

curl_setopt($handle, (CURL OPTION), (CURL VALUE));

lines as you need to configure cURL the way you need it.

e.g.:

  • POST vals
  • Session settings
  • etc., etc.

Good luck!


It's javascript, not php.

<form id="post_form" method="post" target="post_frame">
<input type="hidden name="field1" value="value1>
.... other fields
</form>
<script type="text/javascript">
  document.getElementById("post_form").submit();
</script>
 <iframe name="post_frame" height="100" width="100"/>


right off the file_get_contents man page:

<?php
// Create a stream
$opts = array(
  'http'=>array(
    'method'=>"POST",
    'header'=>"Accept-language: en\r\n" .
              "Cookie: foo=bar\r\n"
  )
);
//put post content into cookie part
$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$file = file_get_contents('http://www.example.com/', false, $context);
?>
<div><?=$file?></div>

not rly an iframe but the same idea

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜