本文整理汇总了PHP中HttpRequest::addPostFile方法的典型用法代码示例。如果您正苦于以下问题:PHP HttpRequest::addPostFile方法的具体用法?PHP HttpRequest::addPostFile怎么用?PHP HttpRequest::addPostFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HttpRequest
的用法示例。
在下文中一共展示了HttpRequest::addPostFile方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: request
protected function request($path, $args = array(), $files = array(), $envId = 0, $version = 'v1')
{
try {
$httpRequest = new HttpRequest();
$httpRequest->setMethod(HTTP_METH_POST);
$postData = json_encode($args);
$stringToSign = "/{$version}{$path}:" . $this->API_ACCESS_KEY . ":{$envId}:{$postData}:" . $this->API_SECRET_KEY;
$validToken = Scalr_Util_CryptoTool::hash($stringToSign);
$httpRequest->setHeaders(array("X_SCALR_AUTH_KEY" => $this->API_ACCESS_KEY, "X_SCALR_AUTH_TOKEN" => $validToken, "X_SCALR_ENV_ID" => $envId));
$httpRequest->setUrl("http://scalr-trunk.localhost/{$version}{$path}");
$httpRequest->setPostFields(array('rawPostData' => $postData));
foreach ($files as $name => $file) {
$httpRequest->addPostFile($name, $file);
}
$httpRequest->send();
if ($this->debug) {
print "<pre>";
var_dump($httpRequest->getRequestMessage());
var_dump($httpRequest->getResponseCode());
var_dump($httpRequest->getResponseData());
}
$data = $httpRequest->getResponseData();
return @json_decode($data['body']);
} catch (Exception $e) {
echo "<pre>";
if ($this->debug) {
var_dump($e);
} else {
var_dump($e->getMessage());
}
}
}
示例2: _OssMethods
private function _OssMethods($action, $key, $files = false) {
if (!$key || !$action)
return;
if ($action != 'DELETE' && $action != 'PUT')
return;
$options = array(
'useragent' => "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; YINUOINFO API; Alexa Toolbar)",
'connecttimeout' => 120,
'timeout' => 120,
'redirect' => 10,
);
if ($action == 'DELETE') {
$r = new HttpRequest(self::OssBaseURL . 'delete.php?key=' . $key, HTTP_METH_GET);
$r->setOptions($options);
$r->addHeaders(array('Authorization' => 'Basic ' . base64_encode(self::US_UP_AUTHORIZATION)));
try {
$ret = $r->send()->getBody();
if ($ret == 'success')
return 1;
} catch (HttpException $e) {
return 0;
}
}
if ($action == 'PUT') {
if (self::UseLocalFile && !APP_DEV){
$r = new HttpRequest(self::LocalOssBaseURL . 'local_upload.php', HTTP_METH_POST);
$r->setPostFields(array('key' => $key,'localfile' => $files));
}else {
$r = new HttpRequest(self::OssBaseURL . 'upload.php', HTTP_METH_POST);
$r->setPostFields(array('key' => $key));
$r->addPostFile('file', $files, 'image/jpeg');
}
$r->setOptions($options);
$r->addHeaders(array('Authorization' => 'Basic ' . base64_encode(self::US_UP_AUTHORIZATION)));
try {
$ret = $r->send()->getBody();
if (preg_match("/OK/i", $ret))
return 1;
} catch (HttpException $e) {
return 0;
}
}
return 0;
}
示例3: reddit_upload_sr_img
function reddit_upload_sr_img($subreddit, $fileName, $name = '')
{
global $REDDIT, $modhash, $cookies;
$type = preg_match('/\\.(gif|png)$/i', $fileName) ? 'png' : 'jpg';
if (!$name) {
$parts = preg_split('/[.\\/\\\\]/', $fileName);
array_pop($parts);
$name = array_pop($parts);
}
$url = "{$REDDIT}/api/upload_sr_img";
$request = new HttpRequest($url, HttpRequest::METH_POST);
$request->addCookies($cookies);
$request->addPostFields(array('r' => $subreddit, 'uh' => $modhash, 'formid' => 'image-upload', 'img_type' => $type, 'name' => $name));
$fileName = realpath("./{$fileName}");
$request->addPostFile('file', $fileName, 'image/png');
$response = $request->send();
$status = $response->getResponseCode();
if ($status != 200) {
die("/api/upload_sr_img failed, status={$status}\n");
}
return $response;
}
示例4: toHttpRequest
/**
*
* adapt Request to HttpRequest
*
* {@link http://us.php.net/manual/en/http.constants.php
* HTTP Predefined Constant}
*
* {@link http://us.php.net/manual/en/http.request.options.php
* HttpRequest options}
*
* @throws InvalidArgumentException
* @param Request $request
* @param Endpoint $endpoint
* @param HttpRequest
* @return \HttpRequest
*/
public function toHttpRequest($request, $endpoint)
{
$url = $endpoint->getBaseUri() . $request->getUri();
$httpRequest = new \HttpRequest($url);
$headers = array();
foreach ($request->getHeaders() as $headerLine) {
list($header, $value) = explode(':', $headerLine);
if ($header = trim($header)) {
$headers[$header] = trim($value);
}
}
// Try endpoint authentication first, fallback to request for backwards compatibility
$authData = $endpoint->getAuthentication();
if (empty($authData['username'])) {
$authData = $request->getAuthentication();
}
if (!empty($authData['username']) && !empty($authData['password'])) {
$headers['Authorization'] = 'Basic ' . base64_encode($authData['username'] . ':' . $authData['password']);
}
switch ($request->getMethod()) {
case Request::METHOD_GET:
$method = HTTP_METH_GET;
break;
case Request::METHOD_POST:
$method = HTTP_METH_POST;
if ($request->getFileUpload()) {
$httpRequest->addPostFile('content', $request->getFileUpload(), 'application/octet-stream; charset=binary');
} else {
$httpRequest->setBody($request->getRawData());
if (!isset($headers['Content-Type'])) {
$headers['Content-Type'] = 'text/xml; charset=utf-8';
}
}
break;
case Request::METHOD_HEAD:
$method = HTTP_METH_HEAD;
break;
default:
throw new InvalidArgumentException('Unsupported method: ' . $request->getMethod());
}
$httpRequest->setMethod($method);
$httpRequest->setOptions(array('timeout' => $endpoint->getTimeout(), 'connecttimeout' => $endpoint->getTimeout(), 'dns_cache_timeout' => $endpoint->getTimeout()));
$httpRequest->setHeaders($headers);
return $httpRequest;
}
示例5: uploadImage
protected function uploadImage($path)
{
$http_request = new \HttpRequest('http://image.api.abcp.ru/upload/', \HttpRequest::METH_POST);
$http_request->addPostFile('imageFile', $path);
$http_request->addPostFields([]);
$http_request->send();
$body = $http_request->getResponseBody();
$result = json_decode($body);
if ($result->status != '200' || empty($result->response->name)) {
print_r($result);
echo "image api error: {$path}\n";
exit;
}
return $result->response->name;
}
示例6: HttpRequest
<?php
$r = new HttpRequest('http://dev.iworks.at/.print_request.php', HTTP_METH_POST);
// if redirects is set to true, a single redirect is allowed;
// one can set any reasonable count of allowed redirects
$r->setOptions(array('cookies' => array('MyCookie' => 'has a value'), 'redirect' => true));
// common form data
$r->setPostFields(array('name' => 'Mike', 'mail' => 'mike@php.net'));
// add the file to post (form name, file name, file type)
$r->addPostFile('image', 'profile.jpg', 'image/jpeg');
try {
print $r->send()->getBody();
} catch (HttpException $e) {
print $e;
}