HTTPful attach file and json-body in one request - httpful

I need to upload files via Rest and also send some configuration with it.
Here is my example code:
$this->login();
$files = array('file'=>'aTest1.jpg');
$data =
array(
'name'=>'first file',
'description'=>'first file description',
'author'=>'test user'
);
$response = Request::post($this->getRoute('test'))
->addHeader('Authorization', "Bearer " . $this->getToken())
->attach($files)
->body(json_encode($data))
->sendsJson()
->send();
I am able to send the file or able to send the body. But it is not working if I try with both...
Any Hint for me?
Regards
n00n

For those coming to this page via Google. Here's an approach that worked for me.
Don't use attach() and body() together. I found that one will clear out the other. Instead, just use the body() method. Use file_get_contents() to get binary data for your file, then base64_encode() that data and place it into the $data as a parameter.
It should work with JSON. The approach worked for me with application/x-www-form-urlencoded mime type, using $req->body(http_build_query($data));.
$this->login();
$filepath = 'aTest1.jpg';
$data =
array(
'name'=>'first file',
'description'=>'first file description',
'author'=>'test user'
);
$req = Request::post($this->getRoute('test'))
->addHeader('Authorization', "Bearer " . $this->getToken());
if (!empty($filepath) && file_exists($filepath)) {
$filedata = file_get_contents($filepath);
$data['file'] = base64_encode($filedata);
}
$response = $req
->body(json_encode($data))
->sendsJson();
->send();

the body() method erases payload content, so after calling attach(), you must fill payload yourself :
$request = Request::post($this->getRoute('test'))
->addHeader('Authorization', "Bearer " . $this->getToken())
->attach($files);
foreach ($parameters as $key => $value) {
$request->payload[$key] = $value;
}
$response = $request
->sendsJson();
->send();

Related

Symfony2 PHPWord response

I am trying to generate a docx document on Symfony2, using the PHPWord bundle.
In my controller, I succeed in returning a docx file, but it is empty, I think it comes from my faulty response format.
public function indexAction($id)
{
$PHPWord = new PHPWord();
$section = $PHPWord->addSection();
$section->addText(htmlspecialchars(
'"Learn from yesterday, live for today, hope for tomorrow. '
. 'The important thing is not to stop questioning." '
. '(Albert Einstein)'
));
// Saving the document
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($PHPWord, 'Word2007');
return new Response($objWriter->save('helloWorld.docx'), 200, array('Content-Type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'));
}
Try this class
<?php
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Settings;
use Symfony\Component\HttpFoundation\Response;
class WordResponse extends Response
{
/**
* WordResponse constructor.
* #param string $name The name of the word file
* #param PhpWord $word
*/
public function __construct($name, &$word)
{
parent::__construct();
// Set default zip library.
if( !class_exists('ZipArchive')){
Settings::setZipClass(Settings::PCLZIP);
}
$writer = IOFactory::createWriter($word, 'Word2007');
//Set headers.
$this->headers->set("Content-Disposition", 'attachment; filename="' . $name . '"');
$this->headers->set("Content-Type", 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
$this->headers->set("Content-Transfer-Encoding", 'binary');
$this->headers->set("Cache-Control", 'must-revalidate, post-check=0, pre-check=0');
$this->headers->set("Expires", '0');
$this->sendHeaders();
$writer->save('php://output');
}
}
Then in your controller do:
return new WordResponse($phpWord, "filename.docx");
Thanks a lot for your answer.
I achieve using the 2nd method, which is in my opinion the best.
I just have to return a response, otherwise the file was generated, but stuck in the web directory.
Using this response, everything was fine and a download prompt appeared, with the "full" file.
Here's my code :
$PHPWord = new PHPWord();
$section = $PHPWord->addSection();
$section->addText(htmlspecialchars(
'"Learn from yesterday, live for today, hope for tomorrow. '
. 'The important thing is not to stop questioning." '
. '(Albert Einstein)'
));
// Saving the document
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($PHPWord, 'Word2007');
$filename="MyAwesomeFile.docx";
$objWriter->save($filename, 'Word2007', true);
$path = $this->get('kernel')->getRootDir(). "/../web/" . $filename;
$content = file_get_contents($path);
$response = new Response();
$response->headers->set('Content-Type', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
$response->headers->set('Content-Disposition', 'attachment;filename="'.$filename);
$response->setContent($content);
return $response;
PHPWord->save() returns a true value so that would be why your file is not being downloaded. With your return new Response() you are setting the content of your response to true (the result of your save call) which is why your response is empty.
You have 2 (and probably more that I haven't thought of) options to generate and download this file..
1. Save your file to a temp folder and server from there
$filename = sprintf(
'%s%sDoc-Storage%s%s.%s',
sys_get_temp_dir(),
DIRECTORY_SEPARATOR,
DIRECTORY_SEPARATOR,
uniqid(),
'docx'
);
$objWriter->save($filename);
$response = new BinaryFileResponse($filename);
For more info on the BinaryFileResponse see the docs.
2. Ignore Symfony and serve directly via the PHPWord action
$objWriter->save($filename, 'Word2007', true);
exit();
The ->save method provides all of the actions to download the generated file internally (see the code) so all you need to do is set the format and the third parameter to true and it will handle all of the headers for you. Granted it won't be returning a Symfony response but you will be exiting out before you get to that exception.

Process Ajax file upload with Symfony2

I used to have a ZF controller that was processing a fineuploader ajax upload. The code was simple:
$adapter = new Zend_File_Transfer_Adapter_Http();
$filename = uniqid();
$adapter->addFilter('Rename', APPLICATION_PATH . "/../public/temp-images/" . $filename);
$adapter->addValidator('Size', false, array("max" => "2MB"));
$adapter->addValidator('isImage', false);
if ($adapter->receive()) {
// Get mime type
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mimeType = finfo_file($finfo, APPLICATION_PATH . "/../public/temp-images/" . $filename);
finfo_close($finfo);
preg_match('/(.*)\/(.*)/', $mimeType, $matches);
$extension = '.' . $matches[2];
Now I'm refactoring using Symfony2 and I have difficulties doing the same thing. This is what I have so far:
$form = $this->createFormBuilder()
->add('qqfile', 'file', array('constraints' => new File(array('maxSize' => '2M'))))
->getForm();
if ($form->isValid()) {
die('yes');
} else {
die('no');
}
This is what gets sent from the browser:
------WebKitFormBoundaryYPzt2RqJ6W4awSFp Content-Disposition: form-data; name="qquuid"
b977c4b2-0edb-486b-aa86-4558275598aa
------WebKitFormBoundaryYPzt2RqJ6W4awSFp Content-Disposition: form-data; name="qqtotalfilesize"
14092
------WebKitFormBoundaryYPzt2RqJ6W4awSFp Content-Disposition: form-data; name="qqfile"; filename="ae35e28.png" Content-Type:
image/png
------WebKitFormBoundaryYPzt2RqJ6W4awSFp--
Now, I know for sure that the form will not be validated, because the POSTed data contains no name for the form. Actually, I don't even need to validate the whole form, just the uploaded file (like here Symfony2: upload a file using a file upload plugin), but how do I use validation for it?
I eventually figured it out myself.
Instead of using a form without a class, I created a class form whose getName() method returns an empty string. I set mapped=false for all the other fields except qqfile and also disabled the csrf protection for the form. In this way, the form gets properly submitted and the file input validated.

Symfony Audio Stream with Gaufrette

I am using KnpGaufretteBundle to store audio files. I am able to download a given file to the client like this:
$filename = "Somefilename.mp3";
$fs = $this->filesystemMap->get('media_fs');
$file = $fs->read($filename);
if($file){
//Create And Return Response
$response = new Response();
$disp = $response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
$variant->getFileName()
);
$response->headers->set('Content-Length', $fs->size($filename));
$response->headers->set('Accept-Ranges', 'bytes');
$response->headers->set('Content-Transfer-Encoding', 'binary');
$response->headers->set('Content-Type', 'application/octet-stream');
$response->headers->set('Content-Disposition', $disp);
$response->setContent($file);
return $response;
}
But now I also want to stream the file to the client, instead of using the attachment content disposition. Basically I want to access it clientside as if I was pointing at an actual mp3 sitting on my server. Does anyone know how this can be done?
I solved this by using the streamwrapper... it was this easy.
$filepath = 'gaufrette://myFileSystemName/'.$filename;
$response = new \Symfony\Component\HttpFoundation\BinaryFileResponse($filepath);

How to retrieve a streamed response (e.g. download a file) with Symfony test client

I am writing functional tests with Symfony2.
I have a controller that calls a getImage() function which streams an image file as follows:
public function getImage($filePath)
$response = new StreamedResponse();
$response->headers->set('Content-Type', 'image/png');
$response->setCallback(function () use ($filePath) {
$bytes = #readfile(filePath);
if ($bytes === false || $bytes <= 0)
throw new NotFoundHttpException();
});
return $response;
}
In functional testing, I try to request the content with the Symfony test client as follows:
$client = static::createClient();
$client->request('GET', $url);
$content = $client->getResponse()->getContent();
The problem is that $content is empty, I guess because the response is generated as soon as the HTTP headers are received by the client, without waiting for a data stream to be delivered.
Is there a way to catch the content of the streamed response while still using $client->request() (or even some other function) to send the request to the server?
The return value of sendContent (rather than getContent) is the callback that you've set. getContent actually just returns false in Symfony2
Using sendContent you can enable the output buffer and assign the content to that for your tests, like so:
$client = static::createClient();
$client->request('GET', $url);
// Enable the output buffer
ob_start();
// Send the response to the output buffer
$client->getResponse()->sendContent();
// Get the contents of the output buffer
$content = ob_get_contents();
// Clean the output buffer and end it
ob_end_clean();
You can read more on the output buffer here
The API for StreamResponse is here
For me didn't work like that. Instead, I used ob_start() before making the request, and after the request i used $content = ob_get_clean() and made asserts on that content.
In test:
// Enable the output buffer
ob_start();
$this->client->request(
'GET',
'$url',
array(),
array(),
array('CONTENT_TYPE' => 'application/json')
);
// Get the output buffer and clean it
$content = ob_get_clean();
$this->assertEquals('my response content', $content);
Maybe this was because my response is a csv file.
In controller:
$response->headers->set('Content-Type', 'text/csv; charset=utf-8');
The current best answer used to work well for me for some time, but for some reason it isn't anymore. The response is parsed into a DOM crawler and the binary data is lost.
I could fix that by using the internal response. Here's the git patch of my changes[1]:
- ob_start();
$this->request('GET', $uri);
- $responseData = ob_get_clean();
+ $responseData = self::$client->getInternalResponse()->getContent();
I hope this can help someone.
[1]: you just need access to the client, which is a
Symfony\Bundle\FrameworkBundle\KernelBrowser

wordpress wp_signon function not working

I am using wp_signon() function to login the user. I am doing this like
$creds = array();
$creds['user_login'] = $username;
$creds['user_password'] = $password;
$creds['remember'] = true;
$user = wp_signon( $creds, false );
i want to send user to home page after login.
But i Am facing following error:
Warning: Cannot modify header information - headers already sent by (output started at E:\xampp\htdocs\wpmoodle\wp-content\themes\twentyten\header.php:12) in E:\xampp\htdocs\wpmoodle\wp-includes\pluggable.php on line 690.
Thanks in advance.
wp_signon() needs to run before you've sent any of your actual page to the browser.
This is because part of what wp_signon() does is to set your authentication cookies. It does this by outputting a "Set-Cookie: ..." header -- if you look at line 690 of pluggable.php, where your error comes from, you'll see that that line sets a cookie.
So, because wp_signon() outputs headers, you can't already have sent any content -- because headers must always be output before content.
However, the error indicates that you've already sent some output -- on line 12 of header.php, presumably some of the first HTML of the standard WordPress theme.
This basically indicates that you need to move your wp_signon() call to somewhere earlier in the WordPress processing, so it has a chance to output its headers before any page content is sent.
If someone needs it, here is my solution:
function custom_login() {
if (isset($_POST['submit'])) {
$login_data = array();
$login_data['user_login'] = sanitize_user($_POST['username']);
$login_data['user_password'] = esc_attr($_POST['password']);
$user = wp_signon( $login_data, false );
if ( is_wp_error($user) ) {
echo $user->get_error_message();
} else {
wp_clear_auth_cookie();
do_action('wp_login', $user->ID);
wp_set_current_user($user->ID);
wp_set_auth_cookie($user->ID, true);
$redirect_to = $_SERVER['REQUEST_URI'];
wp_safe_redirect($redirect_to);
exit;
}
}
}
add_action( 'after_setup_theme', 'custom_login' );

Resources