本文整理汇总了PHP中HTTP_Request2::setBody方法的典型用法代码示例。如果您正苦于以下问题:PHP HTTP_Request2::setBody方法的具体用法?PHP HTTP_Request2::setBody怎么用?PHP HTTP_Request2::setBody使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HTTP_Request2
的用法示例。
在下文中一共展示了HTTP_Request2::setBody方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: http2_request
private function http2_request($method, $path, $params)
{
$url = $this->api . rtrim($path, '/') . '/';
if (!strcmp($method, "POST")) {
$req = new HTTP_Request2($url, HTTP_Request2::METHOD_POST);
$req->setHeader('Content-type: application/json');
if ($params) {
$req->setBody(json_encode($params));
}
} else {
if (!strcmp($method, "GET")) {
$req = new HTTP_Request2($url, HTTP_Request2::METHOD_GET);
$url = $req->getUrl();
$url->setQueryVariables($params);
} else {
if (!strcmp($method, "DELETE")) {
$req = new HTTP_Request2($url, HTTP_Request2::METHOD_DELETE);
$url = $req->getUrl();
$url->setQueryVariables($params);
}
}
}
$req->setAdapter('curl');
$req->setConfig(array('timeout' => 30, 'ssl_verify_peer' => FALSE));
$req->setAuth($this->auth_id, $this->auth_token, HTTP_Request2::AUTH_BASIC);
$req->setHeader(array('Connection' => 'close', 'User-Agent' => 'PHPPlivo'));
$r = $req->send();
$status = $r->getStatus();
$body = $r->getbody();
$response = json_decode($body, true);
return array("status" => $status, "response" => $response);
}
示例2: OXreqPUTforSendMail
public function OXreqPUTforSendMail($url, $QueryVariables, $PutData, $returnResponseObject = false)
{
$QueryVariables['timezone'] = 'UTC';
# all times are UTC,
$request = new HTTP_Request2(OX_SERVER . $url, HTTP_Request2::METHOD_PUT);
$request->setHeader('Content-type: text/javascript; charset=utf-8');
$url = $request->getUrl();
$url->setQueryVariables($QueryVariables);
$request->setBody(utf8_encode($PutData));
return $this->OXreq($request, $returnResponseObject);
}
示例3: makeRequest
/**
* Make an API request.
*
* @param string $url The URL to request agains.
* @param string $method The request method.
* @param mixed $data Optional, most likely a json encoded string.
*
* @return HTTP_Request2_Response
* @throws HTTP_Request2_Exception In case something goes wrong. ;)
*/
protected function makeRequest($url, $method = HTTP_Request2::METHOD_GET, $data = null)
{
if ($this->apiToken !== null) {
$url .= '?u=' . $this->apiToken;
}
if (!$this->client instanceof HTTP_Request2) {
$this->client = new HTTP_Request2();
}
$this->client->setHeader('Content-Type: application/json')->setAuth($this->username, $this->password)->setMethod($method)->setUrl($this->endpoint . $url);
if ($data !== null) {
$this->client->setBody($data);
}
$resp = $this->client->send();
return $resp;
}
示例4: postArticle
/**
* 記事を投稿する.
*
* @param string $title 記事タイトル
* @param string $text 記事本文
* @param string $category 記事カテゴリ
* @return string $res 結果
*/
public function postArticle($title, $text, $category)
{
try {
$req = new HTTP_Request2();
$req->setUrl(self::ROOT_END_POINT . $this->liveDoorId . "/" . self::END_POINT_TYPE_ARTICLE);
$req->setConfig(array('ssl_verify_host' => false, 'ssl_verify_peer' => false));
$req->setMethod(HTTP_Request2::METHOD_POST);
$req->setAuth($this->liveDoorId, $this->atomPubPassword);
$req->setBody($this->createBody($title, $text, $category));
$req->setHeader('Expect', '');
$res = $req->send();
} catch (HTTP_Request2_Exception $e) {
die($e->getMessage());
} catch (Exception $e) {
die($e->getMessage());
}
return $res;
}
示例5: update
function update($acct, Link $link, Link $newlink)
{
$request = new HTTP_Request2($this->url, HTTP_Request2::METHOD_PUT);
$request->setHeader($this->headers);
$query = $link->toArray();
$query['acct'] = $acct;
$request->getUrl()->setQueryVariables($query);
$request->setBody($newlink->toTag());
try {
$response = $request->send();
if (200 == $response->getStatus()) {
return $response->getBody();
} else {
echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' . $response->getReasonPhrase();
return null;
}
} catch (HTTP_Request2_Exception $e) {
echo 'Error: ' . $e->getMessage();
}
}
示例6: http2_request
private function http2_request($method, $path, $params)
{
$url = $this->api . $path;
$http_method = \HTTP_Request2::METHOD_POST;
if (!strcmp($method, "GET")) {
$http_method = \HTTP_Request2::METHOD_GET;
} else {
if (!strcmp($method, "DELETE")) {
$http_method = \HTTP_Request2::METHOD_DELETE;
}
}
$req = new \HTTP_Request2($url, $http_method);
if ($http_method === \HTTP_Request2::METHOD_POST && $params) {
$req->setBody(json_encode($params));
}
$req->setAdapter('curl');
$req->setConfig(array('timeout' => 30, 'ssl_verify_peer' => FALSE));
$req->setHeader(array('Authorization' => $this->auth_token, 'Connection' => 'close', 'User-Agent' => 'CheckMobi/http2_request', 'Content-type' => 'application/json'));
$r = $req->send();
$status = $r->getStatus();
$body = $r->getbody();
$response = json_decode($body, true);
return array("status" => $status, "response" => $response);
}
示例7: _getResponse
public function _getResponse(S3 $s3, $url, $headers, $amz)
{
$req = new HTTP_Request2($url, $this->verb);
if (!$s3->useSSLValidation) {
$req->setConfig('ssl_verify_host', false);
$req->setConfig('ssl_verify_peer', false);
}
if ($s3->proxy != null && isset($s3->proxy['host'])) {
$req->setConfig('proxy_host');
$req->setConfig('proxy_host', $s3->proxy['host']);
if (isset($s3->proxy['user'], $s3->proxy['pass']) && $proxy['user'] != null && $proxy['pass'] != null) {
$req->setConfig('proxy_user', $s3->proxy['user']);
$req->setConfig('proxy_password', $s3->proxy['pass']);
}
}
if ($s3->hasAuth()) {
// Authorization string (CloudFront stringToSign should only contain a date)
$headers[] = 'Authorization: ' . $s3->__getSignature($this->headers['Host'] == 'cloudfront.amazonaws.com' ? $this->headers['Date'] : $this->verb . "\n" . $this->headers['Content-MD5'] . "\n" . $this->headers['Content-Type'] . "\n" . $this->headers['Date'] . $amz . "\n" . $this->resource);
}
$req->setHeader($headers);
$req->setConfig('follow_redirects', true);
// Request types
switch ($this->verb) {
case 'GET':
break;
case 'PUT':
case 'POST':
// POST only used for CloudFront
if ($this->fp !== false) {
$req->setBody($this->fp);
} elseif ($this->data !== false) {
$req->setBody($this->data);
}
break;
}
try {
$res = $req->send();
} catch (HTTP_Request2_Exception $e) {
$this->response->error = array('code' => $e->getCode(), 'message' => $e->getMessage(), 'resource' => $this->resource);
}
$this->response->code = $res->getStatus();
$this->response->body = $res->getBody();
$this->response->headers = $this->modHeaders($res->getHeader());
return $this->response;
}
示例8:
*/
//openssl smime -sign -signer certnew_Vaan.cer -nochain -nocerts -outform PEM -nodetach
//openssl.exe smime -sign -signer public.pem -inkey private.pem -nochain -nocerts -outform PEM -nodetach
$request = new HTTP_Request2($url);
//$request->setMethod(HTTP_Request2::METHOD_POST);
/*
$request->setConfig(array(
'ssl_verify_peer' => FALSE,
'ssl_verify_host' => FALSE
//'ssl_verify_peer' => TRUE,
//'ssl_local_cert' => 'ym.p7b'
));
*/
$request->setHeader($parts[0]);
$request->setBody($parts[1]);
//$request->addUpload($url, $headers_msg, $sendFilename, $contentType);
//$request->s
//$request->getHeaders();
$response = $request->send();
var_dump($response->getStatus());
/*
$request->setMethod(HTTP_Request2::METHOD_POST)
->addPostParameter('username', 'vassily')
->addPostParameter(array(
'email' => 'vassily.pupkin@mail.ru',
'phone' => '+7 (495) 123-45-67'
))
->addUpload('avatar', './exploit.exe', 'me_and_my_cat.jpg', 'image/jpeg');
*/
//$test = new COM();
示例9: elseif
// リクエスト作成
$request = new HTTP_Request2();
$request->setHeader($header);
$request->setUrl($url);
if ($method == 'GET') {
$request->setMethod(HTTP_Request2::METHOD_GET);
} elseif ($method == 'POST') {
$request->setMethod(HTTP_Request2::METHOD_POST);
} elseif ($method == 'PUT') {
$request->setMethod(HTTP_Request2::METHOD_PUT);
} elseif ($method == 'DELETE') {
$request->setMethod(HTTP_Request2::METHOD_DELETE);
} else {
die;
}
$request->setBody(trim($body));
$request->setConfig(array('ssl_verify_host' => false, 'ssl_verify_peer' => false));
// レスポンス取得
$response = $request->send();
// HTTP_Request2のエラーを表示
} catch (HTTP_Request2_Exception $e) {
die($e->getMessage());
// それ以外のエラーを表示
} catch (Exception $e) {
die($e->getMessage());
}
// エラー時
if ($response->getStatus() != "200") {
echo "{";
echo sprintf("\"httpStatus\":\"%s\"", $response->getStatus());
echo ",";
示例10: date
}
// @see http://xpoint.ru/forums/programming/PHP/faq.xhtml#740
if (!empty($GLOBALS['HTTP_RAW_POST_DATA'])) {
$body = $GLOBALS['HTTP_RAW_POST_DATA'];
if ($Config['Log-Mode'] >= 2 && $Config['Log-Path']) {
for ($i = 1; $i <= 200; ++$i) {
$fname = date('Ymd-His') . '-I-' . $i . '.html';
if (!file_exists($fname)) {
$fp = fopen($Config['Log-Path'] . '/' . $fname, 'w');
fwrite($fp, $body);
fclose($fp);
break;
}
}
}
$request->setBody($body);
}
$resp = $request->send();
$headers = $resp->getHeader();
if ($Config['Log-Mode']) {
$log[] = '--------------------------';
$log[] = '<<||<< ' . date('Y-m-d H:i:s');
$log[] = '--------------------------';
foreach ($headers as $name => $value) {
$log[] = $name . ': ' . $value;
}
}
$headers = exclude($headers, $Config['Head-Out-Exclude']);
foreach ($headers as $name => $value) {
header("{$name}: {$value}");
}
示例11: sendToCouchDb
/**
* Send data to CouchDB. This function dies on HTTP error and also if no
* config.ini is found.
*
* @param array $data The object/document.
* @param string $file The filename of the log currently being crunched.
* @param int $count Which line are we at?
*
* @return void
* @uses \HTTP_Request2
*/
public static function sendToCouchDb(array $data, $file, $count)
{
static $config;
if ($config === null) {
$config = parse_ini_file(self::$base . '/config.ini', true);
if ($config === false) {
echo "Couldn't read config.ini.";
exit(1);
}
}
static $req;
if ($req === null) {
require_once 'HTTP/Request2.php';
$req = new \HTTP_Request2();
$req->setAuth($config['couchdb']['user'], $config['couchdb']['pass']);
$req->setMethod(\HTTP_Request2::METHOD_PUT);
}
$obj = (object) $data;
$id = md5($obj->line);
// generate _id
try {
$req->setUrl($config['couchdb']['host'] . "/{$id}");
$req->setBody(json_encode($obj));
$resp = $req->send();
echo "\tDocument: {$id} (file: {$file}, line: {$count}), ";
echo "Response: " . $resp->getStatus();
if ($resp->getStatus() == 409) {
echo " [duplicate]";
}
echo "\n";
unset($resp);
unset($obj);
unset($data);
unset($id);
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
exit(1);
}
}
示例12: setBody
/**
* Sets the request body.
*
* @param string $body body to use.
*
* @return none
*/
public function setBody($body)
{
Validate::isString($body, 'body');
$this->_request->setBody($body);
}
示例13: _fetch_by_options
public function _fetch_by_options()
{
$method = 'Bug.search';
$struct = '';
foreach ($this->options as $k => $v) {
$struct .= sprintf('<member><name>%s</name><value><%s>%s</%s></value></member>' . "\n", $k, 'string', $v, 'string');
}
$xml = <<<X
<?xml version="1.0" encoding="utf-8"?>
<methodCall>
<methodName>{$method}</methodName>
<params>
<param>
<struct>
{$struct}
</struct>
</param>
</params>
</methodCall>
X;
$request = new HTTP_Request2($this->url, HTTP_Request2::METHOD_POST, array('follow_redirects' => true, 'ssl_verify_peer' => false));
$request->setHeader('Accept', 'text/xml');
$request->setHeader('Content-Type', 'text/xml;charset=utf-8');
$request->setBody($xml);
try {
$response = $request->send();
if (200 == $response->getStatus()) {
$x = simplexml_load_string($response->getBody());
$this->data['bugs'] = array();
// FIXME there must be a better way
foreach ($x->params->param->value->struct->member->value->array->data->value as $b) {
$bug = array();
foreach ($b->struct->member as $m) {
if ($m->name == 'internals') {
continue;
}
$value = (array) $m->value;
$bug[(string) $m->name] = (string) array_shift($value);
}
$this->data['bugs'][] = $bug;
}
} else {
$this->error = 'Server returned unexpected HTTP status: ' . $response->getStatus() . ' ' . $response->getReasonPhrase();
return;
}
} catch (HTTP_Request2_Exception $e) {
$this->error = $e->getMessage();
return;
}
if (isset($this->data['error']) && !empty($this->data['error'])) {
$this->error = "Bugzilla API returned an error: " . $this->data['message'];
}
}
示例14: _request
/**
* we use the Pear::\HTTP_Request2 for all the heavy HTTP protocol lifting.
*
* @param string $resource api-resource
* @param string $method request method [default:"GET"]
* @param array $data request data as associative array [default:array()]
* @param array $headers optional request header [default:array()]
*
* @throws ConnectionException
* @throws BadRequestError
* @throws UnauthorizedError
* @throws ForbiddenError
* @throws ConflictDuplicateError
* @throws GoneError
* @throws InternalServerError
* @throws NotImplementedError
* @throws ThrottledError
* @throws CCException
*
* @return string json encoded servers response
*/
private function _request($resource, $method = \HTTP_Request2::METHOD_GET, $data = array(), $headers = array())
{
$url = $this->_url . $resource;
$request = new \HTTP_Request2($url);
$request->setConfig(array('ssl_verify_peer' => API::SSL_VERIFY_PEER, 'ssl_cafile' => realpath(dirname(__FILE__)) . '/cacert.pem'));
$methods = array('options' => \HTTP_Request2::METHOD_OPTIONS, 'get' => \HTTP_Request2::METHOD_GET, 'head' => \HTTP_Request2::METHOD_HEAD, 'post' => \HTTP_Request2::METHOD_POST, 'put' => \HTTP_Request2::METHOD_PUT, 'delete' => \HTTP_Request2::METHOD_DELETE, 'trace' => \HTTP_Request2::METHOD_TRACE, 'connect' => \HTTP_Request2::METHOD_CONNECT);
$request->setMethod($methods[strtolower($method)]);
#
# If the current API instance has a valid token we add the Authorization
# header with the correct token.
#
# In case we do not have a valid token but email and password are
# provided we automatically use them to add a HTTP Basic Authenticaion
# header to the request to create a new token.
#
if ($this->_token) {
$headers['Authorization'] = sprintf('cc_auth_token="%s"', $this->_token);
} else {
if ($this->_email && $this->_password) {
$request->setAuth($this->_email, $this->_password, \HTTP_Request2::AUTH_BASIC);
}
}
#
# The API expects the body to be urlencoded. If data was passed to
# the request method we therefore use urlencode from urllib.
#
if (!empty($data)) {
if ($request->getMethod() == \HTTP_Request2::METHOD_GET) {
$url = $request->getUrl();
$url->setQueryVariables($data);
} else {
// works with post and put
$request->addPostParameter($data);
$request->setBody(http_build_query($data));
}
}
#
# We set the User-Agent Header to pycclib and the local version.
# This enables basic statistics about still used pycclib versions in
# the wild.
#
$headers['User-Agent'] = sprintf('phpcclib/%s', $this->_version);
#
# The API expects PUT or POST data to be x-www-form-urlencoded so we
# also set the correct Content-Type header.
#
if (strtoupper($method) == 'PUT' || strtoupper($method) == 'POST') {
$headers['Content-Type'] = 'application/x-www-form-urlencoded';
}
#
# We also set the Content-Length and Accept-Encoding headers.
#
//$headers['Content-Length'] = strlen($body);
$headers['Accept-Encoding'] = 'compress, gzip';
#
# Finally we fire the actual request.
#
foreach ($headers as $k => $v) {
$request->setHeader(sprintf('%s: %s', $k, $v));
}
for ($i = 1; $i < 6; $i++) {
try {
$response = $request->send();
return $this->_return($response);
} catch (\HTTP_Request2_Exception $e) {
# if we could not reach the API we wait 1s and try again
sleep(1);
# if we tried for the fifth time we give up - and cry a little
if ($i == 5) {
throw new ConnectionException('Could not connect to API...');
}
}
}
}
示例15: _sendRequestWithBody
/**
* Send an OAuth signed request with a body to the API
*
* @param string $url The URL to send the request to
* @param string $body The raw body to PUT/POST to the URL
* @param string $method The HTTP method to use (POST or PUT)
*
* @return object Instance of {@link HTTP_Request2_Response}
* @see http://bit.ly/cdZGfr
*/
private function _sendRequestWithBody($url, $body, $method = "PUT")
{
static $map = array('PUT' => HTTP_Request2::METHOD_PUT, 'POST' => HTTP_Request2::METHOD_POST);
if (array_key_exists($method, $map)) {
$method = $map[$method];
} else {
throw new Services_SimpleGeo_Exception('Invalid HTTP method ' . $method);
}
$signatureMethod = $this->_oauth->getSignatureMethod();
$params = array('oauth_nonce' => (string) rand(0, 100000000), 'oauth_timestamp' => time(), 'oauth_consumer_key' => $this->_oauth->getKey(), 'oauth_signature_method' => $signatureMethod, 'oauth_version' => '1.0');
$sig = HTTP_OAuth_Signature::factory($signatureMethod);
$params['oauth_signature'] = $sig->build($method, $url, $params, $this->_secret);
// Build the header
$header = 'OAuth realm="' . $this->_api . '"';
foreach ($params as $name => $value) {
$header .= ", " . HTTP_OAuth::urlencode($name) . '="' . HTTP_OAuth::urlencode($value) . '"';
}
$req = new HTTP_Request2(new Net_URL2($url), $method);
$req->setHeader('Authorization', $header);
$req->setBody($body);
try {
$result = $req->send();
} catch (Exception $e) {
throw new Services_SimpleGeo_Exception($e->getMessage(), $e->getCode());
}
$check = (int) substr($result->getStatus(), 0, 1);
if ($check !== 2) {
$body = @json_decode($result->getBody());
throw new Services_SimpleGeo_Exception($body->message, $result->getStatus());
}
return $result;
}