Failed to post content starting with '@' using Guzzle

I am using Guzzle to post data to Twitter as soon as I try to reply to any tweet and set the original custom tweet handler in the response content. Then the final content looks like

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

      

Then I get an error for the status content

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

      

because of the following code:

__ cci / ThirdParty / eat / 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);
            }

      

kindly let me know how I can fix this problem.

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

+3


source to share


2 answers


Let's start by understanding why .

With GUZLE, use cURL, so I took a look at the php cURL manual and according to that, here's the definition (which is "responsible" for the data): CURLOPT_POSTFIELDS

Complete data for publishing in HTTP "POST" mode. To publish a file, add the file name with @ and use the full path. The file type can be explicitly specified by following the file name with the type in format '; type = mimetype '. This parameter can be passed as a urlencoded string like 'para1 = val1 & para2 = val2 & ...' or as an array with field name as key and field data as value. If the value is an array, the Content-Type header will be set to multipart / form-data. As of PHP 5.2.0, the value must be an array if files are passed to this parameter 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 to safely pass values ​​beginning with @ by setting the CURLOPT_SAFE_UPLOAD parameter to TRUE.

So, the behavior you mentioned is rational and logical. Please note the last 2 lines in the above explanation:

As of PHP 5.5.0, the @ prefix is ​​deprecated and files can be sent using CURLFile. The @ prefix can be disabled to safely transfer> values ​​starting with @ by setting the CURLOPT_SAFE_UPLOAD parameter to TRUE.

The solution is to set the parameter CURLOP_SAFE_UPLOAD

to TRUE

, but since you are using a framework it will take some work. Something like:

// Your new input array



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

      

And in the framework code:

            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]);
                }
            }

      

(Please note that I havent 'tested this code, and it was written just to show the general idea)

If you count the first part of the last two lines: @prefix is ​​deprecated as of PHP 5.5.0. This means that probably in a newer version of this structure, this will not happen for you.

I have added a link to this answer on the GitHub thread. Note that since this framework already has a newer version, the developers may not work on it and will simply prompt you to get the newer version.

+1


source


My problem was resolved after updating to the latest Guzzle library.



Thanks Ofir Baruch for the direction

0


source







All Articles