本文整理汇总了PHP中Guzzle\Http\Client::post方法的典型用法代码示例。如果您正苦于以下问题:PHP Client::post方法的具体用法?PHP Client::post怎么用?PHP Client::post使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Guzzle\Http\Client
的用法示例。
在下文中一共展示了Client::post方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: upload
public function upload($file, $remote_path, $chunk_size)
{
if (!file_exists($file) || !is_readable($file) || !($fp = fopen($file, 'rb'))) {
throw new \Exception(sprintf(__('The file %s does not seem to exist or is not readable.', 'my-wp-backup'), $file));
}
$upload_id = null;
while (!feof($fp)) {
try {
$res = $this->client->put('chunked_upload', null, array('upload_id' => $upload_id, 'offset' => ftell($fp)))->setBody(wpb_get_file_chunk($fp, $chunk_size))->send()->json();
if (null === $upload_id) {
$upload_id = $res['upload_id'];
}
} catch (BadResponseException $e) {
$body = $e->getResponse()->getBody(true);
error_log($e->getRequest()->getRawHeaders());
error_log($body);
throw new \Exception($body);
}
}
$req = $this->client->post('commit_chunked_upload/auto/' . ltrim($remote_path, '/'), null, array('upload_id' => $upload_id, 'overwrite' => 'true'));
try {
return $req->send()->json();
} catch (BadResponseException $e) {
$body = $e->getResponse()->getBody(true);
error_log($e->getRequest()->getRawHeaders());
error_log($body);
throw new \Exception($body);
}
}
示例2: post
/**
* @inheritdoc
*/
public function post($url, $params)
{
$request = $this->client->post($url, null, json_encode($params));
$this->setHeaders($request);
$response = $request->send();
return json_decode($response->getBody(true));
}
示例3: testResponse
/**
* @dataProvider cbProvider
*/
public function testResponse($expectedStatusCode, $expectedResponseContent, $request, $testCase, $testHeaders)
{
$process = new Process('php -S [::1]:8999', __DIR__ . '/../../resources');
$process->start();
sleep(1);
$signature = sha1($request . ServerMock::PROJECT_SECRET_KEY);
$headers = null;
if ($testHeaders) {
$headers = $testHeaders;
} else {
$headers = array('Authorization' => 'Signature ' . $signature);
}
$request = $this->guzzleClient->post('/webhook_server.php?test_case=' . $testCase, $headers, $request);
try {
$response = $request->send();
} catch (BadResponseException $e) {
$process->stop();
$response = $e->getResponse();
}
static::assertSame($expectedResponseContent, $response->getBody(true));
static::assertSame($expectedStatusCode, $response->getStatusCode());
static::assertArrayHasKey('x-xsolla-sdk', $response->getHeaders());
static::assertSame(Version::getVersion(), (string) $response->getHeader('x-xsolla-sdk'));
static::assertArrayHasKey('content-type', $response->getHeaders());
if (204 === $response->getStatusCode()) {
static::assertStringStartsWith('text/plain', (string) $response->getHeader('content-type'));
} else {
static::assertStringStartsWith('application/json', (string) $response->getHeader('content-type'));
}
}
示例4: getAccessToken
/**
* @inheritdoc
*/
public function getAccessToken()
{
if ($this->accessToken) {
return $this->accessToken;
}
$postFields = array('grant_type' => 'password', 'client_id' => $this->clientId, 'client_secret' => $this->clientSecret, 'username' => $this->username, 'password' => $this->password . $this->securityToken);
$request = $this->guzzle->post('oauth2/token', null, $postFields);
$request->setAuth('user', 'pass');
$response = $request->send();
$responseBody = $response->getBody();
$jsonResponse = json_decode($responseBody, true);
if ($response->getStatusCode() !== 200) {
$message = $responseBody;
if (isset($jsonResponse['error_description'])) {
$message = $jsonResponse['error_description'];
}
$this->log->error($message, array('response' => $responseBody));
throw new Exception\SalesforceAuthentication($message);
}
if (!isset($jsonResponse['access_token']) || empty($jsonResponse['access_token'])) {
$message = 'Access token not found';
$this->log->error($message, array('response' => $responseBody));
throw new Exception\SalesforceAuthentication($message);
}
$this->accessToken = $jsonResponse['access_token'];
return $this->accessToken;
}
示例5: testResponse
/**
* @dataProvider cbProvider
*/
public function testResponse($expectedStatusCode, $expectedResponseContent, $request, $testCase, $testHeaders)
{
$signature = sha1($request . self::PROJECT_SECRET_KEY);
if ($testHeaders) {
$headers = $testHeaders;
} else {
$headers = array('Authorization' => 'Signature ' . $signature);
}
$request = self::$httpClient->post('/webhook_server.php?test_case=' . $testCase, $headers, $request);
try {
$response = $request->send();
} catch (BadResponseException $e) {
$response = $e->getResponse();
}
static::assertSame($expectedResponseContent, $response->getBody(true));
static::assertSame($expectedStatusCode, $response->getStatusCode());
static::assertArrayHasKey('x-xsolla-sdk', $response->getHeaders());
static::assertSame(Version::getVersion(), (string) $response->getHeader('x-xsolla-sdk'));
static::assertArrayHasKey('content-type', $response->getHeaders());
if (Response::HTTP_NO_CONTENT === $response->getStatusCode()) {
static::assertStringStartsWith('text/plain', (string) $response->getHeader('content-type'));
} else {
static::assertStringStartsWith('application/json', (string) $response->getHeader('content-type'));
}
}
示例6: post
public function post($url)
{
$request = $this->client->post($url);
$request->addPostFields($this->postFields);
foreach ($this->headers as $k => $v) {
$request->addHeader($k, $v);
}
return $request->send()->json();
}
示例7: __call
public function __call($method, array $parameters)
{
// Derive the request body
$requestBody = $this->encoder->encodeRequest($this->createNamespace($method), $parameters, $this->requestId++);
// Initialise the POST request from the HTTP client
$request = $this->client->post(null, $this->getRequiredHeaders(), $requestBody);
// Return the decoded response from an invocation of the HTTP request
return $this->encoder->decodeResponse($request->send());
}
示例8: event
/**
* Send and Event to Customer.io
* @param $id
* @param $name
* @param $data
* @return Response
*/
public function event($id, $name, $data)
{
$body = array_merge(array('name' => $name), $this->parseData($data));
try {
$response = $this->client->post('/api/v1/customers/' . $id . '/events', null, $body, array('auth' => $this->auth))->send();
} catch (RequestException $e) {
$response = $e->getResponse();
}
return new Response($response->getStatusCode(), $response->getReasonPhrase());
}
示例9: sendAction
/**
* @param string $action add|update|delete
* @param integer $id
* @param string|null $data
*
* @return Response|null
*/
public function sendAction($action, $id, $data = null)
{
try {
$request = $this->client->post(sprintf('animelist/%s/%s.xml', $action, $id), null, $data ? ['data' => $data] : []);
$request->setAuth($this->user_name, $this->user_password);
$request->setHeader('User-Agent', 'api-team-' . $this->api_key);
return $request->send();
} catch (BadResponseException $e) {
// is not a critical error
return null;
}
}
示例10: write
/**
* Writes the record down to the log of the implementing handler
*
* @param array $record
*
* @return void
*/
protected function write(array $record)
{
$data = $this->generateDataStream($record);
// extract url and content out of http data stream to send it via guzzle
list($header, $body) = explode("\r\n\r\n", $data, 2);
list($postLine, $hostLine) = explode("\r\n", $header);
$host = substr($hostLine, strlen('HOST: '));
$url = substr($postLine, strlen('POST '), -strlen(' HTTP/1.1'));
parse_str($body, $dataArray);
$uri = $this->protocol . $host . $url;
$this->client->post($uri, ['Content-Type' => 'application/json'], json_encode($dataArray))->send();
}
示例11: post
/**
* Post a new case to the signifyd api via http.
*
* Using an array of data provided, post a request to the API to create a
* new signifyd fraud case.
*
* @param array $data
*
* @return array
*/
public function post(array $data)
{
try {
$response = self::$client->post('cases', null, json_encode($data))->send();
if (!$response->isSuccessful()) {
return $this->error($repsonse->getStatusCode(), 'case_id');
}
return $this->success($response->json()['investigationId'], 'case_id');
} catch (Exception $e) {
return $this->error($e->getMessage(), 'case_id');
}
}
示例12: isAuthenticationValid
/**
* @param string $username
* @param string $password
*
* @throws ClientErrorResponseException
*
* @return bool
*/
public function isAuthenticationValid($username, $password)
{
$request = $this->httpClient->post('authentication?username=' . urlencode($username), null, $this->createAuthenticationRequestBody($password));
try {
$request->send();
} catch (ClientErrorResponseException $exception) {
if (400 === $exception->getResponse()->getStatusCode()) {
return false;
}
throw $exception;
}
return true;
}
示例13: authorize
private function authorize()
{
if (!$this->client instanceof Client) {
throw new \Exception('Authorization process called prematurely.');
}
if (!$this->token) {
throw new \Exception('Token not informed.');
}
$authorization = $this->client->post('Login/Autenticar?token=' . $this->token)->send();
if (false === (bool) $authorization->getBody()) {
throw new \Exception('Authorization did not succeed.');
}
preg_match('/^apiCredentials=(\\w+)/', $authorization->getSetCookie(), $match);
$this->apiCredentials = $match[1];
}
示例14: getIssuesByKeys
/**
* returns a collection of issue by key
*
* @param array $keyList
* @return array[]
*/
public function getIssuesByKeys($keyList = array())
{
// if no keys given
if (empty($keyList)) {
return array();
}
// build request for given issue keys
$req = $this->client->post($this->buildUrl("search"), array(), json_encode(array('jql' => 'key IN (' . implode(",", $keyList) . ')', 'startAt' => 0, 'maxResults' => count($keyList), 'fields' => static::$attributeList)));
$req->setHeader('Content-Type', 'application/json');
// request the jira issue data
try {
$res = $req->send($req);
$resJson = $res->json();
} catch (ClientErrorResponseException $e) {
// if there are errors most likely the issue do not exists, so remove them from search list
$errors = $e->getResponse()->json();
foreach ($errors['errorMessages'] as $error) {
if (preg_match("/An issue with key '([^']+)' does not exist for field 'key'\\./mi", $error, $match) || preg_match("/The issue key '([^']+)' for field 'key' is invalid\\./mi", $error, $match)) {
unset($keyList[array_search($match[1], $keyList)]);
}
}
// request again
return $this->getIssuesByKeys($keyList);
}
// loop through all results and parse them into a clean data set
$issues = array();
foreach ($resJson['issues'] as $issueData) {
$cleanData = $this->parser->parseJiraIssue($issueData);
$issues[$cleanData['key']] = $cleanData;
}
return $issues;
}
示例15: getPayload
public function getPayload($entry, $accessToken, $message, $picture, &$chosenProvider)
{
try {
$provider = craft()->oauth->getProvider('twitter');
$token = craft()->socialPoster_accounts->getToken('twitter');
$client = new Client('https://api.twitter.com/1.1');
$subscriber = $provider->getSubscriber($token);
$client->addSubscriber($subscriber);
$request = $client->post('statuses/update.json', null, array('status' => $message));
$response = $request->send();
$data = $response->json();
SocialPosterPlugin::log('Twitter post: ' . print_r($data, true), LogLevel::Info);
$responseReturn = $this->_returnResponse($response);
if (isset($data['id'])) {
return array('success' => true, 'response' => $responseReturn, 'data' => $data);
} else {
return array('success' => false, 'response' => $responseReturn, 'data' => $data);
}
} catch (ClientErrorResponseException $e) {
return array('success' => false, 'response' => $this->_returnResponse($e->getResponse(), $e));
} catch (ServerErrorResponseException $e) {
return array('success' => false, 'response' => $this->_returnResponse($e->getResponse(), $e));
} catch (RequestErrorResponseException $e) {
return array('success' => false, 'response' => $this->_returnResponse($e->getResponse(), $e));
} catch (CurlException $e) {
return array('success' => false, 'response' => $this->_returnResponse($e->getMessage(), $e));
}
}