PHP POST to API with JSON Body and Auth

512 views Asked by At

I am in the process of creating a super simple PHP page that sends JSON data to an remote API via POST. I am having issues with it and hoping someone here could help me out.

This is what I have so far

   $url = "https://remoteHost.com/api/post";
   $json = '{"message":"textHere", "user":"userABC"}';
   $ch = curl_init();
    $timeout = 0;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Authorization: Basic --ACB123-ABC123---ABC123---',
    'Content-Type: application/json',
    'Content-Length: ' . strlen($json),)
    );
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $result = curl_exec($ch);
    curl_close($ch);

    echo $result;

When I execute this nothing happens. The page loads without errors but it looks like the curl isn't actually doing anything. Any Ideas?

0

There are 0 answers