post data and captcha to a site with php - 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.

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.

php -- get content of page that requires login and stop it from automatically redirecting?

I'm trying to get the HTML content of a certain page (that requires a login to view) with PHP. I have the login info of course, and I'm even ABLE to login to the page via curl, as the following code demonstrates (I took out the URLs and login info in this question, but it does actually work for me):
<html>
<head>
</head>
<body><?php
$login_url = 'https://www.MyTopSecretDomainPage/login.asp';
$remotePageUrl = 'http://www.MyTopSecretDomainPage/TheOtherPageThatIWantToGetTheContentsOf.asp?variablesThatAreImport=Things';
//These are the post data username and password
$post_data = 'action=login&cookieexists=true&redirect=&page=&partner=&email=someThing#gmail.com&password=TopSecretPassword&userid_to_cookie=1&saveID=yes';
//Create a curl object
$ch = curl_init();
//Set the useragent
$agent = $_SERVER["HTTP_USER_AGENT"];
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
//Set the URL
curl_setopt($ch, CURLOPT_URL, $login_url );
//This is a POST query
curl_setopt($ch, CURLOPT_POST, 1 );
//Set the post data
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
//We want the content after the query
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
/*
Set the cookie storing files
Cookie files are necessary since we are logging and session data needs to be saved
*/
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
//Execute the action to login
$postResult = curl_exec($ch);
//here I'm trying to get the data another page
curl_setopt_array(
$ch, array(
CURLOPT_URL => $remotePageUrl ,
CURLOPT_RETURNTRANSFER => true
));
$output = curl_exec($ch);
//I first echo the $post_result 'cause I need to log in, but that causes me to redirect, not sure how else to do it so I can afterwards get the other data?
echo($postResult);
?>
<script>
</script>
</body>
</html>
most of this code was based on other answers I found here, but those other answers didn't address the problem I am having, which is that after it logs in to the other website successfully, it automatically redirects to that website, even though I turned CURLOPT_FOLLOWLOCATION off, as you can see. I suspect that somehow built into the other website it automatically redirects the user, but I need to still access content from a different page via PHP, and once I'm redirected -- no more php page!!!!!
I think if I can somehow load the login page (and login) on some hidden window or something and after it redirects on that other window, maybe I can then get the content of the other page I want, but I'm not sure if that's the correct solution, or if that would even work at all.
SO: I have successfully logged in to the other page, but how do I actually get the content of a different page on that site, since it redirects me automatically?

Calling $.get after $.post while maintaining the session somehow

I have an API which you can access through web url. You can sign in by POSTing your username/password to let's say
http://myapi.example/signin
I do that with a jQUERY's $.post request which works fine - I get the status OK back if everything is correct. Now I need to somehow GET data from this API with certain url, which I would do with $.get of course, but I get the reponse unauthorized… So probably the session somehow gets lost, after the POST request/response.
I know that this POST/GET works, because if I do signin and then just type the GET url in browser - i get the response text plain in browser.
How would you keep the "session" somehow to simulate browser's behaviour in this?
function ajaxSignIn(){
$.post(url,
{
email: document.getElementById("name").value,
password: document.getElementById("password").value
},
function(data,status){ // this is where POST is successful (i get data in)
console.log("\nStatus of POST: " + status);
console.log(data);
$.get(urlForGet,function(data,status){ // GET call
console.log("Status of GET: " + status);
});
});
}
If I was unclear please ask for more info.
Thank you for any help.
I think that your API does not work as it should or you are not using all the data it provides you. Usually this is how these APIs work:
You send login request.
Server replies with status code and if it is ok sends you a generated key which is valid for some period of time.
If you want to use the api further you need to supply this key with every new request.
Browsers use Cookies, APIs use this.
In case anyone is interested: I ended up sending ajax request to my local php server, where I managed to get a Cookie from the remote API with the use of cURL.
You create a cookie file and then use the cookie with further GET requests …
curl link to cookie explained
Server-side curl part without getting parameters etc etc.:
// here I come from my ajax
$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_COOKIEJAR, $cookieJarFilePath); // this is what you need
curl_setopt( $ch, CURLOPT_HEADER, 1);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
// i get data in response from the server
$response = curl_exec( $ch );
curl_close($ch);
Here you can continue to use GET requests on the same page now with this cookie with the use of:
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookieJar); // other stuff is the same as before

How do you use PHP to duplicate form fields?

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.

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

Categories