本文整理汇总了PHP中Guzzle\Http\Client::createRequest方法的典型用法代码示例。如果您正苦于以下问题:PHP Client::createRequest方法的具体用法?PHP Client::createRequest怎么用?PHP Client::createRequest使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Guzzle\Http\Client
的用法示例。
在下文中一共展示了Client::createRequest方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: createRequest
/**
* Create a Guzzle Request object using the given JSON parameters
*
* @param string $query JSON String
* @return \Guzzle\Http\Message\RequestInterface
* @throws \Exception
*/
public function createRequest($query)
{
$client = new \Guzzle\Http\Client();
$method = $this->httpMethod;
$uri = $this->getBaseUrl();
$headers = array("content-type" => "application/json");
$options = array();
if ($method === 'POST') {
$postBody = $query;
$request = $client->createRequest($method, $uri, $headers, $postBody, $options);
} else {
if ($method === 'GET') {
$request = $client->createRequest($method, $uri, $headers, null, $options);
if ($query) {
$queryObject = json_decode($query, true);
$query = $request->getQuery();
foreach ($queryObject as $key => $val) {
$query->set($key, $val);
}
}
} else {
throw new Exception('Unexpected HTTP Method: ' . $method);
}
}
return $request;
}
示例2: getStatusFromWoeid
public function getStatusFromWoeid($woeid)
{
$request = $this->guzzle->createRequest(self::WEBSERVICE_METHOD, self::WEBSERVICE_URI . $woeid);
$response = $this->guzzle->send($request);
$status = $this->parseWeatherResponse($response->xml());
return $status;
}
示例3: newRequest
/**
* @see http://explorer.nuxeo.com/nuxeo/site/distribution/current/listOperations List of available operations
* @param string $operation
* @return NuxeoRequest
*/
public function newRequest($operation)
{
$request = $this->client->createRequest(Request::POST, $operation, $this->headers);
$request->setAuth($this->auth->getUsername(), $this->auth->getPassword());
$newRequest = new NuxeoRequest($request);
return $newRequest;
}
示例4: send
/**
* {@InheritDoc}
*/
public function send(Request $request)
{
$guzzleRequest = $this->client->createRequest($request->getMethod(), $request->getUri(), $request->getHeaders(), $request->getBody());
$guzzleResponse = $guzzleRequest->send();
$response = new Response($guzzleResponse->getStatusCode(), $guzzleResponse->getHeaders()->toArray(), $guzzleResponse->getBody(true));
return $response;
}
示例5: restRequest
protected function restRequest($method, $location)
{
$headers = null;
$body = null;
$options = array();
$request = $this->client->createRequest($method, $location, $headers, $body, $options);
$response = $request->send();
$result = $response->json();
return $result;
}
示例6: request
/**
* Send an http request
*
* @param string $method The HTTP method
* @param string $url The url to send the request to
* @param array $parameters The parameters for the request (assoc array)
* @param array $headers The headers for the request (assoc array)
*
* return mixed
*/
public function request($method, $url, array $parameters = null, array $headers = null)
{
$request = $this->client->createRequest($method, $url, $headers, $parameters);
try {
$response = $request->send();
} catch (\Exception $e) {
$response = $e->getResponse();
throw new ApiException($response->getStatusCode(), $response->getBody(true));
}
return $response->json();
}
示例7: request
/**
* {@inheritdoc}
*/
public function request($uri, $data = [], $method = 'GET', $options = [], $headers = [])
{
$request = $this->client->createRequest($method, $uri, $headers, $data, $options);
if (!in_array($method, ['POST', 'PUT'], true)) {
$request->getQuery()->merge($data);
}
$this->preprocessRequest($request);
try {
$response = $request->send();
} catch (BadResponseException $e) {
$response = $e->getResponse();
}
return $response;
}
示例8: createRequest
/**
* Override ti add OVH auth
*
* @param $method
* @param null $uri
* @param null $headers
* @param null $body
* @return \Guzzle\Http\Message\Request
*/
public function createRequest($method = RequestInterface::GET, $uri = null, $headers = null, $body = null)
{
$request = parent::createRequest($method, $uri, $headers, $body);
// see http://snippets.webaware.com.au/howto/stop-turning-off-curlopt_ssl_verifypeer-and-fix-your-php-config/
#$request->getCurlOptions()->set(CURLOPT_SSL_VERIFYPEER, false);
// Add OVH auth headers
$hTimestamp = $this->getTimestamp();
# SIG = "$1$" + sha1.hex(AS+"+"+CK+"+"+METHOD+"+"+QUERY+"+"+BODY +"+"+TSTAMP)
#var_dump($body);
#print $body;
#die();
#if ($method == "POST")
# $baseSig = Keyring::getAppSecret() . '+' . Keyring::getConsumerKey() . '+' . $method . '+' . $request->getUrl() . '+' . '' . '+' . $hTimestamp;
#else
$baseSig = Keyring::getAppSecret() . '+' . Keyring::getConsumerKey() . '+' . $method . '+' . $request->getUrl() . '+' . $body . '+' . $hTimestamp;
#
#print $baseSig . "\n";
$sig = '$1$' . sha1($baseSig);
#print $sig . "\n";
$request->addHeader('X-Ovh-Application', Keyring::getAppKey());
$request->addHeader('X-Ovh-Timestamp', $hTimestamp);
$request->addHeader('X-Ovh-Consumer', Keyring::getConsumerKey());
$request->addHeader('X-Ovh-Signature', $sig);
return $request;
}
示例9: request
protected function request($type, $path, $headers = array(), $body = null, $options = array())
{
$this->initializeGuzzle();
$request = $this->guzzle->createRequest($type, $path, $headers, $body, $options);
try {
$response = $request->send();
$responseBody = $response->getBody();
} catch (ClientErrorResponseException $e) {
$response = $e->getResponse();
$responseBody = $response->getBody();
$message = $responseBody;
$errorCode = $response->getStatusCode();
$jsonResponse = json_decode($responseBody, true);
if (isset($jsonResponse[0]) && isset($jsonResponse[0]['message'])) {
$message = $jsonResponse[0]['message'];
}
$fields = array();
if (isset($jsonResponse[0]) && isset($jsonResponse[0]['fields'])) {
$fields = $jsonResponse[0]['fields'];
}
$this->log->error($message, array('response' => $responseBody, 'fields' => $fields));
throw $this->getExceptionForSalesforceError($message, $errorCode, $fields);
}
return $responseBody;
}
示例10: makeRequest
/**
* Makes the request to the server.
*
* @param string $server
* @param string $service The rest service to access e.g. /connections/communities/all
* @param string $method GET, POST or PUT
* @param string $body
* @param string $headers
*/
public function makeRequest($server, $service, $method, $options, $body = null, $headers = null, $endpointName = "connections")
{
$store = SBTCredentialStore::getInstance();
$settings = new SBTSettings();
$token = $store->getToken($endpointName);
$response = null;
$client = new Client($server);
$client->setDefaultOption('verify', false);
// If global username and password is set, then use it; otherwise use user-specific credentials
if ($settings->getBasicAuthMethod($endpointName) == 'global') {
$user = $settings->getBasicAuthUsername($endpointName);
$password = $settings->getBasicAuthPassword($endpointName);
} else {
$user = $store->getBasicAuthUsername($endpointName);
$password = $store->getBasicAuthPassword($endpointName);
}
try {
$request = $client->createRequest($method, $service, $headers, $body, $options);
if ($settings->forceSSLTrust($endpointName)) {
$request->getCurlOptions()->set(CURLOPT_SSL_VERIFYHOST, false);
$request->getCurlOptions()->set(CURLOPT_SSL_VERIFYPEER, false);
}
if ($method == 'POST' && isset($_FILES['file']['tmp_name'])) {
$request->addPostFile('file', $_FILES['file']['tmp_name']);
}
$request->setAuth($user, $password);
$response = $request->send();
} catch (Guzzle\Http\Exception\BadResponseException $e) {
$response = $e->getResponse();
}
return $response;
}
示例11: makeHttpRequest
/**
* Use guzzle to make request to API
*
* @param Request $request
* @return string
*/
protected function makeHttpRequest($request)
{
$url = $this->urlBuilder->build($request);
$guzzleRequest = $this->guzzle->createRequest('GET', $url);
$guzzleResponse = $this->guzzle->send($guzzleRequest);
return $guzzleResponse->getBody();
}
示例12: send
/**
* Sends a request to the API
*
* @param [string] $url URL for API request
* @param [string] $method Request method (i.e. PUT, POST, DELETE, or GET)
* @param [array] $data Options for request
* @return [Guzzle\Http\Message\Response] $response
*/
public static function send($url, $method, $data = array())
{
// Create a new Guzzle\Http\Client
$browser = new Browser();
$browser->setUserAgent(self::userAgent());
$options = self::processOptions($data);
$request = $browser->createRequest($method, $url, null, null, $options);
if (!empty($data['postdata'])) {
foreach ($data['postdata'] as $k => $v) {
$request->setPostField($k, $v);
}
}
if (!empty($data['cookies'])) {
foreach ($data['cookies'] as $k => $v) {
$request->addCookie($k, $v);
}
}
if (!empty($data['headers'])) {
foreach ($data['headers'] as $k => $v) {
$request->setHeader($k, $v);
}
}
if (!empty($data['body']) && method_exists($request, 'setBody')) {
$request->setBody(json_encode($data['body']));
}
$debug = '#### REQUEST ####' . PHP_EOL;
$debug .= $request->getRawHeaders();
Terminus::getLogger()->debug('Headers: {headers}', array('headers' => $debug));
if (isset($data['body'])) {
Terminus::getLogger()->debug('Body: {body}', array('body' => $data['body']));
}
$response = $request->send();
return $response;
}
示例13: request
/**
* @When /^I request "([^"]*)"(?: using HTTP "([^"]*)")?$/
*/
public function request($path, $method = 'GET')
{
$this->prevRequestedPath = $path;
if (empty($this->requestHeaders['Accept'])) {
$this->requestHeaders['Accept'] = 'application/json';
}
// Add override method header if specified in the list of override verbs
if (array_key_exists($method, $this->getOverrideVerbs())) {
$this->setOverrideMethodHeader($method);
$method = $this->getOverrideVerbs()[$method];
}
$request = $this->client->createRequest($method, $path, $this->requestHeaders);
if ($this->requestBody) {
$request->setBody($this->requestBody);
$this->requestBody = null;
}
try {
$response = $request->send();
} catch (Exception $e) {
$response = $e->getResponse();
}
$this->responses[] = $response;
// Create a fresh client
$this->createClient();
}
示例14: request
/**
* Guzzle3 Request implementation
*
* @param string $httpMethod
* @param string $path
* @param array $params
* @param null $version
* @param bool $isAuthorization
*
* @return Response|mixed
* @throws ClientException
* @throws AuthorizeException
* @throws ServerException
* @throws Error
*/
public function request($httpMethod = 'GET', $path = '', $params = array(), $version = null, $isAuthorization = false)
{
//TODO: Implement Guzzle 3 here
$guzzleClient = new GuzzleClient();
switch ($httpMethod) {
case 'GET':
//TODO: array liked param need manual parser
$request = $guzzleClient->get($path, array(), array('query' => $params));
break;
default:
//default:'Content-Type'=>'application/json' for "*.json" URI
$json_body = json_encode($params);
$request = $guzzleClient->createRequest($httpMethod, $path, array(), $json_body);
$request->setHeader('Content-Type', 'application/json');
}
try {
$res = $request->send();
} catch (GuzzleException\ClientErrorResponseException $e) {
//catch error 404
$error_message = $e->getResponse();
if ($isAuthorization) {
throw new AuthorizeException($error_message, $error_message->getStatusCode(), $e->getPrevious());
} else {
throw new ClientException($error_message, $e->getResponse()->getStatusCode(), $e->getPrevious());
}
} catch (GuzzleException\ServerErrorResponseException $e) {
throw new ServerException($e, '$e->getResponse()->getStatusCode()', $e->getPrevious());
} catch (GuzzleException\BadResponseException $e) {
throw new Error($e->getResponse(), $e->getResponse()->getStatusCode(), $e->getPrevious());
}
$response = new Response($res->json(), $res->getStatusCode());
return $response;
}
示例15: createRequest
/**
* {@inheritDoc}
*/
public function createRequest($url, $parameters, $httpMethod = 'GET')
{
if ($httpMethod == 'POST' || $httpMethod == 'DELETE') {
return $this->client->createRequest($httpMethod, $url, $this->options['headers'], $parameters);
} else {
return $this->client->createRequest($httpMethod, $url, $this->options['headers'], $this->options['body'], $parameters);
}
}