PHP补救API调用创建一个条目与附件不工作(与邮差工作!)



我已经在这上面工作了好几天了,当我试图创建一个带有PDF附件的条目时,没有运气。

使用补救REST API

我每次得到一个超时错误(500)或一个坏的请求。

这里是文档:https://docs.bmc.com/docs/ars2002/example-of-using-the-rest-api-to-create-an-entry-on-a-form-909638132.html#post-attachments-341682506和一个Java示例。

我在Windows上使用XAMPP。PHP 7和Curl Version 7.70.

我的CURL调用有问题吗?

<?php
$urlSample = '<<MY URL>>';
$myUser = '<<MY USER>>';
$myPassword = '<<MY PASS>>';
$myAuthString = '<<MY AUTH STRING>>';
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $urlSample . '/api/jwt/login?username='.$myUser.'&password='.$myPassword.'&authString='.$myAuthString,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/x-www-form-urlencoded'
),
));
$response = curl_exec($curl);
curl_close($curl);
$token = $response; // This WORKS!!!
// Define the endpoint URL
$url = $urlSample . '/api/arsys/v1/entry/SRM:RequestInterface_Create?fields=values(Request%20Number)';
// Define the PDF attachment
$pdf_file = "mypdf.pdf";
// Define the form fields and their values
$fields = array(
"values" => array(
array(
"z1D Action" => "CREATE",
"Source Keyword" => "RESTAPI",
"TitleInstanceID" => "<< MY INSTANCE ID >>",
"Login ID" => $myUser,
"Customer Login" => "example",
"Date Required" => date('Y-m-d') . 'T' . date('h:m:s'),
"z1D_WorkInfoSummary" => "PDF Form", // It Works When I don't send an Attachment
"z2AF_WIAttachment1" => $pdf_file,
"SR Type Field 1" => "Requestor Name:TEST | Phone Number:123-456-7890 | Equipment: Email",
"SR Type Field 2" => "TEST",
"SR Type Field 3" => "TEST",
"SR Type Field 4" => "TEST",
"SR Type Field 5" => "TEST",
)
)
);

// Create the multipart/form-data boundary
$boundary = uniqid();
// Build the multipart/form-data payload
$data = "--" . $boundary . "rn";
$data .= "Content-Disposition: form-data; name="entry"rn";
$data .= "Content-Type: application/jsonrnrn";
$data .= json_encode($fields) . "rn";
$data .= "--" . $boundary . "rn";
$data .= "Content-Disposition: form-data; name="attach-z2AF_WIAttachment1"; filename="$pdf_file"rn";
$data .= "Content-Type: application/octet-streamrnrn";
$data .= file_get_contents($pdf_file) . "rn";
$data .= "--" . $boundary . "--rn";
// Define the HTTP headers
$headers = array(
"Authorization: AR-JWT " . $token,
"Content-Type: multipart/form-data; boundary=" . $boundary,
"Accept: application/json"
);
// Initialize cURL
$curl = curl_init();
// Set the cURL options
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// Execute the request
$response = curl_exec($curl); // Got a Timeout 
// Close the cURL session
curl_close($curl);
// Print the response
print_r(json_decode($response)); 
?>

我能够使它在PHP上工作经过很多的挣扎使用这个:

<?php
// Define the endpoint URL
$url = '<< MY URL >>';
// Define the PDF attachment
$pdf_file = "C:\xampp\htdocs\curl-tests\test.pdf";
// Define the form fields and their values
$fields = array(
"values" => array(
array(
"z1D Action" => "CREATE",
"Source Keyword" => "RESTAPI",
"TitleInstanceID" => "<< MY INSTANCE ID>>",
"Login ID" => "<< MY USER >>",
"Customer Login" => "jactest",
"Date Required" => date('Y-m-d') . 'T' . date('h:m:s'),
"z1D_WorkInfoSummary" => "TEST",
"z2AF_WIAttachment1" => $pdf_file,
"SR Type Field 1" => "TEST",
"SR Type Field 2" => "TEST",
"SR Type Field 3" => "TEST",
"SR Type Field 4" => "TEST",
"SR Type Field 5" => "TEST",
)
)
);

// Create the multipart/form-data boundary
$boundary = uniqid();
// Build the multipart/form-data payload
$data = "--" . $boundary . "rn";
$data .= "Content-Disposition: form-data; name="entry"rn";
$data .= "Content-Type: application/jsonrnrn";
$data .= str_replace(']','',str_replace('[','',json_encode($fields))) . "rn";
// $data .= json_encode($fields) . "rn"; // NOT GOOD
$data .= "--" . $boundary . "rn";
$data .= "Content-Disposition: form-data; name="attach-z2AF_WIAttachment1"; filename="$pdf_file"rn";
$data .= "Content-Type: application/octet-streamrnrn";
$data .= file_get_contents($pdf_file) . "rn";
$data .= "--" . $boundary . "--rn";
// echo nl2br($data); exit();
// Define the HTTP headers
$headers = array(
"Authorization: AR-JWT " . $token,
"Content-Type: multipart/form-data; boundary=" . $boundary,    
"Content-Length: " . strlen($data),
);

// Set up the cURL options
$options = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,  
CURLOPT_VERBOSE => 1,
CURLOPT_STDERR => fopen('logs_remedy.txt', 'w'),  
);
// Initialize cURL
$curl = curl_init();
curl_setopt_array($curl, $options);
// Execute the request
$response = curl_exec($curl);
// Close the cURL session
curl_close($curl);
// Print the response
$theResponse = (json_decode($response));
echo '<b>RESPONSE WITH FILE STREAM</b><br>';
echo '<pre>',print_r($theResponse),'</pre>';
?>

注意我生成的json,这是我的主要问题,我必须验证它几次,并提出了一些"["]";字符,我必须删除,它终于工作了。

相关内容

  • 没有找到相关文章

最新更新