/** * @param PostFileInterface $file * * @return $this */ public function addPostBodyFile(PostFileInterface $file) { $this->postBody->addFile($file); return $this; }
/** * @param string $fileName * @param string $parentFolderId * @param string $content */ public function __construct($fileName, $parentFolderId, $content) { $postBody = new PostBody(); $postBody->setField('parent_id', $parentFolderId); $postBody->addFile(new PostFile('filename', $content, $fileName)); $this->request = new PostRequest('files/content'); $this->request->setBody($postBody); }
/** * @param string $fileId * @param string $content * @param ExtendedRequest $extendedRequest */ public function __construct($fileId, $content, ExtendedRequest $extendedRequest = null) { $postBody = new PostBody(); $postBody->addFile(new PostFile('filename', $content)); $this->request = new PostRequest("files/{$fileId}/content"); $this->request->setBody($postBody); if (!is_null($extendedRequest)) { $this->request->addHeaders($extendedRequest->getHeaders()); $this->request->setRawJsonBody($extendedRequest->getPostBodyFields()); } }
/** * @param string $endpoint * @param string $content * @param array $headers * @param array $files * @return Response */ public function send($endpoint, $content, array $headers = array(), array $files = array()) { $request = $this->client->createRequest('POST', $endpoint, array('body' => $content, 'exceptions' => false, 'headers' => $headers)); $body = new PostBody(); if ($files && $request instanceof RequestInterface) { foreach ($files as $name => $path) { $body->addFile(new PostFile($name, fopen($path, 'r'))); } } $request->setBody($body); $response = $this->client->send($request); return new Response($response->getStatusCode(), $response->getBody()); }
public function upload($source, $target, $type, $approved = true, $options = null, $callbackUrl = null) { $body = new PostBody(); foreach ($options as $k => $v) { $body->setField("smartling.{$k}", $v); } $body->setField('apiKey', $this->apiKey); $body->setField('projectId', $this->projectId); $body->setField('fileType', $type); $body->setField('fileUri', $target); $body->setField('approved', $approved ? 'true' : 'false'); $body->addFile(new PostFile('file', file_get_contents($source))); $request = $this->client->createRequest('POST', 'file/upload', ['body' => $body]); return $this->queueRequest($request); }
/** * Upload the deployment package to the server. * @param string $packageFile * @return array * @throws ServerException */ public function deploy($packageFile) { $url = $this->config->get('remote.endpoint') . '?cmd=deploy'; $config = $this->getRequestConfig(); $config['package_md5'] = md5_file($packageFile); $this->logger->info("Sending package to remote server..."); try { $body = new PostBody(); $body->setField('config', json_encode($config)); $body->addFile(new PostFile('package', fopen($packageFile, 'r'), null, ['Content-Type' => 'application/gzip'])); $request = $this->getClient()->createRequest('POST', $url, ['headers' => $this->getHeaders(), 'body' => $body]); $request->getEmitter()->on('progress', function (ProgressEvent $e) { $p = $e->uploaded == 0 ? 0 : intval($e->uploaded * 100 / $e->uploadSize); $this->logger->plain($p . '% uploaded (' . $e->uploaded . ' of ' . $e->uploadSize . " bytes)\r"); }); $response = $this->getClient()->send($request); return json_decode($response->getBody()->getContents(), true); } catch (ClientException $e) { throw new ServerException("Deploy error: " . $this->parseClientError($e), 0, $e); } catch (RequestException $e) { throw new ServerException("An error ocurred in the request while deploying the package: " . $e->getMessage(), 0, $e); } }
/** * Apply POST fields and files to a request to attempt to give an accurate * representation. * * @param RequestInterface $request Request to update * @param array $body Body to apply */ protected function addPostData(RequestInterface $request, array $body) { static $fields = ['string' => true, 'array' => true, 'NULL' => true, 'boolean' => true, 'double' => true, 'integer' => true]; $post = new PostBody(); foreach ($body as $key => $value) { if (isset($fields[gettype($value)])) { $post->setField($key, $value); } elseif ($value instanceof PostFileInterface) { $post->addFile($value); } else { $post->addFile(new PostFile($key, $value)); } } if ($request->getHeader('Content-Type') == 'multipart/form-data') { $post->forceMultipartUpload(true); } $request->setBody($post); }
public function testCreatesMultipartUploadWithMultiFields() { $b = new PostBody(); $b->setField('testing', ['baz', 'bar']); $b->setField('other', 'hi'); $b->setField('third', 'there'); $b->addFile(new PostFile('foo', fopen(__FILE__, 'r'))); $s = (string) $b; $this->assertContains(file_get_contents(__FILE__), $s); $this->assertContains('testing=bar', $s); $this->assertContains('Content-Disposition: form-data; name="third"', $s); $this->assertContains('Content-Disposition: form-data; name="other"', $s); }
public function testCreatesMultipartUploadWithMultiFields() { $b = new PostBody(); $b->setField('testing', ['baz', 'bar']); $b->addFile(new PostFile('foo', fopen(__FILE__, 'r'))); $s = (string) $b; $this->assertContains(file_get_contents(__FILE__), $s); $this->assertContains('testing=bar', $s); }
/** * Apply POST fields and files to a request to attempt to give an accurate * representation. * * @param RequestInterface $request Request to update * @param array $body Body to apply */ protected function addPostData(RequestInterface $request, array $body) { static $fields = ['string' => true, 'array' => true, 'NULL' => true, 'boolean' => true, 'double' => true, 'integer' => true]; $post = new PostBody(); foreach ($body as $key => $value) { if (isset($fields[gettype($value)])) { $post->setField($key, $value); } elseif ($value instanceof PostFileInterface) { $post->addFile($value); } else { $post->addFile(new PostFile($key, $value)); } } $request->setBody($post); }
/** * Upload a file to the Library. Must be one of PNG, JPG, JPEG, GIF, or PDF. * The server scans files for viruses, so this returns an ID for a FileUploadStatus. * @param string $accessToken - Constant Contact Oauth2 Access Token * @param string $fileName - Name of the file * @param string $fileLocation - Path to the location of the file on the server * @throws IllegalArgumentException if file type is not one listed in the description * @param string $description - Description of the file * @param string $source - Source * @param string $folderId - Folder ID to upload file to. Set as 0 for no folder. * @return string File upload status ID * @throws CtctException */ public function uploadFile($accessToken, $fileName, $fileLocation, $description, $source, $folderId) { $finfo = finfo_open(FILEINFO_MIME_TYPE); $mime = finfo_file($finfo, $fileLocation); finfo_close($finfo); if ($mime == "image/png") { $fileType = "PNG"; } elseif ($mime = "image/jpeg") { $fileType = "JPG"; } elseif ($mime = "image/gif") { $fileType = "GIF"; } elseif ($mime = "application/pdf") { $fileType = "PDF"; } else { throw new IllegalArgumentException(sprintf(Config::get('errors.file_extension'), "PNG, JPG, JPEG, GIF, PDF was " . $mime)); } $baseUrl = Config::get('endpoints.base_url') . Config::get('endpoints.library_files'); $request = parent::createBaseRequest($accessToken, "POST", $baseUrl); $request->setHeader("Content-Type", "multipart/form-data"); $body = new PostBody(); $body->setField("folderId", $folderId); $body->setField("file_name", $fileName); $body->setField("file_type", $fileType); $body->setField("description", $description); $body->setField("source", $source); $body->addFile(new PostFile("data", fopen($fileLocation, 'r'), $fileName)); $request->setBody($body); try { $response = parent::getClient()->send($request); } catch (ClientException $e) { throw parent::convertException($e); } return $response->getHeader("Id"); }
/** * Create a Remove Contacts Activity from a file. Valid file types are txt, csv, xls, xlsx * @param string $accessToken - Constant Contact OAuth2 access token * @param string $fileName - The name of the file (ie: contacts.csv) * @param string $fileLocation - The location of the file on the server, this method uses fopen() * @param string $lists - Comma separated list of ContactList id's to add the contacts to * @return Activity * @throws CtctException */ public function addRemoveContactsFromListsActivityFromFile($accessToken, $fileName, $fileLocation, $lists) { $baseUrl = Config::get('endpoints.base_url') . Config::get('endpoints.remove_from_lists_activity'); $request = parent::createBaseRequest($accessToken, "POST", $baseUrl); $request->setHeader("Content-Type", "multipart/form-data"); $body = new PostBody(); $body->setField("lists", $lists); $body->setField("file_name", $fileName); $body->addFile(new PostFile("data", fopen($fileLocation, 'r'), $fileName)); $request->setBody($body); try { $response = parent::getClient()->send($request); } catch (ClientException $e) { throw parent::convertException($e); } return Activity::create($response->json()); }
/** * Get the "body" payload field for the Guzzle request. * * @param Swift_Mime_Message $message * @return PostBody */ protected function getBody(Swift_Mime_Message $message) { $body = new PostBody(); $body->setField('from', $this->getFrom($message)); $body->setField('to', $this->getTo($message)); $body->setField('subject', $message->getSubject()); $messageHtml = $message->getBody(); if ($message->getChildren()) { foreach ($message->getChildren() as $child) { if (str_contains($messageHtml, $child->getId())) { $messageHtml = str_replace($child->getId(), $child->getFilename(), $messageHtml); $body->addFile(new PostFile('inlineattachment', $child->getBody(), $child->getFilename(), ['Content-Type' => $child->getContentType()])); } else { switch (get_class($child)) { case 'Swift_Attachment': case 'Swift_Image': $body->addFile(new PostFile('attachment', $child->getBody(), $child->getFilename(), ['Content-Type' => $child->getContentType()])); break; case 'Swift_MimePart': switch ($child->getContentType()) { case 'text/plain': $body->setField('text', $child->getBody()); break; } break; } } } } $body->setField('html', $messageHtml); return $body; }
/** * Send a new request. * * @return $this */ protected function sendRequest() { // Setup the basi settings. $httpClient = new Client(['timeout' => $this->config->getConnectionTimeout()]); $requestType = 'GET'; $request = null; $options = array('headers' => array('User-Agent' => "\n Mozilla/5.0\n (compatible; CtoCommunication RequestExtended on Contao " . VERSION . "." . BUILD . ";\n rv:1.0);\n CtoCommunication RPC (ctoComV" . $GLOBALS["CTOCOM_VERSION"] . ")", 'Accept-Language' => vsprintf("%s, en;q=0.8", array($GLOBALS['TL_LANGUAGE'])), 'Accept' => 'text/plain; q=0.5, text/html')); // Add the user auth if set. if ($this->client->hasHttpAuth()) { $options['auth'] = [$this->client->getHttpUser(), $this->client->getHttpPassword()]; } if ($this->isGetRequest) { // Only get. // Build the array with the query params. $query = array(); foreach ($this->data as $key => $value) { $query[$value["name"]] = $value["value"]; } // Add to the options. $options = array_merge_recursive($options, array('query' => $query)); } else { // Create a new post body and add all form fields. $postBody = new PostBody(); foreach ($this->data as $key => $value) { if (isset($value["filename"]) == true && strlen($value["filename"]) != 0) { $postBody->addFile(new PostFile($value["name"], fopen($value["filepath"], 'r'), basename($value["filepath"]), array('contentType' => $value["mime"], 'encoding' => 'binary'))); } else { $postBody->setField($value["name"], $this->objIOEngine->OutputPost($value["value"], $this->objCodifyengine)); } } // Create a new request for post. $requestType = 'POST'; $options = array_merge_recursive($options, array('body' => $postBody)); } // create a new request. $request = $httpClient->createRequest($requestType, $this->client->getUrl() . $this->getQueryString(), $options); // Send the request. $this->request = $httpClient->send($request); return $this; }