Invisible recaptcha siteverify - error codes - php

I'm trying to verify my $_POST['g-recaptcha-response'] on https://www.google.com/recaptcha/api/siteverify but i keep getting the following result:
"success": false,
"error-codes": [
"missing-input-response",
"missing-input-secret"
]
My code:
if($has_errors == false) {
$result = file_get_contents( 'https://www.google.com/recaptcha/api/siteverify', false, stream_context_create( array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query( array(
'response' => $_POST['g-recaptcha-response'],
'secret' => variable_get('google_recaptcha_secret', '')
) ),
),
) ) );
var_dump($result);
$result = json_decode($result);
if($result->success == false) {
form_set_error('name', t('Submission blocked by Google Invisible Captcha.'));
}
}
I checked my variable google_recaptcha_secret, it is correct.

I've never seen file_get_contents used to post data like that, I'm not saying it's not possible but I would recommend trying with cURL:
if ($has_errors == false) {
$data = [
'response' => $_POST['g-recaptcha-response'],
'secret' => variable_get('google_recaptcha_secret', ''),
];
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($curl);
$error = !$result ? curl_error($curl) : null;
curl_close($curl);
var_dump($result);
$result = json_decode($result);
if ($error || $result->success == false) {
form_set_error('name', t('Submission blocked by Google Invisible Captcha.'));
}
}

Related

How to avoid saturing frecuency capping at my instagram application?

I am using instagram api to search specific hashtag getting top media and recent media but graphic shows 4 different calls, so the the 200 limit per hour are consumed really fast. I know about ig_hashtag_search , top_media and recent_media but what i dont know what is shadowIGHastag.
Is there a way to avoid overconsumption of my app?
This is how i use the api
function insthashtag()
{
include "../insta/define.php";
function makeApiCall($endpoint, $type, $params)
{
$ch = curl_init();
if ('POST' == $type) {
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_POST, 1);
} elseif ('GET' == $type) {
curl_setopt($ch, CURLOPT_URL, $endpoint . '?' . http_build_query($params));
}
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
$hashtag = 'sedapal';
$hashtagId = '17843308429009249';
$hashtagSearchEndpoint = ENDPOINT_BASE . 'ig_hashtag_search';
$hashtagSearchParams = array(
'user_id' => $instagramAccountId,
'fields' => 'id,name',
'q' => $hashtag,
'access_token' => $accessToken
);
$hashtagSearch = makeApiCall($hashtagSearchEndpoint, 'GET', $hashtagSearchParams);
/* To get hashtagID */
/* echo '<pre>';
print_r($hashtagSearch);
die(); */
$hashtagDataEndpoint = ENDPOINT_BASE . $hashtagId;
$hashtagDataParams = array(
'fields' => 'id,name',
'access_token' => $accessToken
);
$hashtagData = makeApiCall($hashtagDataEndpoint, 'GET', $hashtagDataParams);
$hashtagTopMediaEndpoint = ENDPOINT_BASE . $hashtagId . '/top_media';
$hashtagTopMediaParams = array(
'user_id' => $instagramAccountId,
'fields' => 'id,caption,children,comments_count,like_count,media_type,media_url,permalink',
'access_token' => $accessToken
);
$hashtagTopMedia = makeApiCall($hashtagTopMediaEndpoint, 'GET', $hashtagTopMediaParams);
$topPost = $hashtagTopMedia['data'][0];
$topPost1 = $hashtagTopMedia['data'][1];
$topPost2 = $hashtagTopMedia['data'][3];
/* To get recent data
$hashtagRecentEndpoint = ENDPOINT_BASE . $hashtagId . '/recent_media';
$hashtagRecentParams = array(
'user_id' => $instagramAccountId,
'fields' => 'id,caption,children,comments_count,like_count,media_type,media_url,permalink',
'access_token' => $accessToken
);
$hashtagRecent = makeApiCall($hashtagRecentEndpoint, 'GET', $hashtagRecentParams);
$recentPost = $hashtagRecent['data'][0];
$recentPost2 = $hashtagRecent['data'][1]; */
/* $recentPost3 = $hashtagRecent['data'][2]; */
$return = [$topPost['media_type'], $topPost['media_url'], $topPost1['media_type'], $topPost1['media_url'], $topPost2['media_type'], $topPost2['media_url']];
$jsondata = json_encode($return, JSON_PRETTY_PRINT);
return $jsondata;

Expected a list of items but got type "dict" PHP

I am connecting to an API and got this error when passing the following data format:
$data = array(
"from_date" => "2021-11-10",
"to_date" => "2021-11-21",
"adults" => 1,
"guest_id" => 2954339,
"stay_type" => "GUEST",
"entities" => [ "property_id" => 89835 ]
);
$reservation = fetch_api('reservations', '', 'POST', $data);
The error:
[message] => Expected a list of items but got type "dict".
[code] => not_a_list
The issue is with the entities value. I believe it is because it is an array. Below is my fetch_api function. Which works fine when connecting to other endpoints and sending data without nested array.
function fetch_api($endpoint, $parameters = '', $method = 'GET', $data = null){
$curl = curl_init();
$headers = array(
"accept: application/json",
"Authorization: Token xxxxxx",
"cache-control: no-cache",
"content-type: application/json",
);
if ($method == 'GET') {
curl_setopt($curl, CURLOPT_HTTPGET, 1);
}
if ($method == 'POST') {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
}
curl_setopt($curl, CURLOPT_URL, 'https://www.lodgix.com/public-api/v2/'. $endpoint .'/' . $parameters);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$result = curl_exec($curl);
$result = json_decode($result);
if (!$result) {
die("Connection Failure");
}
curl_close($curl);
return $result;
}
I have tried to json_encode the nested array like below:
if ($method == 'POST') {
curl_setopt($curl, CURLOPT_POST, 1);
if ($data) {
for ($i=0; $i < count($data); $i++) {
if (gettype($data[$i]) === 'array') {
json_encode($data[$i]);
}
}
}
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
}
Unfortunately, it did not help. Am I on the right track in believing the issue is with the nested array or the issue is completely different?

Post JSON with cURL doesn't work

I know this question was asked many times but I didn't find an answer to it.
I have this cURL call:
$curl = curl_init();
$post_data = "{'application_token':'aksjnhksdbfjsbkdjfkjdhf','client_company_name':'TEST COMPANY','client_email':'test#test.ro','client_phone_number':'012000000','client_cui':'1234567','client_chosen_option':'BEST OPTION','registration_number':'36230'}";
curl_setopt($curl, CURLOPT_URL, 'http://website.ro/test/api/test');
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
));
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = json_decode(curl_exec($curl), 1);
curl_close($curl);
var_dump($result);
And in my test method I have this:
function testAction () {
if ($_SERVER['REQUEST_METHOD'] === 'POST'){
return json_encode(
[
'response_type' => 'success',
'response_message' => 'SUCCESS'
]
);
} else {
return json_encode(
[
'response_type' => 'error',
'response_message' => 'ERROR'
]
);
}
}
And my result output is always the error one. Can anyone help me please?

cURL request Spotify API Unexpected status: 415

Here is my script for an auth request to Spotify but it returns an error. I tried changing the Content-Type, but that doesn't seem to cut it. Here is my code:
$spot_api_client = 'client';
$spot_api_secret = 'secret';
$spot_api_redirect = 'myurl';
if(isset($_GET['state']) && isset($_COOKIE['stateKey']) && $_COOKIE['stateKey'] == $_GET['state']){
$ch = curl_init();
$curlConfig = array(
CURLOPT_URL => "https://accounts.spotify.com/api/token",
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => array(
'grant_type' => 'authorization_code',
'code' => $_GET['code'],
'redirect_uri' => urlencode($spot_api_redirect),
),
CURLOPT_HTTPHEADER => array(
'Accept' => '*/*',
'User-Agent' => 'runscope/0.1',
'Authorization' => 'Basic '. base64_encode($spot_api_client.':'.$spot_api_secret),
'Content-Type'=>'application/json'
)
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
}
Well, seems I found the answer based on this question :
$url = 'https://accounts.spotify.com/api/token';
$method = 'POST';
$spot_api_redirect = 'myurl';
$credentials = "client:secret";
$headers = array(
"Accept: */*",
"Content-Type: application/x-www-form-urlencoded",
"User-Agent: runscope/0.1",
"Authorization: Basic " . base64_encode($credentials));
$data = 'grant_type=authorization_code&code='.$_GET['code'].'&redirect_uri='.urlencode($spot_api_redirect);
if(isset($_GET['state']) && isset($_COOKIE['stateKey']) && $_COOKIE['stateKey'] == $_GET['state']){
unset($_COOKIE['stateKey']);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = json_decode(curl_exec($ch), true);
curl_close($ch);
print_r($response);
}
Just in case of node.js / request use, point out "WTF" tag :)
request(
{
method : "POST",
url : "https://accounts.spotify.com/api/token",
json : true,
headers :
{
"Content-Type" : "application/x-www-form-urlencoded", // WTF!
"Authorization" : "Basic " + new Buffer(client_id+":"+client_secret).toString('base64')
},
body : "grant_type=client_credentials"
},
function (err, response, body)
{
if (err)
{
cb(err);
}
else
{
if (body.hasOwnProperty("access_token"))
{
access_token = body.access_token;
cb(null);
}
else
{
cb(body);
}
}
}
);

Convert php curl to GAE urlfetch for iTunes InApp verifyReceipt

Can someone help to convert this PHP Curl to UrlFetch ? This is used for Apple iTunes verifyReceipt
if (getiTunesProductionLevel($app_id)=="sandbox" || $sandbox_override == TRUE) {
$endpoint = 'https://sandbox.itunes.apple.com/verifyReceipt';
}
else {
$endpoint = 'https://buy.itunes.apple.com/verifyReceipt';
}
$postData = json_encode(array(
'receipt-data' => $receipt,
'password' => $sharedSecret));
$ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$response = curl_exec($ch);
$errno = curl_errno($ch);
$errmsg = curl_error($ch);
curl_close($ch);
this is as good as I can get. But not good enough.
logMessage(LogType::Info,"XXX URLFetch 0");
$postData = json_encode(array(
'receipt-data' => $receipt,
'password' => $sharedSecret));
$post_data = json_decode($postData);
logMessage(LogType::Info,"XXX URLFetch 1");
$data = http_build_query($post_data);
logMessage(LogType::Info,"XXX URLFetch 2");
$context = [
'http' => [
'method' => 'post',
'header' => "Content-Type: application/x-www-form-urlencoded\r\n",
'content' => $data
]
];
logMessage(LogType::Info,"XXX URLFetch 3");
$context = stream_context_create($context);
logMessage(LogType::Info,"XXX URLFetch 4");
$result = file_get_contents($endpoint, false, $context);
logMessage(LogType::Info,"XXX result:" . $result);
$response = $result;
$errno = 0;
logMessage(LogType::Info,"XXX response:");
It is able to post but returns this response
XXX result:{"status":21002}
Why do you json_decode $post_data in your urlfetch code but not in curl?
I had an error like this, it turned out 'POST' had to be in uppercase when running locally.
On .appspot it worked with lowercase 'post' but not locally on my PC.

Categories