예제 #1
0
 /**
  * Test: Get/Set post body field
  *
  * @covers \Pushy\Transport\RequestMessage::getPostBody
  * @covers \Pushy\Transport\RequestMessage::setPostBodyField
  */
 public function testGetSetPostBodyField()
 {
     // Ensure we start off with no query params
     $this->assertEmpty($this->requestMessage->getPostBody());
     // Ensure we get back RequestMessage for chaining
     $this->assertEquals($this->requestMessage, $this->requestMessage->setPostBodyField('test', 'value'));
     // Ensure query params is no longer empty
     $this->assertNotEmpty($this->requestMessage->getPostBody());
 }
예제 #2
0
파일: Http.php 프로젝트: sqmk/pushy
 /**
  * Send request
  *
  * @param RequestMessage $requestMessage
  *
  * @return array Response data
  * @throws ApiException
  * @throws ConnectionException
  */
 public function sendRequest(RequestMessage $requestMessage)
 {
     // Initialize curl and set options
     $curl = curl_init();
     $headers = [];
     curl_setopt($curl, CURLOPT_URL, $requestMessage->getFullUrl());
     curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $requestMessage->getMethod());
     curl_setopt($curl, CURLOPT_HEADER, false);
     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $this->verifyPeer);
     curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($curl, CURLOPT_HEADERFUNCTION, function ($curl, $header) use(&$headers) {
         $parts = explode(': ', $header);
         $headers[$parts[0]] = isset($parts[1]) ? trim($parts[1]) : null;
         return strlen($header);
     });
     // Set JSON body if available
     if ($body = $requestMessage->getPostBody()) {
         curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
     }
     // Get response body and status code
     $jsonResponse = json_decode(curl_exec($curl));
     $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
     curl_close($curl);
     // Parse headers for call stats
     $this->processHeaders($headers);
     // Throw connection exception if no status code or json response
     if ($jsonResponse === null || $statusCode == 0) {
         throw new ConnectionException();
     }
     // Throw API exception if unexpected status code
     if ($statusCode !== 200) {
         throw new ApiException($jsonResponse->errors[0]);
     }
     return $jsonResponse;
 }