Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send a file #155

Closed
walslayer opened this issue May 3, 2015 · 5 comments
Closed

Send a file #155

walslayer opened this issue May 3, 2015 · 5 comments

Comments

@walslayer
Copy link

Hello,

I want to send a simple file to an url.
But i've never found an example to do it.
Could you tell me how to send a file via Requests Class.

Thanks in advance.

Kind regards.

@jskit2015
Copy link

要使用 钩子,类似这样:

$hooks = new Requests_Hooks();
$hooks->register('curl.before_send', function($fp, $data=array(
    "file" => "@/Applications/XAMPP/xamppfiles/htdocs/Public/Work/image/icon.png"
    )){
    curl_setopt($fp, CURLOPT_POSTFIELDS, $data);
});
$response = Requests::post($url, $headers, array(), array('hooks' => $hooks));

@gilbcharlene
Copy link

I was wondering about this too. I ended up using another library, but thank you jskit2015 for your solution.
I think it would be beneficial if you could send a file without having to use hooks.

@sepehr
Copy link

sepehr commented Jun 17, 2015

That's a lot for sending a file!

This tiny lack is against the simplicity aim of the interface and makes the whole thing useless IMO.

@rmccue
Copy link
Collaborator

rmccue commented Oct 26, 2015

This is a duplicate of #34. Working on it right now!

@rmccue rmccue closed this as completed Oct 26, 2015
@hpaknia
Copy link

hpaknia commented May 14, 2017

In case you are using php 5.6+ or in my case php 7.1, you need to use curlFile and set CURLOPT_SAFE_UPLOAD to true.
This is the class I use to utilize this library. Passing $files array separately from $body helps out with sending the request with files and regular post data.

<?php

require_once __DIR__ . '/../Requests/Requests.php';
Requests::register_autoloader();

class SBW_Client {

    /**
     * Sonic boom API Request helper
     * @todo add file upload functionality
     * @param string $method
     * @param string $uri
     * @param array $body
     * @param array $headers
     * @return Buzz\Message\Response
     * @throws Exception
     */
    public static function request(string $method, string $uri = null, array $body = [], array $headers = [], array $files = []) {
        $method = strtolower($method);


        // preparing full uri
        $uri = self::getAbsoluteUri($uri);

        $hooks = [];

        // checking requested method to make sure a wrong method is not get called
        // feel free to add more methods in case we start to use another http method
        switch ($method) {
            case 'post':
                // if it is a multipart forma data form
                if (count($files) > 0) {
                    // replacing file paths with curlFile Object
                    array_walk($files, function($filePath, $key) use(&$body) {
                        $body[$key] = new \CURLFile(realpath($filePath));
                    });

                    // starting to add a hook to attach file to request
                    $hooks = new Requests_Hooks();
                    $hooks->register('curl.before_send', function($fp) use ($body) {
                        curl_setopt($fp, CURLOPT_SAFE_UPLOAD, true);
                        curl_setopt($fp, CURLOPT_POSTFIELDS, $body);
                    });
                    $hooks = ['hooks' => $hooks];
                    // no need to set the body, it's taken care of by hooks
                    $body  = [];
                }
            case 'put':
            case 'patch':
            case 'delete':
                $response = Requests::$method($uri, $headers, $body, $hooks);
                break;
            case 'get':
                if (!empty($body)) {
                    $uri .= '?' . http_build_query($body);
                }
                $response = Requests::get($uri, $headers);
                break;
            default:
                throw new Exception("Method $method is not allowed!");
                break;
        }

        return $response;
    }

    /**
     * Returns absolute uri to the api resource
     * @param string $uri
     * @return string
     */
    protected static function getAbsoluteUri(string $uri = null) {
        return PLACE_YOUR_API_PROTOCOL_AND_BASE_URI
                . DIRECTORY_SEPARATOR
                . trim($uri, '/');
    }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants