-
Notifications
You must be signed in to change notification settings - Fork 494
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
Comments
要使用 钩子,类似这样: $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)); |
I was wondering about this too. I ended up using another library, but thank you jskit2015 for your solution. |
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. |
This is a duplicate of #34. Working on it right now! |
In case you are using php 5.6+ or in my case php 7.1, you need to use curlFile and set <?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, '/');
}
} |
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.
The text was updated successfully, but these errors were encountered: