0

I'm working on a project and am attempting to link a Java application to my website/database via an API. I'm running on an aws ec2 instance. The problem I am encountering is using Curl to POST data to another webpage. (The end-product will require proxies, and I've decided to use curl to manage this.)

The PHP code that makes the request:

<?php
function get_url($url)
{
    $ch = curl_init();

    if($ch === false)
    {
        die('Failed to create curl object');
    }
    // Temporary, just trying to get POST value to work. 
    $data = "password=temp";
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $data2 = curl_exec($ch);
    curl_close($ch);
    return $data2;
}

echo get_url("www.mywebsite.org/API/myscript.php");
?>

The server-side code that handles the request:

<?php
echo "Password: " . $_POST["password"];
?>

The issue I am having is with passing the post variables through curl and retrieving them.

The output on the page is:

Password: 

and the POST variables are not being set. I'm thinking I am missing a CURLOPT somewhere but can't figure out which one!

3
  • Don't you have any headers? Commented Dec 9, 2019 at 16:03
  • Is it possibly just to add the protocol to the url: ""http://"? Hope that works Commented Dec 9, 2019 at 16:46
  • Hello, I had to do same a few months ago, you're gonna need to add authorization header, sorry it's not explained a lot on the internet. Here is another post about it. stackoverflow.com/questions/12331224/… Hope it'll help you Commented Dec 12, 2019 at 15:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.