curlOpt() public static method

Set a new default header to send on every request
public static curlOpt ( string $name, string $value ) : string
$name string header name
$value string header value
return string
Example #1
0
 public function testCurlOpts()
 {
     Request::curlOpt(CURLOPT_COOKIE, 'foo=bar');
     $response = Request::get('http://mockbin.com/request');
     $this->assertTrue(property_exists($response->body->cookies, 'foo'));
     Request::clearCurlOpts();
 }
Example #2
0
 public function request($path, $params, $headers = [], $isSecure = false)
 {
     $params['appid'] = $this->options['appid'];
     $params['mch_id'] = $this->options['mch_id'];
     $params['nonce_str'] = $this->getNonce();
     $params['sign'] = $this->sign($params);
     $xml = $this->xmlEncode($params);
     if ($isSecure) {
         Request::curlOpt(CURLOPT_SSLCERTTYPE, 'PEM');
         Request::curlOpt(CURLOPT_SSLCERT, $this->options['cert']);
         Request::curlOpt(CURLOPT_SSLKEYTYPE, 'PEM');
         Request::curlOpt(CURLOPT_SSLKEY, $this->options['private']);
     }
     /**
      * @var $res \Unirest\Response
      */
     $res = Request::post(sprintf('%s%s', $this->options['host'], $path), $headers, $xml);
     if ($isSecure) {
         Request::clearCurlOpts();
     }
     if ($res->code !== 200) {
         throw new \Exception('Invalid response status code', $res->code);
     }
     $body = $this->xmlDecode($res->body);
     if (!is_array($body)) {
         throw new \Exception(sprintf('Invalid response body: %s', $res->body));
     }
     if ($body['return_code'] !== static::SUCCESS) {
         throw new \Exception(sprintf('Invalid return_code: %s', $res->body));
     }
     if ($body['appid'] !== $this->options['appid'] || $body['mch_id'] !== $this->options['mch_id']) {
         throw new \Exception(sprintf('Invalid appid or mch_id: %s', $res->body));
     }
     $sign = $body['sign'];
     if ($sign !== $this->sign($body)) {
         throw new \Exception(sprintf('Invalid sign: %s', $res->body));
     }
     return $body;
 }