POST with file_get_contents in PHP with Captcha - php

I want to read an external site after submit code with captcha. But using (PHP) file_get_contents the captcha is loaded 2 times, so its wrong.
The Form load an image to solve the captcha and sent via POST to the original site that show the result. Normally I solve the captcha and than, in the new site, I read (with my eyes) the content.
Now I want to let PHP read the content. I always solve first the captcha (in my page) and than send it to the other site and let (php) "file_get_contents" read the content.
HERE MY CODE..
<?php
$captcha = $_POST["captcha"];
?>
<html>
<form method="post" action="(this page)">
<img src="remote-captcha"><input type="text" name="captcha">
<submit>
<?php
$postdata = http_build_query(
array(
'captcha' => $captcha
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('https://www.form-page', false, $context);
$search = '/<div class="myclass">(.*)<\/div>/';
preg_match($search, $result, $show);
echo $show[0];
?>
At this point the form is loaded 2 times so the captcha is wrong.
Can someone help me?

Related

Controle if posted api link started

I have application which post data to another site(running other application) with API link, and now I need returned feedback like "Application started!" or "error".. I tried to control variable $result but It returns me nothing. Application is started and everything works fine if I visit link with posted results.
If I visit API link manually, it just give me blank page(runned).
proces.php
<?php
if(isset($_POST['submit'])) {
$ssId = $_POST['ssId'];
$red = $_POST['red'];
$id = $_POST['id'];
$user = $_POST['user'];
$postdata = http_build_query(
array(
'ssId' => $ssId,
'red' => $red,
'id' => $id,
'user' => $user
)
);
$opts = array('http' =>
array(
'user' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('https://apiLink.com/someKey&ssId='.$ssId.'&red='.$red.'&id='.$id.'&user='.$user.'', false, $context);
header("Location: startApp.php");
}
?>
Problem is your current code handles request using $_POST however links trigger http 'GET' method instead of 'POST', also you have some additional checks like $_POST['submit'], hence links redirect fails to work with your logic.
If you replace $_POST with $_REQUEST then things should work with links as well provided you send all the relevant parameters along with it. Other option is just give a button enclosed in form with hidden input fields.

Redirect in php server side after http request

I am very new to php so this question might be trivial.
I am trying to understand if it is possible to redirect the browser from php server side to the page returned by HTTP request.
I have a HTTP Post request looking like so:
$postdata = http_build_query(
array(
"someData" => "data" ,
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'content' => $postdata,
'header' =>
"Cookie: someCookie" .
'content-type: application/x-www-form-urlencoded; charset=utf-8'
)
);
$context = stream_context_create($opts);
$result = fopen('myWebsiteUrl', 'r', false, $context);
var_dump(stream_get_contents($result));
In the post I am being redirected to a different page with Get.
I am trying to force the browser to move to the 'redirected' page. With the above code I am getting back the html of the redirected page but what I'm after is an actual redirect.
The option of retrieving the redirect URL and doing the redirect myself in PHP doesn't work because the Get request has to happen within the same session as the Post.
header('Location: whereveryouwantogo.php?get=123456');
does a redirect for you with a GET-parameter attached. I'm not sure if I really got your question right, but I'm pretty sure you could do what you want/need with the header-command.

how to use another website to make a query with php?

I want to use sci-hub.cc to download scientific papers, but it is in russian and it is not easy to use.
It has a textfield for enter DOI or paper link.
Now, I want to make a website that, when users insert paper link, send this link to sci-hub as a query and then download it. How can I do this in php?
Thanks a lot.
i dont know how your page is working but what you looking for is a "POST" Request with.
The code below "does" the same if you type in something in the field on your page:
<php
$url = 'http://sci-hub.cc/';
$data = ['Request'=>'TEXT FOR FIELD'];
$options = array('http' => array(
'method' => 'POST',
'content' => http_build_query($data),
'header'=> "Content-Type: application/x-www-form-urlencoded \r\n")
);
$context = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
?>

How to make an HTTP request to a form in a web page in php

What I am trying to achieve is:
I have a web site to which I have full source code access. The pages in this web site has been created using velocity templates and I have a page with the following form.
<h3>form data</h3>
<form action="$portalPath/test" method="post">
<input type="text" name="text" value="$!self.getTextFromFormData()" />
<input type="submit" />
</form>
Now from another application written in php, I want to make an http request to this page and get a file downloaded. (Which is an html file). To do that, I wrote following code from the other web application :
$url = 'http://localhost/portal/default/test';
$data = array('filename.html');
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);
But the result shows the html source of the template I access(i.e. test) and not the html file I want to download. What I want to do is to make an http request to auto enter the file name to the form and make the form auto submit the request and process it and get the required html file downloaded as the result. I don't know if this is possible or if possible whether this is the correct way. If this can be done using curl, that's better. Any idea will be highly appreciated.
See: how can I post an external form using PHP?
So, from the referenced URL:
<?php
$url = 'http://localhost/portal/default/test';
$fields = array(
'text'=>urlencode($value_for_field_text),
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
// Initialize curl
$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);
// Results of post in $result
?>

Retrieving a PHP file using file_get_content(), cURL() or fopen() all return PHP code itself

I'm retrieving a PHP file using file_get_content(), cURL() or fopen() all return PHP code itself. It's not retrieving just the HTML code itself that PHP is outputting. How can I overcome this please?
It's basically generating an HTML newsletter preview for my customer that is generated from their CMS. However I want them to be able to copy and paste the HTML it generates into Mailchimp or such like. It just fetches the entire PHP source code though as well as the HTML :(
Hope you can help.
Thanks in anticipation.
Pete
Trying to read a local PHP file will return its source code, since its a "local" file, and is not parsed by the server.
If you wish to read the result of the PHP file you must request it from the server.
Meaning instead of doing:
$data = file_get_contents('x.php');
You should be doing
$data = file_get_contents('http://mysite.com/path/x.php');
Which will be parsed in the server, and then returned to you.
Cheers! :)
Shai.
This will work in localhost. Try this one: $url = "http://localhost/box.php";
function isAllBoxContents($boxid,$pageNum)
{
$postdata = http_build_query( array('myBoxid' => "$boxid", 'isAllBoxContents' => "all",'pageNum' => "$pageNum",'innerReq' => true ));
$opts = array('http' => array( 'method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $postdata ));
$context = stream_context_create($opts);
$url = "http://localhost/box.php";
$result = file_get_contents( $url, false, $context);
$cacheFileName = "isAllBoxContents".$boxid.$pageNum;
cacheMe($cacheFileName,$result);
}

Categories