Google reCaptcha implementation; response is always NULL - php

I have the following code for recaptcha veryfication.
$curl = curl_init();
$postfields = array(
'secret' => '------Private Key------',
'response' => $_POST['g-recaptcha-response'],
);
var_dump($_POST['g-recaptcha-response']);
$options = array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://www.google.com/recaptcha/api/siteverify',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $postfields,
);
curl_setopt_array($curl, $options);
$response = json_decode(curl_exec($curl), true);
The problem is that the response I get is always NULL, no matter what. Can somebody help me with this? Thanks

Related

How to correctly output specific cURL response from POST request

I'm running a cURL post request that is appearing to hit the url and return a response. However, I'm led to believe that I'm not handling the request output properly and it's throwing an error that I don't understand. I'm extremely new to cURL and if anyone could point me in the right direction, I'd appreciate it.
$curl = curl_init();
$headers = array(
'Content-Type: application/json',
'X-Requested-With: XMLHttpRequest',
);
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'XXX',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => [
client_id => 'XXX',
client_secret => 'XXX',
member_id => 'XXX'
],
CURLOPT_HTTPHEADER => $headers,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC
]);
$resp = curl_exec($curl);
curl_close($curl);
var_dump(json_decode($resp, true));
This results in the errors that follows:
{"errors":{"":["Input string '--------------------------cec6101ba64bcb7f' is not a valid number. Path '', line 1, position 42."]},"title":"One or more validation errors occurred.","status":400,"traceId":"80031695-0002-ef00-b63f-84710c7967bb"}
$curl = curl_init();
$headers = array(
'Content-Type: application/json',
'X-Requested-With: XMLHttpRequest',
);
//////
Adding this and using json_encode allowed for the request to be properly sent with the required id's and secret tokens.
//////
$data = array(
"ClientId" => "$api_clientId" ,
"ClientSecret" => "$api_clientSecret",
"MemberId" => "$api_memberId"
);
$data_string = json_encode($data);
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://cors-anywhere.herokuapp.com/https://staging.micromerchantsystems.com/authenticationservice/api/GateKeeper/createtoken',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $data_string,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC
]);
$resp = curl_exec($curl);
curl_close($curl);
var_dump(json_decode($resp, true));

add basic authorization to php curl script

How do I add basic authorization to my below php script? I am trying to send data to an API
$ch = curl_init();
$token = 'eyJpZCI6MTExOCwiZW52IXXXXXXXXJzaWQiOjYsImFsZyI6IkhTMjU2In0.eyJzdWIiOiJhZG1pbkBtaW5leDXXXXXXXTAsImV4cCI6MTU0ODk0ODgxMH0.owTm-ItzXnpqVSFbXXXXXXXXXX';
$authorization = "Authorization: Bearer ".$token; // Prepare the authorisation token
$curlConfig = array(
CURLOPT_URL => "https://api.endpoint.com",
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => array(
'From' => 'HI',
'To' => '024XXXXXXX',
'Content' => 'hello there'
)
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);
echo $result
I guess this will work (I'm not sure, as I never used Authorization)...
$ch = curl_init();
$token = 'eyJpZCI6MTExOCwiZW52IXXXXXXXXJzaWQiOjYsImFsZyI6IkhTMjU2In0.eyJzdWIiOiJhZG1pbkBtaW5leDXXXXXXXTAsImV4cCI6MTU0ODk0ODgxMH0.owTm-ItzXnpqVSFbXXXXXXXXXX';
$curlConfig = array(
CURLOPT_URL => "https://api.endpoint.com",
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => array(
'From' => 'HI',
'To' => '024XXXXXXX',
'Content' => 'hello there'
)
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: Bearer ".$token
));
curl_close($ch);
echo $result
That's it?

Cannot properly login to a website using PHP cURL

I've been stumped on why my PHP cURL request is not properly signing me in to a login website (for non-personal reasons I won't disclose which one).
This is my code:
$user = "TestUsername";
$pass = "TestPassword";
$curl = curl_init();
$curl_config = array(
CURLOPT_URL => "http://website_name.com/form_action_url",
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => array('user' => urlencode($user),
'pass' => urlencode($pass)
),
CURLOPT_HEADER => true,
);
curl_setopt_array($curl, $curl_config);
$response= curl_exec($curl);
Can anyone figure out what I'm doing wrong? Thanks in advance.

PHP Box API revoke token : Client id was not found in the headers or body

When trying to revoke the oauth access_token for Box, I get the error : Client id was not found in the headers or body
This is the curl-command (which works fine) :
curl https://api.box.com/oauth2/revoke -d 'client_id=CLIENT_ID&client_secret=CLIENT_SECRET&token=access_token' -X POST
When trying the same with php curl, I get the error.
<?php
$revokeurl="https://api.box.com/oauth2/revoke";
$dataq = array(
'client_id' => $client_id,
'client_secret' => $client_secret,
'token' => $access_token
);
$dataqjson= json_encode($dataq);
$headers=array(
"Content-Type: application/json"
);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $revokeurl,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $dataqjson,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_SSL_VERIFYHOST => FALSE
));
$response = curl_exec($curl);
$json_response = json_decode( $response, TRUE );
curl_close($curl);
?>
Why is my POST with json body not correct ?
Data should be set as application/x-www-form-urlencoded rather than JSON-encoded. Example:
<?php
$revokeurl = "https://api.box.com/oauth2/revoke";
$dataq = array(
'client_id' => 'client_id',
'client_secret' => 'client_secret',
'token' => 'access_token',
);
$curl = curl_init();
curl_setopt_array(
$curl,
array(
CURLOPT_URL => $revokeurl,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => http_build_query($dataq),
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false
)
);
$response = curl_exec($curl);
$json_response = json_decode($response, false);
print_r($json_response);
curl_close($curl);

Send HTTP post request to ASP.Net Web API using PHP

<html>
<body><input type="button" id="send" value="Send"></body>
</html>
There'll be a button in my html,
When it's clicked, i'd like to connect to http://localhost:81/Help/Api/POST-Login
which is my api.
Then i'd like to post data to it.
Pretty confused now, any ideas guys. Thanks in advance
<?php
$url = "http://localhost:81/Help/Api/POST-Login";
$data = array(
'message' => 'hi,
mobile' => 12345678,
);
$options = array(
'http' => array(
'method' => 'POST',
'content' => json_encode( $data ),
'header'=> "Content-Type: application/json\r\n" .
"Accept: application/json\r\n"
)
);
$context = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
$response = json_decode( $result );
Use the below code to get the response from .NET API:
$url = "http://localhost:81/Help/Api/POST-Login";
$data = array( 'message' => 'hi, mobile' => 12345678,
);
$options = array(
CURLOPT_CUSTOMREQUEST => "POST", //set request type post or get
CURLOPT_POST => false, //set to GET
CURLOPT_COOKIEFILE => "cookie.txt", //set cookie file
CURLOPT_COOKIEJAR => "cookie.txt", //set cookie jar
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $data
);
$ch = curl_init($url);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
$err = curl_errno($ch);
$errmsg = curl_error($ch);
$header = curl_getinfo($ch);
curl_close($ch);
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
echo $header['content'];

Categories