使用Guzzle将文件发布到Web服务 [英] Posting a file to a web service with Guzzle

查看:163
本文介绍了使用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服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆