Post a document with PHP

Discussions

Ask a Question
Back to All

Post a document with PHP

Hello,

I'm trying to Upload a file with PHP. I took the example code from
https://developer.personio.de/reference#auth-1 and
https://developer.personio.de/reference#post_v1-recruiting-applications-documents

$token = NULL;

$curl = curl_init();

$params = [
  'client_id' => 'abc',
  'client_secret' => 'abc',
];

$url = 'https://api.personio.de/v1/auth?' . http_build_query($params);

curl_setopt_array($curl, [
  CURLOPT_URL => $url,
  CURLOPT_RETURNTRANSFER => TRUE,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_HTTPHEADER => [
    "Accept: application/json",
  ],
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
}
else {
  $data = json_decode($response);
  $token = $data->data->token;
}

if ($token) {
  $curl = curl_init();

  //$path = '/var/www/html/web/some.pdf';
  //$post_data[] = curl_file_create($path, 'application/pdf', 'some.pdf');

  curl_setopt_array($curl, [
    CURLOPT_URL => "https://api.personio.de/v1/recruiting/applications/documents",
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    //CURLOPT_POSTFIELDS => $post_data,
    //CURLOPT_POSTFIELDS => [
    //  'token' => $token,
    //  'file' => '@/var/www/html/web/some.pdf',
    //],
    CURLOPT_POSTFIELDS => "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"file\"\r\n\r\ndata:application/pdf;name=Test.pdf;base64,JVBERi0xLjUKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3RoIDMgMCBSL0ZpbHRlci9GbGF0ZURlY29kZT4+CnN0cmVhbQp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DAwMDAgbiAKMDAwMDAwNjA4MCAwMDAwMCBuIAp0cmFpbGVyCjw8L1NpemUgMTQvUm9vdCAxMiAwIFIKL0luZm8gMTMgMCBSCi9JRCBbIDwxQjlGRjNGNkE3MDE5MzhEMzYxMUJFOTQ2NTg0QjFEQj4KPDFCOUZGM0Y2QTcwMTkzOEQzNjExQkU5NDY1ODRCMURCPiBdCi9Eb2NDaGVja3N1bSAvMTk1MThCRDUxQ0Y4MEVGMzc4RUIzRTY0QkE3QkVDNzkKPj4Kc3RhcnR4cmVmCjYyNTUKJSVFT0YK\r\n-----011000010111000001101001--\r\n\r\n",
    CURLOPT_HTTPHEADER => [
      "Authorization: Bearer $token",
      "Accept: application/json",
      "Content-Type: multipart/form-data; boundary=---011000010111000001101001",
    ],
  ]);
  $response = curl_exec($curl);
  $err = curl_error($curl);

  curl_close($curl);

  if ($err) {
    echo "cURL Error #:" . $err;
  }
  else {
    echo $response;
  }
}

Receiving a token works fine, but the response from the second request is just empty. I also tried different options of attaching the file to the curl command without any success.

Could you point me in the right direction?

regards
Hendrik