Testing recaptcha on localhost - php

I've been trying to test recaptcha on localhost (xampp with php 5.3); the code below (taken from this answer) works on my remote host and returns 'success':'true', but on localhost { "success": false, "error-codes": [ "missing-input-response" ] } is returned. However, if I change the POST request to a GET request (essentially toggle the comment of the 2 $result = lines), then I get a successful call.
Why does this happen?! Although I'm happy that it works on my actual site, I'd like to understand why GET works but POST doesn't on localhost
<html><head></head><body><script src='https://www.google.com/recaptcha/api.js' async defer>
if(isset($_POST['g-recaptcha-response'])) {
$url = 'https://www.google.com/recaptcha/api/siteverify';
$data = array('secret' => 'my_secret_key',
'response' => $_POST['g-recaptcha-response']);
$options = array(
'http' => array(
'method' => "POST",
'header' => "Content-type: application/x-www-form-urlencoded" . PHP_EOL,
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
//$result = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret=my_secretkey&response=' . $_POST['g-recaptcha-response']);
$result = file_get_contents($url, false, $context);
echo $result;
}
?>
<form method="post" action='recaptchatest.php'>
<input id="test" name="test" />
<div style="left:100px;" class="g-recaptcha" data-sitekey="my_key"></div>
<input type="submit" value="Send" id="submit" class="buttons" />
</form>
</body></html>
For completeness, the output of http_build_query($data) is a correctly formed query string (secret=<my key>&response=<response>) and the output of stream_context_create($options) is Resource id #3

Related

fetching imgur cURL to json_decode php upload form

i'm trying to use imgur as a uploading backend - i guess it secure my website if there's upload picture (is it right?) so
i went with this way :
<?php
$client_id = 'xxxxx';
$file = file_get_contents($_FILES["imgupload"]["tmp_name"]);
$url = 'https://api.imgur.com/3/image.json';
$headers = array("Authorization: Client-ID $client_id");
$pvars = array('image' => base64_encode($file));
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL=> $url,
CURLOPT_TIMEOUT => 30,
CURLOPT_POST => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $pvars
));
if ($error = curl_error($curl)) {
die('cURL error:'.$error);
}
$json_returned = curl_exec($curl); // blank response
echo "Result: " . $json_returned ;
curl_close ($curl);
?>
HTML form
<form action="upload" method="post" enctype="multipart/form-data">
<input type="file" name="imgupload" /><br>
<input type="submit" value="Upload to Imgur" />
</form>
when i click on submit the result is fine as i guess ^^
Result: {"data":{"id":"TvFtE29","title":null,"description":null,"datetime":1585015712,"type":"image\/png","animated":false,"width":900,"height":940,"size":48902,"views":0,"bandwidth":0,"vote":null,"favorite":false,"nsfw":null,"section":null,"account_url":null,"account_id":0,"is_ad":false,"in_most_viral":false,"has_sound":false,"tags":[],"ad_type":0,"ad_url":"","edited":"0","in_gallery":false,"deletehash":"Z9xFH8mrSH8lRDB","name":"","link":"https:\/\/i.imgur.com\/TvFtE29.png"},"success":true,"status":200}
but my problem i want to collect the https://i.imgur.com/TvFtE29.png into specific variable like $uploaded = 'https://i.imgur.com/TvFtE29.png'; to added into my database -> user pic
i went with json_decode but it's not completed with me in the right way, if someone can help with this issue 🌹
thanks.
json_decode worked fine here:
$json_returned = json_decode($json_returned, true);
$uploaded = $json_returned['data']['link'];

Bad Request 400 uploading images to the imgur API

I want to upload some pictures with the imgur-API to imgur, but I am always getting an Error 400 Bad Request. Whats the issue?
<?php
if (isset($_POST['uploadprofileimg'])) {
$image = base64_encode(file_get_contents($_FILES['profileimg']['tmp_name']));
$options = array('http'=>array(
'method'=>"POST",
'header'=>"Authorization: Bearer *MY_ACCESS_TOKEN*\n".
"Content-Type: application/x-www-form-urlencoded",
'content'=>$image
));
$context = stream_context_create($options);
$imgurURL = "https://api.imgur.com/3/image";
$response = file_get_contents($imgurURL, false, $context);
}
?>
<h1>My Account</h1>
<form action="upload-pb.php" method="post" enctype="multipart/form-data">
Upload a profile image:
<input type="file" name="profileimg">
<input type="submit" name="uploadprofileimg" value="Upload Image">
</form>
Looking at the endpoint for /image here, it needs a parameter of image. You're passing it in as content, and not properly encoded as image. Looking here, I borrowed how to properly upload content using stream_context_create/file_get_contents:
<?php
if (isset($_POST['uploadprofileimg'])) {
$image = base64_encode(file_get_contents($_FILES['profileimg']['tmp_name']));
$postdata = http_build_query(
array(
'image' => $image,
)
);
$options = array('http'=>array(
'method'=>"POST",
'header'=>"Authorization: Bearer *MY_ACCESS_TOKEN*\n".
"Content-Type: application/x-www-form-urlencoded",
'content' => $postdata
));
$context = stream_context_create($options);
$imgurURL = "https://api.imgur.com/3/image";
$response = file_get_contents($imgurURL, false, $context);

PHP save form data on reload [duplicate]

This question already has answers here:
how to remember input data in the forms even after refresh page?
(10 answers)
Closed 4 years ago.
So i got this form:
<form action="send.php" method="post">
Dogecoin-address: <input type="text" name="address"/><br>
<input type="submit" name="Submit" value="Submit">
</form>
<?php
// starting the session
session_start();
if (isset($_POST['address'])) {
$_SESSION['address'] = $_POST['Submit'];
}
?>
i want to keep"address"data here: when i reload this:
$url = 'https://faucethub.io/api/v1/send?
api_key=4b21af7e916403216ffb11e523f912bc&currency=DOGE&amount=1&to='.
$_POST['address'];
$data = array('key1' => 'value1', 'key2' => 'value2');
// use key 'http' even if you send the request to https://...
$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);
if ($result === FALSE) { /* Handle error */ }
var_dump($result);
?>
when the user submit his "address" i want his "address"remain/save on the second code when he refreshes!
how is this possible by using $_SESSION?OR by using cookies?or any other way?
i'm new to php and i don't know how to use it
Change your $_POST value to address instead of Submit. This way your address is stored inside the $_SESSION['address'] variable.
You can access the address by using $_SESSION['address'].

$_GET remove string(30) and making it to output what I want only?

I coded this script and when I search for a user it shows
string(30) "result=2014-01-30 12:18:03&b=r"
the result=000000000 0000000&b=r is part of the api I'm using but the string(30) isn't and it outputs that too, how do I remove the string(30) and would I be able to replace the result text with something I choose?
Here's my script, you search a username and it outputs their last logged on date.
<html>
<h2> ----- </h2>
<?php
if (!empty($_GET['userID'])) { //First check
$url = 'http://api-testing.agame.com/php/getLastLog.php';
$data = array('userID' => $_GET['userID']); //Then use the value
// use key 'http' even if you send the request to https://...
$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);
}
?>
<form action='' method='get'>
<input type='text' name='userID' />
<input type='submit' value='Search' />
</form>
</html>
look at var_dump()
it is used for check the value and type of php variables(debugging purpose).
You have correct data in this line :
$result = file_get_contents($url, false, $context);
so just remove this line :
var_dump($result);
Use parse_str():
$result = file_get_contents($url, false, $context);
parse_str($result);
echo $result; // 2014-01-30 12:18:03
or
$result = file_get_contents($url, false, $context);
parse_str($result, $output);
echo $output['result']; // 2014-01-30 12:18:03

How to get JSON in response

When I debug a site via Chrome browser I get JSON response. But when I try to do this via PHP I get an error message.
failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found
Thanks for any help.
For example:
Things to do in Chrome:
Go to page: http://gruper.pl/warszawa and on the bottom you will see a button "Wiecej ofert". After click you will see in a debug:
http://gruper.pl/DataProvider.php?cityId=51&categoryId=0&mainNaviId=1&showBTile=true&page=1
and response:
[{"ID_PAGE":"59199","ID_CITY":"3952","main_city":"3952","date_start":"2014-02-23 18:00:00","date_end":"2014-03-01 23:59:00","price".....
Is there any possibility to get the same in PHP?
My code is:
<?php
$url = 'http://gruper.pl/DataProvider.php?cityId=51&categoryId=0&mainNaviId=1&showBTile=true&page=1';
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n" .
"Accept:application/json\r\n" .
"Accept-Encoding:gzip,deflate,sdch\r\n" .
"X-Requested-With:XMLHttpRequest\r\n",
'method' => 'GET'
),
);
$context = stream_context_create($options);
$result = (file_get_contents($url, false, $context));
?>
<html>
<head>
<meta charset="UTF-8">
</head>
</html>
It looks like that URL will return a 404 HTTP status code unless these headers are set:
X-Requested-With: XMLHttpRequest
Referer: http://gruper.pl/warszawa
So this will work:
<?php
$url = 'http://gruper.pl/DataProvider.php?cityId=51&categoryId=0&mainNaviId=1&showBTile=true&page=1';
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "X-Requested-With: XMLHttpRequest\r\n" .
"Referer: http://gruper.pl/warszawa"
)
);
$context = stream_context_create($options);
$result = (file_get_contents($url, false, $context));
echo $result;
?>

Categories