这是示例,一些我如何使它工作的方法
<?php
function getCurlValue($filename, $contentType, $postname)
{
// PHP 5.5 introduced a CurlFile object that deprecates the old @filename syntax
// See: https://wiki.php.net/rfc/curl-file-upload
if (function_exists('curl_file_create')) {
return curl_file_create($filename, $contentType, $postname);
}
// Use the old style if using an older version of PHP
$value = "@{$filename};filename=" . $postname;
if ($contentType) {
$value .= ';type=' . $contentType;
}
return $value;
}
function Curl_file_upload($url, $postData)
{
$filename = '/Users/bhushan/Downloads/resume.pdf';
$cfile = getCurlValue($filename,'application/pdf','resume.pdf');
//NOTE: The top level key in the array is important, as some apis will insist that it is 'file'.
$data = array('file' => $cfile);
$ch = curl_init();
$options = array(CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLINFO_HEADER_OUT => true, //Request header
CURLOPT_HEADER => true, //Return header
CURLOPT_SSL_VERIFYPEER => false, //Don't veryify server certificate
CURLOPT_POST => true,
CURLOPT_COOKIE =>"authToken=".$_SESSION['authToken'],
CURLOPT_POSTFIELDS => $data
);
curl_setopt_array($ch, $options);
$output = $result = curl_exec($ch);
$header_info = curl_getinfo($ch,CURLINFO_HEADER_OUT);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($result, 0, $header_size);
$body = substr($result, $header_size);
curl_close($ch);
return $output;
}
function curl_call($url, $postData)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER, false);
//curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
$output=curl_exec($ch);
curl_close($ch);
return $output;
}
if(!isset($_SESSION['authToken']) || empty($_SESSION['authToken']))
{
$data = array('orgCode' => 'xxxx', 'userName' => 'xxxx', 'password' => 'xxx');
echo $json = curl_call('https://xxx.xxx.taleo.net/ldd01/ats/api/v1/login', $data);
$json = json_decode($json, true);
if(isset($json['response']['authToken'])) $_SESSION['authToken'] = $json['response']['authToken'];
}
else if(isset($_SESSION['authToken']) && !empty($_SESSION['authToken']))
{
$params = array();
echo $json = Curl_file_upload('https://xxx.xxx.taleo.net/ldd01/ats/api/v1/object/candidate/58/resume', $params);
}