1

I have a page on domain A.com that holds a link to a page on B.com (I have no control on B.com).

On B.com the page that opens contains a form identified by

<form class="cont_tab col-xs-12 col-md-8 prenotiamo">

User on A.com clicks on the link, open the page on B.com, fills the form with a fixed username and password and submit it. The result is another page opened that is the real destination of my link. Actually B.com opens in a new tab but its ok also to open it as an iFrame.

Is there a way to automate the form submission?

Lelio Faieta
  • 6,457
  • 7
  • 40
  • 74
  • I did not understand your question – Giacomo M Jul 31 '19 at 13:31
  • @GiacomoM what is not clear on my question? – Lelio Faieta Jul 31 '19 at 13:32
  • What is the actual situation and what are you trying to do. – Giacomo M Jul 31 '19 at 13:33
  • Sure there is, use something like DomCrawler to do this for you. https://symfony.com/doc/current/components/dom_crawler.html#forms – Lulceltech Jul 31 '19 at 13:34
  • as you can read I am trying to automate the form submission. the issue is just that the form is on a different domain and I can't use js since it is a cross site scripting (not allowed) – Lelio Faieta Jul 31 '19 at 13:34
  • @Lulceltech is it going to work on a different domain? – Lelio Faieta Jul 31 '19 at 13:37
  • If Site B does not provide any access or functionality to fill the form, you will have to do it on the server side. Or else try to skip B entirely and directly post from A to C. – Lain Jul 31 '19 at 13:38
  • @lain, no problem to do it on server side. the point is how. And i cannot skip to submit that form to see the target page – Lelio Faieta Jul 31 '19 at 13:39
  • Based on question in context with comments, this is a duplicate of [How do I send a POST request with PHP?](https://stackoverflow.com/questions/5647461/how-do-i-send-a-post-request-with-php) – esqew Jul 31 '19 at 13:41
  • @LelioFaieta Yes, it's what its designed for. In my line of work I use it to scrap other websites, however you can use it to do all sorts of things like submit forms and data to other websites etc. – Lulceltech Jul 31 '19 at 13:41
  • You can send a form to anywhere you like, so you could just have a form on your site with the same fields, and point the action to the “B.com” URL. But this will only worrk, if B does not have any measures in place to actively prevent that - cookies, session, form tokens. (And if that is the case, it probably means they would not want you to do anything like this in the first place.) – misorude Jul 31 '19 at 13:42
  • If you can not control server B, there are not many things you can do. – Giacomo M Jul 31 '19 at 13:42
  • @Lelio Faieta: I do not know php well enough to help you out. But usually you send a request (to B), read the response (from B) and forward it to wherever you need it to go (to C). – Lain Jul 31 '19 at 13:48
  • @esqew yes. This is what I was looking for – Lelio Faieta Jul 31 '19 at 13:54
  • apparently curl is not the way to go since the form submission is handled by a js script on the page itself and it happens by triggering the js on click on the submit button. Thus, CURL is useless – Lelio Faieta Aug 01 '19 at 14:41

1 Answers1

0

If I understand your question correctly, you can try the following suggestion I have below. But this will depend on how B.com is built and what can you take advantage of. My suggestion assumes the url of B.com will change after a successful submission and that it allows requests from any origin.

  1. Open B.com in an iframe. You can display the iframe after the user clicks on the link in A.com
  2. Set the onLoad function for the iframe so you can check whenever the iframe url changes
  3. When the onload function fires, parse the URL to find out if it was success then hide iframe and continue your workflow in A.com

Also, if you create this kind of dependency in your site, it can potentially break at any time since B.com could change their site. So, ultimately, I would not recommend this. If B.com does not have an API you can communicate with, then you are probably not doing something right.

Restnom
  • 124
  • 1
  • 15
  • 1
    _“so you can check whenever the iframe url changes”_ - not possible, the Same Origin Policy prevents that kind of access across domain boundaries. – misorude Jul 31 '19 at 13:43
  • @misorude that is also dependent on how B.com is built. If they allow any origin that it would not be an issue. The workaround I suggested is entirely based on the capabilities of B.com, but I would not recommend it in any production site. – Restnom Jul 31 '19 at 13:49
  • You can not “allow any origin” for access to documents shown in frames. You are probably referring to CORS - but that is about requesting data from different origins via HTTP; accessing content shown in an iframe is a completely different thing. – misorude Jul 31 '19 at 13:58