使用Guzzle将文件发布到Web服务

编程入门 行业动态 更新时间:2024-10-27 01:27:40
本文介绍了使用Guzzle将文件发布到Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经尝试了几个小时来发出发送文件的POST请求。

I've been trying for several hours to make a POST request that sends a file.

首先尝试一个简单的 file_get_contents()与流上下文,似乎不工作。

First tried a simple file_get_contents() with stream context, doesn't seem to work. I never get a response back while the GET on a different URL works.

我在网络上搜索了一个HTTP客户端,发现在Packagist上下载了40万次的Guzzle;我决定尝试这种技术。

I searched on the web for an HTTP client and found Guzzle which was downloaded 400k times on Packagist; I decided to try out this technology. Well documented but, alas, getting an error also when posting that damn file.

$request = $client ->post('/files/') ->addPostFields(array('comments' => 'no value')) ->addPostFile('file', 'test.doc') ->setAuth($this->username, $this->password);

我花了几个小时的时间浏览网页,发现我有一个417期望失败issue。

It took me hours of reading and scouring the web to find out that I had a "417 Expectation Failed" issue.

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>417 Expectation Failed</title> </head><body> <h1>Expectation Failed</h1> <p>The expectation given in the Expect request-header field could not be met by this server. The client sent<pre> Expect: 100-Continue, 100-Continue </pre> </p><p>Only the 100-continue expectation is supported.</p> </body></html>

在网上进行更多搜索后,我最终阅读了Expect:100-continue这是由Guzzle自动发送的,所以我尝试:

After more searching on the web, I ended up reading on the "Expect: 100-continue" header that was sent automatically by Guzzle, so I tried:

$request->removeHeader('expect');

我现在遇到错误的请求:

I now end up with a bad request:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>400 Bad Request</title> </head><body> <h1>Bad Request</h1> <p>Your browser sent a request that this server could not understand.<br /> Request header field is missing ':' separator.<br /> <pre> zzle/3.7.3 curl/7.15.5 PHP/5.3.27</pre> </p> </body></html>

打印请求标题以找到:missing,i get:

Printing the request headers to find that : missing, i get:

var_dump($request->__toString()); POST /files/ HTTP/1.1 Host: nunc57.qc.bell.ca User-Agent: Guzzle/3.7.3 curl/7.15.5 PHP/5.3.27 Content-Type: multipart/form-data Authorization: Basic bm92YTpzeW5hcHNl

现在我真的很抱歉,我希望有人已经找到一个解决这个问题的解决方案。我真的厌倦了所有这些:(

Now I'm really stumped, I hope someone has already found a solution to this issue. I'm getting really tired of all this :(

推荐答案

我想知道是否POST到 / files / (带斜杠)可能会导致问题,也许这会强制内部重定向和颠倒Goutte,但不是一个Web浏览器?我试着调整您的页面,使您POST到 / files 或 / files / something 。

I wonder whether POSTing to /files/ (with a trailing slash) might be causing problems. Perhaps this forces an internal redirect and upsets Goutte, but not a web browser? I'd try adjusting your page so that you POST either to /files or /files/something.

可以使用网络浏览器访问目标网址,我还建议您使用浏览器工具记录请求标头,并查看是否有任何标头可以添加到您的Guzzle请求。

If you can POST okay to the target URL using a web browser, I'd also suggest you record the request headers using browser tools, and see if there are any headers that could be added to your Guzzle request.

更多推荐

使用Guzzle将文件发布到Web服务

本文发布于:2023-05-25 11:53:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/300584.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   Guzzle   Web

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!