PHP cURL: multipart/form

I am using the Bullhorn REST API to parse resume files (pdf, doc, docx, txt, etc) into JSON data through cURL in PHP. I am able to successfully send the HTTP POST requests and receive a response from the API endpoint.

When I test the API with a sample resume in Postman, I receive the correct response with the data parsed from the resume into JSON. However , when I test the API with the same sample resume in PHP using cURL, I receive the correct response, but with no data being parsed from the resume into JSON.

I suspect that the issue is stemming from how I pass file data / form data to the API endpoint in my cURL HTTP POST request. Postman has no problem passing file data to the API endpoint, I just need to figure out how to pass the file data to the API endpoint through PHP cURL.

[Successful] Postman HTTP request:

POST /rest-services/90fdee/resume/parseToCandidate?format=docx HTTP/1.1
Host: rest34.bullhornstaffing.com
BhRestToken: 5081ebee-ef0f-4bed-ae8e-41dc50a4e67b
Cache-Control: no-cache
Postman-Token: b6420b58-3709-6c76-7bd5-ae8f385009ab
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename="SampleResume.pdf"
Content-Type: application/pdf


------WebKitFormBoundary7MA4YWxkTrZu0gW--

[Successful] Postman Response (not important, just shows that the HTTP request is successful)

{
  "confidenceScore" : "50.00",
  "candidate" : {
    "name" : "Test Johnson",
    "occupation" : "Director Creative Management",
    "companyName" : "Advantage Media",
    "firstName" : "Test",
    "lastName" : "Johnson",
    "email" : "testjohnson@gmail.com",
    "editHistoryValue" : "Test Johnson"
  },
  "candidateEducation" : [ {
    "startDate" : 883674000000,
    "endDate" : 1041440400000,
    "graduationDate" : 1041440400000,
    "school" : "University of Minnesota",
    "degree" : "Bachelor's Degree",
    "major" : "Environmental Science"
  } ],
  "candidateWorkHistory" : [ {
    "startDate" : 1483290000000,
    "endDate" : 1483290000000,
    "companyName" : "Advantage Media",
    "title" : "Director of Creative Management",
    "comments" : "Develop and lead a team of fierce warriors and schematic specialists into the desolate wasteland of business on behalf of our clients. Train employees on how to assist retailer in managing."
  }, {
    "startDate" : 1204390800000,
    "endDate" : 1272729600000,
    "companyName" : "Google",
    "title" : "Janitor",
    "comments" : "Literally just clean stuff and try not to bother the code monkies. They are all cool so it is a good gig."
  } ],
  "skillList" : [ "Eating", "Sleeping", "Programming", "Karate", "Frisbee", "Waiting", "Thinking", "Crying", "Jumping", "Science" ],
  "primarySkills" : [ {
    "id" : 5304,
    "name" : "Accounting"
  }, {
    "id" : 5246,
    "name" : "Business Development"
  }, {
    "id" : 15623,
    "name" : "Consumer"
  }, {
    "id" : 17032,
    "name" : "Distribution"
  }, {
    "id" : 691,
    "name" : "Marketing"
  }, {
    "id" : 6901,
    "name" : "Merchandising"
  }, {
    "id" : 76406,
    "name" : "NEGOTIATIONS"
  }, {
    "id" : 697,
    "name" : "Sales"
  }, {
    "id" : 17872,
    "name" : "STRATEGIC"
  }, {
    "id" : 5283,
    "name" : "Supply Chain"
  } ]
}

[Unsuccessful] PHP cURL code for HTTP request:

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_URL => "https://rest34.bullhornstaffing.com/rest-services/10ephs/resume/parseToCandidate?format=pdf",
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_HTTPHEADER => array(
        "BhRestToken: 5081ebee-ef0f-4bed-ae8e-41dc50a4e67b",
        "Cache-Control: no-cache",
        "Content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
    ),
    CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gWrnContent-Disposition: form-data; name="file"; filename="SampleResume.pdf"rnContent-Type: application/pdfrnrnrn------WebKitFormBoundary7MA4YWxkTrZu0gW--",
));


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

curl_close($curl);

[Unsuccessful] PHP cURL Response:

{
  "confidenceScore" : ".00",
  "candidate" : {
  }
}
链接地址: http://www.djcxy.com/p/8592.html

上一篇: 如何使用curl发布到REST / JSON服务?

下一篇: PHP cURL:multipart / form