0

Can someone help me as to why my PHP post isn't working?

$url = 'https://example.com/portal/api/create';
$data = array('api_key' => 'my_api_key', 'firstname' => 'test', 'surname' => 'test', 'email' => 'test@test.com', 'notes' => 'notes', 'phonecell' => '123456789');

$options = array(
    'http' => array(
        'header'  => "Content-type: multipart/form-data\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data),
    )
);
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { 
    echo $result; 
}

I've tested this in postman and it works fine but when I send over a PHP post it looks like I get the error 'invalid API key' Should there be any reason that PHP file_get_contents would do anything weird here?

Ahmad Adibzad
  • 501
  • 2
  • 6
  • 14
iLC
  • 347
  • 9
  • 22
  • Why `multipart/form-data`? You're not sending any files – Phil May 09 '18 at 02:09
  • 1
    the doc said i had to make it that for some reason - i think options include images https://www.gymmastersoftware.com/gymmaster-api/#path--portal-api-v1-prospect-create – iLC May 09 '18 at 02:10
  • FYI - With only a single header, you shouldn't need the `\r\n` at the end. I would try using `application/x-www-form-urlencoded` – Phil May 09 '18 at 02:50
  • Try dumping out the `$data` string. Make sure your API key still looks correct – Phil May 09 '18 at 02:51
  • 1
    `http_build_query()` alone is not enough when you want `multipart/form-data` for that some additional action is required see this for more https://stackoverflow.com/questions/4003989/upload-a-file-using-file-get-contents – Vinay May 09 '18 at 03:27

0 Answers0