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

使用Remedy 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.PHP7和Curl 7.70版.

我的卷发有问题吗?

<?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 . "\r\n";
$data .= "Content-Disposition: form-data; name=\"entry\"\r\n";
$data .= "Content-Type: application/json\r\n\r\n";
$data .= json_encode($fields) . "\r\n";
$data .= "--" . $boundary . "\r\n";
$data .= "Content-Disposition: form-data; name=\"attach-z2AF_WIAttachment1\"; filename=\"$pdf_file\"\r\n";
$data .= "Content-Type: application/octet-stream\r\n\r\n";
$data .= file_get_contents($pdf_file) . "\r\n";
$data .= "--" . $boundary . "--\r\n";

// 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 . "\r\n";
$data .= "Content-Disposition: form-data; name=\"entry\"\r\n";
$data .= "Content-Type: application/json\r\n\r\n";
$data .= str_replace(']','',str_replace('[','',json_encode($fields))) . "\r\n";
// $data .= json_encode($fields) . "\r\n"; // NOT GOOD
$data .= "--" . $boundary . "\r\n";
$data .= "Content-Disposition: form-data; name=\"attach-z2AF_WIAttachment1\"; filename=\"$pdf_file\"\r\n";
$data .= "Content-Type: application/octet-stream\r\n\r\n";
$data .= file_get_contents($pdf_file) . "\r\n";
$data .= "--" . $boundary . "--\r\n";

// 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,这是我的主要问题,我不得不多次验证它,并提出了一些我必须删除的"[""]字符,最终它起作用了.

Php相关问答推荐

如何在Laravel Controller中存储文本区域值?

仅显示特定WooCommerce产品类别的额外产品字段

用户信息更新期间Laravel邮箱验证问题

自定义WooCommerce查询字符串仅显示前3个产品

Mysqli_stmt::Execute():结果字段元数据中存在过早的EOF

如何在php网站代码中清理浏览器缓存

如何在Livewire 3 Form类中做依赖注入?

用GUZLE模拟Read_Timeout

使用HPOS过滤WooCommerce中的订单列表

未定义的类型';PDF';在Laravel 8

ANTLR4-lexer如何消耗更多的 token 并停止现有规则?

在WooCommercel邮箱通知上添加来自Apaczka插件的选定交付点

Filament v3:具有 hasMany 和 BelongsTo 的关系管理器

打折时更改 Woocommerce 产品名称

在 PHP 中将字符串分解为数组(字符串类似于 WP 短代码)

PHP 在数组中搜索值

向元素添加类时添加循环错误

WooCommerce 订单和邮箱通知的自定义产品描述

Laravel 10 单元测试看不到来自 Artisan Command 的数据库更改

如何将子查询结果添加到学说 2 中主查询的末尾?