我找到了特定图片上传案例的编码。编码很丑,但是行得通。我想将功能转移到已经将guzzlehttp用于另一个上载(XML)的项目中。我已经转移了编码。上传时,我收到一条错误消息:“图像文件有缺陷或格式不受支持。”从网站上获取。
但是我可以通过GUI和使用旧的解决方案上传相同的图像,就像我说的那样,它可以工作。 我试图找出guzzlehttp在不同程度上构成了内容-但没有成功。
我再也没有了...
$content='' . //
'------WebKitFormBoundaryszHt4ozS9SzbbiRC' . CRLF . //
'Content-Disposition: form-data; name="sid"' . CRLF . //
CRLF . //
$sid . CRLF . //
'------WebKitFormBoundaryszHt4ozS9SzbbiRC' . CRLF . //
'Content-Disposition: form-data; name="PhonebookId"' . CRLF . //
CRLF . //
'255' . CRLF . //
'------WebKitFormBoundaryszHt4ozS9SzbbiRC' . CRLF . //
'Content-Disposition: form-data; name="PhonebookType"' . CRLF . //
CRLF . //
'1' . CRLF . //
'------WebKitFormBoundaryszHt4ozS9SzbbiRC' . CRLF . //
'Content-Disposition: form-data; name="PhonebookEntryId"' . CRLF . //
CRLF . //
$phone . CRLF . //
'------WebKitFormBoundaryszHt4ozS9SzbbiRC' . CRLF . //
'Content-Disposition: form-data; name="PhonebookPictureFile"; filename="wetter.jpg"' . CRLF . //
'Content-Type: image/jpeg' . CRLF . //
CRLF . //
file_get_contents('./test.jpg') . //
CRLF . //
'------WebKitFormBoundaryszHt4ozS9SzbbiRC' . CRLF . //
'Content-Disposition: form-data; name="apply"' . CRLF . //
CRLF . //
CRLF . //
'------WebKitFormBoundaryszHt4ozS9SzbbiRC--' . CRLF;
$len=strlen($content);
$header="" . //
"Host: $addr\n" . //
"Connection: keep-alive\n" . //
"Cache-Control: max-age=0\n" . //
"Accept: text/html\n" . //
"User-Agent: Mozilla/5.0\n" . //
"Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryszHt4ozS9SzbbiRC\n" . //
"Content-Length: $len\n" . //
"\n";
$context=array(
'http' => array(
'method' => 'POST',
'header' => $header,
'content' => $content
)
);
$url="http://$addr/cgi-bin/firmwarecfg";
$ret=file_get_contents($url,false,stream_context_create($context));
}
我试图转移...:
function uploadBackgroundImage(array $config)
{
$options = $config['fritzbox'];
$fritz = new Api($options['url']);
$fritz->setAuth($options['user'], $options['password']);
$fritz->mergeClientOptions($options['http'] ?? []);
$fritz->login();
$formfields = [
'PhonebookId' => '255',
'PhonebookType' => '1',
'PhonebookEntryId' => '613',
];
$filefields = [
'PhonebookPictureFile' => [
'type' => 'image/jpeg',
'filename' => 'updatebg.jpg',
'content' => file_get_contents('./test.jpg'),
]
];
$result = $fritz->postFile($formfields, $filefields);
if (strpos($result, 'Das Bild wurde erfolgreich hinzugefügt') === false) {
throw new \Exception('Upload failed');
}
}
...调用此给定函数:
public function postFile(array $formFields, array $fileFields): string
{
$multipart = [];
// sid must be first parameter
$formFields = array_merge(['sid' => $this->sid], $formFields);
foreach ($formFields as $key => $val) {
$multipart[] = [
'name' => $key,
'contents' => $val,
];
}
foreach ($fileFields as $name => $file) {
$multipart[] = [
'name' => $name,
'filename' => $file['filename'],
'contents' => $file['content'],
'headers' => [
'Content-Type' => $file['type'],
],
];
}
$url = $this->url . '/cgi-bin/firmwarecfg';
$resp = $this->getClient()->request('POST', $url, [
'multipart' => $multipart,
]);
return (string)$resp->getBody();
}