How do you use PHP to duplicate form fields? - php

I currently have a clients landing page set up so that it posts any new signups to Salesforce. The information primarily goes through to my own system which requires specific form field id's. Salesforce also requires specific form field id's. To do this I have created a second set of form fields that are hidden, and I'm using javascript to set the value of the hidden fields to the same as the visible ones. The only problem with this is that if javascript isn't enabled on the end-users browser, then most of the information doesn't go through to Salesforce.
Is there an alternative way of doing this in PHP?
I'm not an expert with PHP, and my thinking was to post the data to an interrupt page, echo the value of the visible fields in the hidden fields, and then use a header redirect to go to the normal script.
Any suggestions would be greatly appreciated

I wont show you the form as I'm aware you already have one set up
But here is how I would approach this using cURL
$url = 'http://salesforceurl.here';
$post_data = "first_name={$_POST['fname']}&last_name={$_POST['lname']}&email={$_POST['email']}&phone={$_POST['telephone']}&country={$_POST['country']}&description={$_POST['comments']}";
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec( $ch );

If you want to avoid having to rely on anything front-end like JavaScript, then cURL is going to be your best bet. With this, you would eliminate the hidden fields. When the user submits the form, your PHP script would gather the information, repackage it for SalesForce, and then POST it to their form script as if entering it directly into their form. This also means that your server is doing the POST rather than the user's browser, so the interaction with SalesForce ends up being completely transparent to the user.

Related

I want to autofill the username & password in external URL in PHP in a iFrame

I am stuck it in for 2 days I cant find any solution regarding this problem
The main problem is that I want to autofill the username & password from the when I click on the button
I searched so many solution in Stackoverflow but didn't helped me
I have a button when I click on it and iFrame popup loading the external URL. URL of the site which is loading is "https://bkwins.org/" in it successfully
Now I want to autofill the username & password of the site.
Can this be Acheiveable through the PHP curl methods?
Here is the following code
$url = 'https://bkwins.org/';
$myvars = "Hi";
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $myvars);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec( $ch );
No.
To populate the form you need to either:
Run JavaScript on the page the form exists on.
You can't do this from your website due to the same origin policy
A browser extension could do this, but you don't appear to be writing one
Modify the HTML source code of the page
It isn't your site, so you can't do that.
You could fetch the page using PHP/cURL, modify the HTML, then serve the modified HTML to the user from your own site but that wouldn't be coming from the third party site.

sending queries to remote servers [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am trying to create a php code that will provide all the information that we need in one place.
Some of the information that we need is on https://link
I am able to sending queries directly to it using a php form but the only trouble is it only works after someone has performed a manual search. Once someone has search for something then it works until the browser is closed.
Once a user closes the browser, then it start returning errors again.
try opening this link without doing a manual search. https://link
that would give an error, now open the first link https:// and then search for something.
now open this link again: https://searchType=singleQuery&phrase=exact&keywords=02284065
that should not give an error.
Came someone explain to me whats going on and how I can work around this?
Thanks.
edit:
I do not have access to the code on the remote server. on my end I am currently using
<?php
echo file_get_contents(searchType=singleQuery&phrase=exact&keywords=02284065");
?>
to test out. once I solve this problem then I would develop the code further.
edit: this is what I have so far, this returns the form but does not submit the form.
<?php
$url='https:/';
$keywords ='';
$ch = curl_init() ;
curl_setopt( $ch, CURLOPT_URL, "$url" );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
$data = array(
'keywords' => "$keywords",
'Search' => 'submit'
);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $data );
$output = curl_exec( $ch );
curl_close( $ch );
echo "$output\n";
?>
It's quite likely that you're trying to use this service in an improper way. These days, many portals just accept queries (and POST requests, to be more precise) from a "whitelist" of clients. A client in this whitelist could be the website itself, or an authorized mobile app.
You should learn something about the Same Origin Policy, as a "basic" of what we're talking about.
Usually, it's up to the site owner deciding who will be able to query the server, and who will not.
Solution: You should contact the healthinfo.moh.gov.on.ca administrators and ask them if they have some APIs, in order to get your data in a more structured and correct way.
EDIT: probably you can do your search after a manual one on the website because of some session data. After that, the session expires and you can't continue.
Agree with Francesco - esp. about the misuse and contacting the provider - but would add, for educational purposes:
Try using a cURL request instead of file_get_contents.
php doc examples
A snippet I happen to be working with right now:
public function triggerMessage(Contact $tocontact, $msgid)
{
$msgurl = 'https://www.server.com/t/?' . $msgid;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $msgurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, count(get_object_vars($tocontact)));
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->contactToUrlParams($tocontact));
$result = curl_exec($ch);
curl_close($ch);
return $result;
}

post data and captcha to a site with php

I am trying to post data to a url from php. I have checked codes like below from this forum.
$url = 'http://www.someurl.com';
$myvars = 'myvar1=' . $myvar1 . '&myvar2=' . $myvar2;
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $myvars);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec( $ch );
But my target site has a captcha as well.
How do I load the captcha on my php and submit it with captcha answer?
I had a look at the link. Didn't get the final solution though. How to retrieve captcha and save session with PHP cURL?
also How to retrieve captcha and save session with PHP cURL? and http://hungrycoder.xenexbd.com/general/how-to-submit-a-form-using-php-curl-fsockopen.html
My target site has code like below
<form method="post" action="action_form.php" name="frm_sms" id="frm_sms"> < input type="hidden" name="nav" value="sms" />
I don't need to bypass the captcha but I only want to load a neat form and captcha img but no other junk and submit info with captcha answer.
You will have to first fetch the captcha image using cURL and save the contents of any session cookie that it sets, or note the captcha identifier (depends on how their captcha works) and then save a copy of the image to your server.
Then in your form display the copy of the captcha to your visitor. Add an input field for them enter the captcha solution add their input to your $myvars string, and send the cookie or captcha id that was set with the image along with the form post.
That's one way you can forward their captcha on to your user. Or maybe they have an API you can sign up for.

Fill out a form automatically using curl and php

I am trying to write a script that fills out forms automatically and then automatically presses the submit button.
I have read that you can use curl to post HTTP requests, but what do you do when the form handles the post request with JavaScript, like the code below?
<form name="myform" action="javascript:void(0)" method="POST" onsubmit="return fancy_function(this)">
Based off your answer to the comments above, what you want to do is not "fill out the form" with curl, but rather post the completed form to the same page the form would normally send to.
This is also called cross-site posting, and many developers specifically test for and don't allow it to improve security. This will also be much much harder if the form has a captcha.
Assuming that it still makes sense to do (I've used this technique before for a newsletter signup form), here's what you want to do:
To get the form's action url, you'll need to look through the Javascript code for the site and find where funcy_function() is defined. Then in the body of the function, you'll likely see the destination url. You'll also need to note the specific names of the form variables in the html. You'll need to setup your variables with the exact same names. Then your curl setup will look like this:
$url = 'http://www.targeturl.com';
$myvars = 'myvar1=' . $myvar1 . '&myvar2=' . $myvar2;
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $myvars);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec( $ch );
This will send the post variables to the specified url, imitating what would normally happen from the form. The html that the page returns will be in $response if you want to parse or use it.
If you want to use cURL:
You can capture every HTTP request (GET or POST forms) with Firefox Web-Tools:
Web-Tools -> Network -> send your form -> context menu of the form URL (right click on form script) -> Copy => copy as cURL address
More infos: https://everything.curl.dev/usingcurl/copyas

How do you do HTML form testing without real user input simulation?

this question is like this one, except it's for PHP testing via browser. It's about testing your form input.
Right now, i have a form on a single page. It has 12 input boxes. Every time i test the form, i have write those 12 input boxes in my browser.
i know it's not a specific coding question. This question is more about how to do direct testing on your form
So, how to do recursive testing without consuming too much of your time ?
I think Selenium Remote Control is one of the most popular names in the field of web interface testing. See for example this question.
If you don't want to use some big programs for test just one small form - you can use your own testing bike :)
$args = array(/* Your _POST params */)
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); // Your local|remote url
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
// Parse the response here
// You may specify the loop with need args for your 12 checkboxes

Categories