admin管理员组文章数量:1279237
I would like make a script using PHP (probably need JS) to send POST data to another webpage and get the result back.
For example, Domain A will have a form with a textbox and submit button, and Domain B will have a script which will fill the textbox and press the submit button and return the generated HTML page.
I would like make a script using PHP (probably need JS) to send POST data to another webpage and get the result back.
For example, Domain A will have a form with a textbox and submit button, and Domain B will have a script which will fill the textbox and press the submit button and return the generated HTML page.
Share Improve this question edited Aug 20, 2012 at 21:00 Ryan Kohn 13.5k14 gold badges59 silver badges81 bronze badges asked Jun 14, 2012 at 9:32 sczdavossczdavos 2,06311 gold badges38 silver badges71 bronze badges 5- 1 Using JS: you might be able to post a form to another website, you CANNOT get generated HTML. You need a server side form submission proxy. – Salman Arshad Commented Jun 14, 2012 at 9:33
- possible duplicate of Scrape a website (javascript website) using php – Quentin Commented Jun 14, 2012 at 9:34
- Ok. Is there possibility how can I just post a form? – sczdavos Commented Jun 14, 2012 at 9:35
- @sczdavos are you using any javascript library like jquery or prototype? – ryacii Commented Jun 14, 2012 at 9:56
- @ryacii thank for your respond I've already used ppsreejith's solution – sczdavos Commented Jun 14, 2012 at 10:04
3 Answers
Reset to default 6The following lines of code can be written on another php script,
//set POST variables
$url = 'the website from which you need data through post';
$fields = array(
//post parameters to be sent to the other website
'text'=>urlencode($_POST['text']), //the post request you send to this script from your domain.
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
Now $result will contain the text received from the other website.
With JS: for security reasons not. Read on Same origin policy.
With PHP you can do what you want, including POST
ing other servers. For example use CURL.
--- Original 2012 Answer ---
Use jquery; just load the page with a $.ajax() call. If you need to, you can use "jsonp" which works around the restrictions between calling pages from different domains.
The nice thing about using javascript is that you don't need any serverside languages.
--- 2018 edit ---
I realise now that you can't do a POST with jsonp (as it is essentially read-only) so this will only work if the website accepts the form as a GET (ie, the parameters in the URL). Otherwise, best to use Curl or similar as suggested in other answers.
本文标签: phpHow to submit form on an external website and get generated HTMLStack Overflow
版权声明:本文标题:php - How to submit form on an external website and get generated HTML? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741247390a2365144.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论