Tuesday, February 1, 2022

[SOLVED] cURL not working on Google Compute engine

Issue

I'm facing a similar issue to this question on my VM instance that runs WordPress:

href="https://stackoverflow.com/questions/38014596/php-retrieve-facebook-image-in-google-compute-engine">PHP - Retrieve Facebook image in Google Compute Engine

I'm using AJAX to call send_otp.php with mobile_number as a parameter.

My send_otp.php script is:

$mobile_number = $_POST['mobile_number'];
//  Don't do in production, possible RCE
$url = "http://gateway.com/api?numbers=" . $mobile_number. "";
//      Initiate curl
$ch = curl_init();
// Disable SSL verification (Don't do in production)
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL, $url);
// Execute
$result = curl_exec($ch);
// Closing
curl_close($ch);
    
$result = json_decode($result, true);

And my Javascript code is as follows:

cust_mob = jQuery('#number');
jQuery.ajax({
    url: 'send_otp.php',
    type: 'POST',
    dataType: 'json',
    data: {
        mobile_nuber: '' + cust_mob.val().toString() + '',
        security: '<?php echo wp_create_nonce('mobile-validate-nonce'); ?>'
    },
    success: function (response) {
        console.log('success');
    }, error: function (response) {
        console.log('success');
    }
});

I've removed a lot of code just to post it here and everything works fine on my local machine WordPress installation using WAMP.

This code is a part of my theme and all I'm doing is zipping the theme folder and uploading it to WordPress after making changes. Everything works fine except this 'curl' part e.g. login using ajax and updating profile using AJAX is working fine.

Please tell me how to resolve it, or are there any other methods to achieve the same?


Solution

Try this lib: https://github.com/azayarni/purl

I use it in my pet-project that hosted on GAE platform and it works fine.



Answered By - brevis
Answer Checked By - Katrina (WPSolving Volunteer)