PHP cURL:multipart / form
我正在使用Bullhorn REST API通过PHP中的cURL将简历文件(pdf,doc,docx,txt等)解析为JSON数据。 我能够成功发送HTTP POST请求并从API端点接收响应。
当我使用Postman中的示例简历测试API时,我收到了从简历中解析出的数据到JSON中的正确响应。 但是 ,当我使用cURL在PHP中使用相同的样本简历测试API时,我收到了正确的响应,但没有数据从简历中解析为JSON。
我怀疑这个问题是由于我将cURL HTTP POST请求中的文件数据/表单数据传递给API端点所导致的。 Postman将文件数据传递给API端点没有问题,我只需要弄清楚如何通过PHP cURL将文件数据传递给API端点。
[成功] Postman HTTP请求:
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--
[成功] Postman Response (不重要,仅表明HTTP请求成功)
{
"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]用于HTTP请求的PHP cURL代码:
$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);
[不成功] PHP cURL响应:
{
"confidenceScore" : ".00",
"candidate" : {
}
}
链接地址: http://www.djcxy.com/p/8591.html