cgee cgee - 1 month ago 14
PHP Question

Get a return value with cURL between two websites

I've two websites and I can transfer strings with cURL from website one to website two. All fine.

But now I want to transfer a security key from website one to website two and check in website two if the key is in the database of website two.

If yes -> return true

If no -> return false

That is my code:
This code transfer the key string with cURL to the other website.

function dmd_check_key($arg){
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://example.de/link/key_generator.php?key='.$arg.'',
CURLOPT_USERAGENT => 'Codular Sample cURL Request'
));
$resp = curl_exec($curl);
curl_close($curl);
return $resp;
}


This code is in website two and check if the security key is in the database.

function dmd_key_check($key){
if(isset($_GET['key'])){
// Initialize
$database = new medoo([
'database_type' => 'mysql',
'database_name' => 'xxx',
'server' => 'localhost',
'username' => 'xxx',
'password' => 'xxx',
'charset' => 'utf8'
]);

$datas = $database->select("keys", [
"dmd_key",
"id"
], [
"dmd_key[=]" => $key
]);

if($datas[0]['dmd_key'] == $key){
print 'true';
}else{
print 'false';
}
}
}


You can see the last if-clause. If the key string is equal the string in the database set return true.. if not.. return false.

Now my question is.
How can I get the return value from website two?

Here my AJAX form:

<script type="text/javascript">
jQuery(document).ready(function() {
//click event f�r submit button
jQuery('.dmd_admin_buttons').click(function () {

//Daten von den HTML Feldern in JS-Vars �bersetzen
var post = jQuery('input[name=post]');
var page = jQuery('input[name=page]');
var searchbox = jQuery('input[name=searchbox]');
var formidable = jQuery('input[name=formidable]');
var key = jQuery('input[name=key]');

if ( jQuery( post ).prop( "checked" ) )
post.val(1);
else
post.val(0);

if ( jQuery( page ).prop( "checked" ) )
page.val(1);
else
page.val(0);

if ( jQuery( searchbox ).prop( "checked" ) )
searchbox.val(1);
else
searchbox.val(0);

if ( jQuery( formidable ).prop( "checked" ) )
formidable.val(1);
else
formidable.val(0);

var data = 'post=' + post.val() + '&page=' + page.val() + '&searchbox=' + searchbox.val() + '&formidable=' + formidable.val() + '&key=' + key.val();

jQuery.ajax({
url: "<?php echo plugins_url( 'saveData.php', __FILE__ ); ?>",
type: "POST",
data: data,
success: function(data) {
alert(data);
}
});
return false;
});
});
</script>

Answer

website two should print a strings like

function dmd_key_check($key){
    if(isset($_GET['key'])){
        // Initialize
        $database = new medoo([
                'database_type' => 'mysql',
                'database_name' => 'xxx',
                'server' => 'localhost',
                'username' => 'xxx',
                'password' => 'xxx',
                'charset' => 'utf8'
        ]); 

        $datas = $database->select("keys", [
                "dmd_key",
                "id"
        ], [
                "dmd_key[=]" => $key
        ]); 

        if($datas[0]['dmd_key'] == $key){
            print json_encode(array('status'=>true));
        }else{
            print json_encode(array('status'=>false));
        } 
    }
}

website one should check $resp

function dmd_check_key($arg){
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_URL => 'https://example.de/link/key_generator.php?key='.$arg.'',
        CURLOPT_USERAGENT => 'Codular Sample cURL Request'
    ));
    $resp = curl_exec($curl);
    curl_close($curl);

    $resp = json_decode($resp);
    if($resp['status'] == 'true'){
         //do some thing for true
    }else{
         //do some thing for false
    }

}
Comments