php - 无法使用 Guzzle 发布以 '@' 开头的内容

标签 php twitter guzzle

我使用 Guzzle 在 Twitter 上发布数据,一旦我尝试回复任何推文并在回复内容中设置原始推文用户处理程序。然后最终内容看起来像

$input = array('status' => '@abhishekm2040 Hi with screenname again.', 
           'in_reply_to_status_id' => '2345454545'); 

然后我收到状态内容的错误消息

 "message": "Unable to open abhishekm2040 Hi with screenname again. for reading",

因为下面的代码:

__src/thirdparty/Guzzle/Http/Message/RequestFactory.php

if (is_array($body) || $body instanceof Collection) {
                // Normalize PHP style cURL uploads with a leading '@' symbol
                foreach ($body as $key => $value) {
                    if (is_string($value) && substr($value, 0, 1) == '@') {
                        $request->addPostFile($key, $value);
                        unset($body[$key]);
                    }
                }
                // Add the fields if they are still present and not all files
                $request->addPostFields($body);
            }

请告诉我如何解决这个问题。

https://github.com/guzzle/guzzle3/issues/57

最佳答案

让我们从了解原因开始。

Guzzle 使用 cURL,因此我查看了 php cURL 手册并根据它,这里是 CURLOPT_POSTFIELDS 的定义选项(对数据“负责”):

The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. The filetype can be explicitly specified by following the filename with the type in the format ';type=mimetype'. This parameter can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data. As of PHP 5.2.0, value must be an array if files are passed to this option with the @ prefix. As of PHP 5.5.0, the @ prefix is deprecated and files can be sent using CURLFile. The @ prefix can be disabled for safe passing of values beginning with @ by setting the CURLOPT_SAFE_UPLOAD option to TRUE.

所以你提到的行为是理性和合乎逻辑的。 请注意上面解释中的最后两行:

As of PHP 5.5.0, the @ prefix is deprecated and files can be sent using CURLFile.The @ prefix can be disabled for safe passing of > values beginning with @ by setting the CURLOPT_SAFE_UPLOAD option to TRUE.

解决方案是将 CURLOP_SAFE_UPLOAD 选项设置为 TRUE,但由于您使用的是框架 - 这需要一些工作。像这样的东西:

//你的新输入数组

$input = array('status' => '@abhishekm2040 Hi with screenname again.', 
           'in_reply_to_status_id' => '2345454545',
           'ignore_a' => true);

在框架的代码中:

            if(isset($body['ignore_a']) && $body['ignore_a']){
               $ignore_a = true;
               //Here you should set the curlop_safe_upload option to true.
               //Since i'm not familiar with that framework I don't know
               //how to do it.
               //Update: It seems that the framework doesn't has a `setCurlOpt` function so you'll just have to write one.
            } else {
               $ignore_a = false;
            }

            foreach ($body as $key => $value) {
                if (!$ignore_a && is_string($value) && substr($value, 0, 1) == '@') {
                    $request->addPostFile($key, $value);
                    unset($body[$key]);
                }
            }

(请注意,我还没有测试过这段代码,它只是为了展示总体思路而编写的)

如果您考虑最后两行的第一部分:@ 前缀在 PHP 5.5.0 中已弃用。这意味着可能在该框架的较新版本中 - 它不会发生在你身上。

我在您的 GitHub 线程上添加了指向此答案的链接。请注意,由于该框架已经有了较新的版本,开发人员可能不会对其进行处理,只会向您提供获取较新版本的机会。

关于php - 无法使用 Guzzle 发布以 '@' 开头的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30205824/

相关文章:

php - Guzzle cookies 处理

php - 如何在 Guzzle 5 中发送 PUT 请求的参数?

php - guzzle NULL响应lumen php oauth2

php - 如何在仪表板的 wordpress 菜单中添加 <a> id?

javascript - 自动过期帖子

ios - 对用户隐藏 UIActivityViewController 中的字符串

android - 如何使用 fabric 获取用户时间线?

php - 拉拉维尔 5.2 : Order Models by Pivot table field

php - 从所有列中选择不同的值

css - 使用 rvest 在 Twitter 中抓取用户视频