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!

Tristen
  • 362
  • 1
  • 18
  • Don't you have any headers? – Tharindu Dec 09 '19 at 16:03
  • Is it possibly just to add the protocol to the url: ""http://"? Hope that works – UnpassableWizard Dec 09 '19 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. https://stackoverflow.com/questions/12331224/how-to-include-authorization-header-in-curl-post-http-request-in-php Hope it'll help you – Arlien Dec 12 '19 at 15:06

0 Answers0