本文整理汇总了PHP中HttpRequest::setContentType方法的典型用法代码示例。如果您正苦于以下问题:PHP HttpRequest::setContentType方法的具体用法?PHP HttpRequest::setContentType怎么用?PHP HttpRequest::setContentType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HttpRequest
的用法示例。
在下文中一共展示了HttpRequest::setContentType方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __call
/**
* RPC method proxy
*
* @param string $method RPC method name
* @param array $params RPC method arguments
* @return mixed decoded RPC response
* @throws Exception
*/
public function __call($method, array $params)
{
if (strlen($this->__namespace)) {
$method = $this->__namespace . '.' . $method;
}
$this->__request->setContentType("text/xml");
$this->__request->setRawPostData(xmlrpc_encode_request($method, $params, array("encoding" => $this->__encoding) + (array) $this->__options));
$response = $this->__request->send();
if ($response->getResponseCode() != 200) {
throw new Exception($response->getResponseStatus(), $response->getResponseCode());
}
$data = xmlrpc_decode($response->getBody(), $this->__encoding);
if (xmlrpc_is_fault($data)) {
throw new Exception((string) $data['faultString'], (int) $data['faultCode']);
}
return $data;
}
示例2: executeAsrRequest
function executeAsrRequest($url, $header, $audio)
{
// First, we need to set a few SSL options
$sslOptions = array();
$sslOptions['verifypeer'] = "0";
$sslOptions['verifyhost'] = "0";
// Create an HttpRequest object
$r = new HttpRequest($url, HttpRequest::METH_POST);
// Set the SSL options, Headers, Content-Type, and body
$r->setSslOptions($sslOptions);
$r->setHeaders($header);
$r->setContentType($header['Content-Type']);
$r->setBody($audio);
try {
// Send the request
$m = $r->send();
// Return the response object
return $m;
} catch (HttpException $ex) {
// If an error occurs, just display it to the web page
echo '<br><br><font color="red" Exception: ' . $ex . '</font><br><br>';
}
}
示例3: HttpRequest
<html>
<head></head>
<body>
<?php
$r = new HttpRequest('http://www.webservicex.net/globalweather.asmx', HttpRequest::METH_POST);
//set headers
$arrayHeadeFields = array("Host" => "www.webservicex.net", "SOAPAction" => "http://www.webserviceX.NET/GetWeather");
$r->setContentType("text/xml; charset=utf-8");
$r->setHeaders($arrayHeaderFields);
//set the soap message for the request
$xmlSoapMessage = '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetWeather xmlns="http://www.webserviceX.NET">
<CityName>singapore</CityName>
<CountryName>singapore</CountryName>
</GetWeather>
</soap:Body>
</soap:Envelope>';
$r->setBody($xmlSoapMessage);
try {
$r->send();
echo '<h2>Request Header</h2>';
echo '<pre>';
print_r($r->getRawRequestMessage());
echo '</pre>';
$responseCode = $r->getResponseCode();
$responseHeader = $r->getResponseHeader();
$responseBody = $r->getResponseBody();
echo '--------------------------------------------------------------------------------------------<br/>';
echo '<h2>Rseponse Code</h2>';
示例4: login
/** Performs the actual login
* @warning Beware of exceptions.
* @return TRUE on Success, FALSE on Failure.
*/
public function login()
{
try {
//Create a new POST request
$request = new \HttpRequest($this->_loginURL, \HTTP_METH_POST);
//Set the content type
$request->setContentType($this->_contentType);
//Add POST data
$request->addPostFields(array('accountType' => $this->_accountType, 'Email' => $this->_username, 'Passwd' => $this->_password, 'service' => $this->_service, 'source' => $this->_source));
//Should we include Captcha information?
if (!empty($this->_captchaText) && !empty($this->_captchaToken)) {
$request->addPostFields(array('logintoken' => $this->_captchaToken, 'logincaptcha' => $this->_captchaText));
}
// print '------- REQUEST -------------';
// print_r($request);
//
//Make the request
$response = $request->send();
// print '------- RESPONSE ------------';
// print_r($response);
//Success?
if ($response->getResponseCode() == HTTP_RESPONSE_OK) {
//Split the repsonse body into tokens
$tokens = explode("\n", $response->getBody());
//Save each token into the session object
foreach ($tokens as $id_val) {
//Split into token_name=
$token = explode('=', $id_val);
if (empty($token[0])) {
//Skip invalid tokens
continue;
}
//Save the tokens in the session object for future use
$_SESSION[$this->getSessionKey() . '::' . $token[0]] = $token[1];
}
$this->_success = true;
return true;
}
//Captcha?
if ($response->getResponseCode() == HTTP_RESPONSE_FORBIDDEN) {
$captchaURL = '';
$captchaToken = '';
//Split the response body into tokens
$tokens = explode("\n", $response->getBody());
foreach ($tokens as $id_val) {
//This splits the parameters we got from Google into
//ID=Value strings. $token[0]=id, $token[1]=value.
//'2' allows the token to contain '=' chars
$token = explode('=', $id_val, 2);
if ($token[0] == 'CaptchaUrl') {
$captchaURL = CAPTCHA_URL_PREFIX . trim($token[1]);
} elseif ($token[0] == 'CaptchaToken') {
$captchaToken = trim($token[1]);
}
}
//Should we throw a CaptchaException?
if (!empty($captchaToken) && !empty($captchaURL)) {
throw new ClientLoginCaptchaException($captchaURL, $captchaToken);
} else {
throw new ClientLoginException($request->getBody());
}
}
} catch (HttpException $e) {
throw new ClientLoginException("An error has occurred while trying" . " to login: " . $e->getMessage(), $e);
}
//For all other responses, return false
return false;
}
示例5: update_orion
function update_orion()
{
$now = date('Y-m-d G:i:s');
$msg = $now . "\n";
try {
$orion_key = json_decode(file_get_contents('../orion_key.txt'))[0];
} catch (Exception $ex) {
$orion_key = "";
}
try {
$oldpois = json_decode(file_get_contents('pois.txt'), TRUE);
} catch (Exception $ex) {
$oldpois = array();
}
$newpois = array();
$content = file_get_contents(POI_SERVER);
$response_structure = json_decode($content, TRUE);
$pois = $response_structure['pois'];
$poicnt = 0;
$more = TRUE;
while ($more) {
$more = FALSE;
$contextElements = '{"contextElements":[' . "\n";
foreach ($pois as $uuid => $data) {
if (!isset($newpois[$uuid])) {
$json_data = json_encode($data);
$encoded_data = rawurlencode($json_data);
$lat = $data['fw_core']['location']['wgs84']['latitude'];
$lon = $data['fw_core']['location']['wgs84']['longitude'];
if (strlen($contextElements) < 1000000) {
$newpois[$uuid] = 1;
$contextElements .= '{' . '"type": "cie_poi",' . '"isPattern": "false",' . '"id": "cie_poi_' . $uuid . '",' . '"attributes": [' . '{ "name": "data",' . '"type": "string",' . '"value": "' . $encoded_data . '"' . '},' . '{' . '"name": "position",' . '"type": "coords",' . '"value": "' . $lat . ', ' . $lon . '",' . '"metadatas": [' . '{' . '"name": "location",' . '"type": "string",' . '"value": "WGS84"' . '}]}]},' . "\n";
$poicnt++;
$more = TRUE;
}
}
}
if ($more) {
$contextElements = substr($contextElements, 0, strrpos($contextElements, ',')) . '],"updateAction":"APPEND"}';
$r = new HttpRequest(restBaseURL . 'updateContext', HttpRequest::METH_POST);
$r->setContentType('application/json');
$headers = array();
$headers['Content-Type'] = 'application/json';
$headers['Accept'] = 'application/json';
if ($orion_key != "") {
$headers['X-Auth-Token'] = $orion_key;
}
$r->setHeaders($headers);
$r->addRawPostData($contextElements);
try {
$r->send();
$status = $r->getResponseBody();
if (strpos($status, '"errorCode"')) {
$msg .= $status . "\n";
}
} catch (HttpException $ex) {
$msg .= "ERROR\n" . $ex . "\n";
}
}
}
if ($poicnt > 0) {
// delete disappeared pois
$deletecnt = 0;
foreach ($oldpois as $uuid => $x) {
if (!isset($newpois[$uuid])) {
$deletecnt++;
$r = new HttpRequest(restBaseURL . "contextEntities/cie_poi_" . $uuid, HttpRequest::METH_DELETE);
$r->setContentType('application/json');
$headers = array();
$headers['Content-Type'] = 'application/json';
$headers['Accept'] = 'application/json';
if ($orion_key != "") {
$headers['X-Auth-Token'] = $orion_key;
}
$r->setHeaders($headers);
try {
$r->send();
$resp_str = $r->getResponseBody();
$resp = json_decode($resp_str, TRUE);
$code = "?";
foreach ($resp as $key => $info) {
if ($key == 'code') {
$code = $info;
} else {
if (isset($info['code'])) {
$code = $info['code'];
}
}
}
// OK if 200 (OK) or 404 (not found)
if ($code != "200" && $code != "404") {
$newpois[$uuid] = 2;
$msg .= "Failed to delete {$uuid}\n({$code}) {$resp_str}\n";
}
} catch (Exception $ex) {
$newpois[$uuid] = 3;
$msg .= "Failed to delete {$uuid}\nException: {$ex}\n";
}
}
}
//.........这里部分代码省略.........
示例6: call
/**
* Make a request to Fluidinfo API
*
* @param $method
* @param $path
* @param $params
* @param $payload
* @return object
*/
public function call($method, $path, $params = null, $payload = null, $contenttype = 'application/json', $inPool = false)
{
$url = $this->prefix . $path;
if ($params) {
$url .= '?' . $this->array2url($params);
}
$ch = new HttpRequest();
$met = 0;
if ($method == 'POST') {
$met = HTTP_METH_POST;
} else {
if ($method == 'PUT') {
$met = HTTP_METH_PUT;
} else {
if ($method == 'DELETE') {
$met = HTTP_METH_DELETE;
} else {
if ($method == 'GET') {
$met = HTTP_METH_GET;
} else {
if ($method == 'HEAD') {
$met = HTTP_METH_HEAD;
} else {
}
}
}
}
}
$ch->setMethod($met);
if ($this->credentials) {
$ch->setOptions(array('url' => $url, 'timeout' => 65, 'low_speed_time' => 65, 'useragent' => 'fluid-phpv1.1', 'httpauth' => $this->credentials));
} else {
$ch->setOptions(array('url' => $url, 'timeout' => 65, 'low_speed_time' => 65, 'useragent' => 'fluid-phpv1.1'));
}
$headers = array();
if ($method != 'GET') {
if ($payload or $method == 'PUT') {
$payload = json_encode($payload);
//$ch->setPutData($payload);
$headers[] = 'Content-Type: ' . $contenttype;
$headers[] = 'Content-Length: ' . strlen($payload);
if ($method == 'POST') {
$ch->setRawPostData($payload);
} else {
$ch->setPutData($payload);
}
$ch->setContentType($contenttype);
}
}
$ch->addHeaders($headers);
$ch->addHeaders(array('Expect' => ''));
if ($inPool == false) {
$response = $ch->send();
$infos = $response->getHeaders();
$output = $response->getBody();
if ($infos['Content-Type'] == 'application/json' or $infos['Content-Type'] == 'application/vnd.fluiddb.value+json') {
$output = json_decode($output);
}
$return_arr = array($response->getResponseCode(), $output, implode("\n", $infos));
print "<pre>";
print "Returning for method " . $method . "<br />";
print_r($return_arr);
print "<br />";
print "</pre>";
return $return_arr;
} else {
$this->pool->attach($ch);
}
}
示例7: HttpRequest
<?php
$request = new HttpRequest();
$request->setUrl('http://mockbin.com/har');
$request->setMethod(HTTP_METH_POST);
$request->setQueryData(array('foo' => array('bar', 'baz'), 'baz' => 'abc', 'key' => 'value'));
$request->setHeaders(array('content-type' => 'application/x-www-form-urlencoded', 'accept' => 'application/json'));
$request->setCookies(array('bar' => 'baz', 'foo' => 'bar'));
$request->setContentType('application/x-www-form-urlencoded');
$request->setPostFields(array('foo' => 'bar'));
try {
$response = $request->send();
echo $response->getBody();
} catch (HttpException $ex) {
echo $ex;
}