我在使用CURLFile对象(curl_file_create
)时遇到问题。我尝试了多种方法,但是无论如何,最终我都会在日志文件中收到PHP警告,并且在我的cURL调用中省略了该文件。
首先,一些信息:
$this
的引用。所有变量均正确加载。代码块
$curl = curl_init();
$curlOpts = array(
CURLOPT_POST => 1,
CURLOPT_URL => $postUrl,
CURLOPT_TIMEOUT => 20,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => 'api:' . $this->apiKey
);
$postFields = array(
'from' => $email->from,
'to' => $email->to,
'subject' => $email->subject
);
if (strlen($email->cc) > 0) {
$postFields['cc'] = $email->cc;
}
if (strlen($email->bcc) > 0) {
$postFields['bcc'] = $email->bcc;
}
if (strlen($email->html) > 0) {
$postFields['html'] = $email->html;
} else {
$postFields['text'] = $email->text;
}
if (count($email->attachments) > 0) {
// Curl attachments for < PHP5.5 not supported
if (function_exists('curl_file_create')) {
$curlOpts[CURLOPT_SAFE_UPLOAD] = 1; // for < PHP 7
//$curlOpts[CURLOPT_HTTPHEADER] = array('Content-Type: multipart/form-data');
//$postFields['attachment'] = curl_file_create($email->attachments[0]);
for ($i = 1; $i <= count($email->attachments); $i++) {
$postFields['attachment[' . $i . ']'] = curl_file_create($email->attachments[$i - 1], 'text/csv', basename($email->attachments[$i - 1]));
//$postFields['attachment[' . $i . ']'] = curl_file_create('/var/www/sites/domain/contact.csv', 'text/csv', 'contact.csv');
//$postFields['attachment[' . $i . ']'] = curl_file_create('test.txt', 'text/plain', 'test.txt');
//$postFields['attachment[' . $i . ']'] = curl_file_create(realpath('test.txt'), 'text/plain', 'test.txt');
//$postFields['attachment[' . $i . ']'] = new \CURLFile($email->attachments[$i - 1]);
}
} else {
\D3DevelForms\Models\Error::CreateAndSaveSystemError(
$plugin,
\D3DevelForms\Common::ERROR_WARNING,
'PHP 5.5 or newer required for Mailgun Attachments',
\D3DevelForms\Models\Error::ERROR_CODE_API_MAILGUN_LOCAL_ERROR,
'You are using an outdated version of PHP. Email attachments via Mailgun will be ignored.');
}
}
$curlOpts[CURLOPT_POSTFIELDS] = $postFields;
$log->UpdateDebugLog('Mailgun API Options', $curlOpts);
curl_setopt_array($curl, $curlOpts);
$curl_response = curl_exec($curl);
$info = curl_getinfo($curl);
卷曲选项($curlOpts
)
Array
(
[47] => 1
[10002] => https://api.mailgun.net/v3/devtester.devtest.com/messages
[13] => 20
[19913] => 1
[107] => 1
[10005] => api:APIKEY
[-1] => 1
[10015] => Array
(
[from] => Dev Tester <devtester@devtest.com>
[to] => devemail@gmail.com
[subject] => Form Summary
[text] => My Text Content
[attachment[1]] => CURLFile Object
(
[name] => /var/www/path_to/my_file.csv
[mime] => text/csv
[postname] => my_file.csv
)
)
)
返回的卷曲信息($info
)
Array
(
[url] => https://api.mailgun.net/v3/devtester.devtest.com/messages
[content_type] => application/json
[http_code] => 200
[header_size] => 388
[request_size] => 312
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.503718
[namelookup_time] => 0.004273
[connect_time] => 0.0932
[pretransfer_time] => 0.279756
[size_upload] => 1021
[size_download] => 105
[speed_download] => 208
[speed_upload] => 2026
[download_content_length] => 105
[upload_content_length] => 1021
[starttransfer_time] => 0.368725
[redirect_time] => 0
[redirect_url] =>
[primary_ip] => Y.Y.Y.Y
[certinfo] => Array
(
)
[primary_port] => 443
[local_ip] => X.X.X.X
[local_port] => 38636
)
更新:从命令行使用cURL进行测试时,它确实可以按预期工作,包括当我以apache
进程运行它时。
sudo -u apache curl -s --user 'api:APIKEY' \
https://api.mailgun.net/v3/devtester.devtest.com/messages \
-F from='Dev Tester <devtest@devtester.devtest.com>' \
-F to='devtester@gmail.com' \
-F subject='Hello' \
-F text='Testing some Mailgun awesomness!' \
-F attachment=@/var/www/path_to/my_file.csv
{
"id": "<AA.BB.CC@devtester.devtest.com>",
"message": "Queued. Thank you."
}
我在Apache日志中收到一个PHP警告,显示如下:
“ PHP警告:curl_setopt_array():密钥附件的无效文件名[1]”
这很棘手,因为我已经确认了以下内容:
答案 0 :(得分:2)
尤里卡。问题是使用echo time_elapsed_string('2013-05-01', true);
// 5 years, 9 months, 1 week, 5 days, 23 hours, 17 minutes, 54 seconds ago
。我能够像这样纠正问题:
curl_setopt_array
我没有遇到关于//$curlOpts[CURLOPT_POSTFIELDS] = $postFields;
$log->UpdateDebugLog('Mailgun API Options', $curlOpts);
curl_setopt_array($curl, $curlOpts);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postFields);
的此限制的任何文档,因此我不确定这是否是我的不当用法,或者这是一个未知的错误。