{ return in_array(strtolower($needle), array_map('strtolower', $haystack)); } /** Please send me: array( 'uri'=> '', 'method'=>''[, 'curloptions'=> array( ... ) ]) I will throw an Exception if something goes wrong so no need to validate But you can decide whether you wnat me to do it or not */ public static function sendRequest($arrayParms, $throwEx = true) { Utils::arrayKeyExists(array('url', 'method'), $arrayParms); $client = new \Zend\Http\Client($arrayParms['url']); $client->setMethod($arrayParms['method']); switch ($arrayParms['method']) { case POST: Utils::arrayKeyExists('payload', $arrayParms); $client->setParameterPost($arrayParms['payload']); break; case GET: Utils::arrayKeyExists('payload', $arrayParms); $client->setParameterGet($arrayParms['payload']); break; } $adapter = new \Zend\Http\Client\Adapter\Curl(); $curloptions = array(CURLOPT_POST => 1, CURLOPT_HTTPAUTH => CURLAUTH_BASIC, CURLOPT_RETURNTRANSFER => 1, CURLOPT_SSL_VERIFYPEER => FALSE, CURLOPT_SSL_VERIFYHOST => FALSE); if (array_key_exists('curloptions', $arrayParms)) { $curloptions = array_merge($curloptions, $arrayParms['curloptions']); } $adapter->setOptions(array('curloptions' => $curloptions)); $client->setAdapter($adapter); $response = $client->send(); if ($response->isSuccess()) {
/** * * @return Client */ protected function getClient() { if ($this->client === null) { $adapter = new \Zend\Http\Client\Adapter\Curl(); $adapter->setCurlOption(CURLOPT_RETURNTRANSFER, true)->setCurlOption(CURLOPT_SSL_VERIFYPEER, false); $this->client = new Client(); $this->client->setAdapter($adapter); } return $this->client; }
public function __construct($apiKey, $game) { $this->apiKey = $apiKey; $this->region = new Region(Region::EUROPE); $this->game = $game; $this->client = new HttpClient(); $adapter = new \Zend\Http\Client\Adapter\Curl(); $adapter->setCurlOption(CURLOPT_SSL_VERIFYPEER, false); $this->client->setAdapter($adapter); // $this->client = new GuzzleClient([ // 'base_uri' => rtrim($this->region->getApiHost(''), '/'), // ]); }
public function __construct($game) { $this->game = $game; // $this->mock = new MockHandler([]); $this->client = new HttpClient(); $adapter = new \Zend\Http\Client\Adapter\Curl(); $adapter->setCurlOption(CURLOPT_SSL_VERIFYPEER, false); $this->client->setAdapter($adapter); // $this->client = new Client([ // 'handler' => HandlerStack::create($this->mock) //]); $cache = StorageFactory::adapterFactory('memory ', array('ttl' => 100)); parent::__construct('5nssdkuvwub25ydqzhwwznvzh8hh2ag9', new Region(Region::EUROPE), $cache); }
/** * * @return \ZendGData\HttpClient */ private function getGoogleClient() { $adapter = new \Zend\Http\Client\Adapter\Curl(); $adapter->setOptions(array('curloptions' => array(CURLOPT_SSL_VERIFYPEER => false))); $httpClient = new \ZendGData\HttpClient(); $httpClient->setAdapter($adapter); $client = \ZendGData\ClientLogin::getHttpClient(self::GOOGLE_USER_ID, self::GOOGLE_PASSWORD, \ZendGData\Photos::AUTH_SERVICE_NAME, $httpClient); return $client; }
protected function getPaypalRequest() { $config = $this->getServiceLocator()->get('config'); $paypalConfig = new \SpeckPaypal\Element\Config($config['speck-paypal-api']); $adapter = new \Zend\Http\Client\Adapter\Curl(); $adapter->setOptions(array('curloptions' => array(CURLOPT_SSL_VERIFYPEER => false))); $client = new \Zend\Http\Client(); $client->setMethod('POST'); $client->setAdapter($adapter); $paypalRequest = new \SpeckPaypal\Service\Request(); $paypalRequest->setClient($client); $paypalRequest->setConfig($paypalConfig); return $paypalRequest; }
/** * Sends the data via cURL * @param array $params * @param integer $method * @param string $raw_body * @return Zend_Http_Response */ private function curl($orig_url, $params = array(), $method = Request::GET, $headers = array(), $remove_unsafe_params = true, $retry_count = 0) { try { if (is_null($orig_url) && defined('MO_API_URL')) { $orig_url = MO_API_URL; } else { if (is_null($orig_url)) { throw new \Exception('No url was provided and MO_API_URL is not defined'); } } $url = $orig_url . $this->getFunc(); if ($method == Request::DELETE || $method == Request::PUT) { if (isset($params['_id'])) { $url .= '/' . (isset($params['_id']) ? (int) $params['_id'] : 0); } } // remove any routing to prevent overwritting the url if ($remove_unsafe_params) { if (isset($params['module'])) { unset($params['module']); } if (isset($params['controller'])) { unset($params['controller']); } if (isset($params['action'])) { unset($params['action']); } if (isset($params['func'])) { unset($params['func']); } } // gots to do this so that transfer-encoding: chunked comes through properly $curl_adapter = new \Zend\Http\Client\Adapter\Curl(); // Enforce a 30 second timeout (default is unlimited) if ($this->getTimeout() > 0) { $curl_adapter->setCurlOption(CURLOPT_TIMEOUT, $this->getTimeout()); $curl_adapter->setCurlOption(CURLOPT_CONNECTTIMEOUT, $this->getTimeout()); } //$curl_adapter->setCurlOption(CURLOPT_ENCODING , "gzip"); $curl_adapter->setCurlOption(CURLOPT_SSL_VERIFYPEER, false); $curl_adapter->setCurlOption(CURLOPT_USERAGENT, 'Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10'); $http_client = new \Zend\Http\Client(); $http_client->setAdapter($curl_adapter); $http_client->setUri($url); $http_client->setArgSeparator('&'); $request = new \Zend\Http\Request(); $request->setUri($url); //$http_client->setCookieJar($this->getHttpCookieJar()); // Set the token authentication $request->getHeaders()->addHeaderLine('Accept-Encoding', 'gzip,deflate'); $request->getHeaders()->addHeaderLine('Content-Type', \Zend\Http\Client::ENC_URLENCODED); if (is_array($headers)) { foreach ($headers as $key => $header) { $request->getHeaders()->addHeaderLine($key, $header); } } if ($request->getUri() === null) { throw new \Exception('No URI given. Param \'func\' is required.'); } /* @var $response Zend_Http_Response */ $response = false; if ($method == \Mojavi\Request\Request::GET) { if (is_array($params)) { $request->getQuery()->fromArray($params); } $request->setMethod(\Zend\Http\Request::METHOD_GET); $response = $http_client->send($request); } else { if ($method == \Mojavi\Request\Request::POST) { // If we uploaded files, we have to send them differently if (count($_FILES) > 0) { $request->getFiles()->fromArray($_FILES); $http_client->setEncType(\Zend\Http\Client::ENC_FORMDATA); } else { $http_client->setEncType(\Zend\Http\Client::ENC_URLENCODED); } if (is_array($params)) { $request->getPost()->fromArray($params); } $request->setMethod(\Zend\Http\Request::METHOD_POST); $response = $http_client->send($request); } else { if ($method == \Mojavi\Request\Request::DELETE) { $request->setMethod(\Zend\Http\Request::METHOD_DELETE); $response = $http_client->send($request); } else { if ($method == \Mojavi\Request\Request::PUT) { if (count($_FILES) > 0) { $request->getFiles()->fromArray($_FILES); $http_client->setEncType(\Zend\Http\Client::ENC_FORMDATA); } else { $http_client->setEncType(\Zend\Http\Client::ENC_FORMDATA); } if (is_array($params)) { $request->getQuery()->fromArray($params); } $request->setMethod(\Zend\Http\Request::METHOD_PUT); $response = $http_client->send($request); } } } } return $response; } catch (\Exception $e) { if (strpos($e->getMessage(), 'connect() timed out!') !== false && $retry_count < 3) { return $this->curl($orig_url, $params, $method, $headers, $remove_unsafe_params, ++$retry_count); } else { if (strpos($e->getMessage(), 'couldn\'t connect to host') !== false && $retry_count < 3) { return $this->curl($orig_url, $params, $method, $headers, $remove_unsafe_params, ++$retry_count); } else { if (strpos($e->getMessage(), 'Operation timed out') !== false && $retry_count < 3) { return $this->curl($orig_url, $params, $method, $headers, $remove_unsafe_params, ++$retry_count); } } } throw $e; } }
/** * Proxify an existing client. * * Returns the client given as argument with appropriate proxy setup. * * @param \Zend\Http\Client $client HTTP client * @param array $options ZF2 ProxyAdapter options * * @return \Zend\Http\Client */ public function proxify(\Zend\Http\Client $client, array $options = array()) { if ($this->proxyConfig) { $host = $client->getUri()->getHost(); if (!$this->isLocal($host)) { $proxyType = isset($this->proxyConfig['proxy_type']) ? $this->proxyConfig['proxy_type'] : 'default'; if ($proxyType == 'socks5') { $adapter = new \Zend\Http\Client\Adapter\Curl(); $host = $this->proxyConfig['proxy_host']; $port = $this->proxyConfig['proxy_port']; $adapter->setCurlOption(CURLOPT_FOLLOWLOCATION, true); $adapter->setCurlOption(CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5); $adapter->setCurlOption(CURLOPT_PROXY, $host); if (isset($port) && !empty($port)) { $adapter->setCurlOption(CURLOPT_PROXYPORT, $port); } $client->setAdapter($adapter); } elseif ($proxyType == 'default') { $adapter = new \Zend\Http\Client\Adapter\Proxy(); $options = array_replace($this->proxyConfig, $options); $adapter->setOptions($options); $client->setAdapter($adapter); } } } return $client; }
/** * @param string $url * @param array $options * * @return array */ protected function makeRequest($url, array $options = []) { if ($this->client === null) { $this->client = new HttpClient(); $adapter = new \Zend\Http\Client\Adapter\Curl(); $adapter->setCurlOption(CURLOPT_SSL_VERIFYPEER, false); $this->client->setAdapter($adapter); } $key = $this->getRequestKey($url, $options); if ($this->cache->hasItem($key) === true) { return $this->cache->getItem($key); // $options = array_replace_recursive($options, [ // 'headers' => [ // 'If-Modified-Since' => $this->cache->getMetadata($key)['mtime'], // ], // ]); } $options = array_replace_recursive($options, ['headers' => ['Accept' => 'application/json', 'User-Agent' => $this->getUserAgent()], 'query' => ['apikey' => $this->apiKey, 'locale' => $this->region->getLocale()]]); $request = new Request(); $request->setUri($url); foreach ($options['query'] as $param => $value) { $request->getQuery()->{$param} = $value; } $request->getHeaders()->addHeaders($options['headers']); $request->setMethod(Request::METHOD_GET); try { $response = $this->client->dispatch($request); } catch (ClientException $exception) { if ($exception->getCode() === 404) { return null; } throw new BattleNetException($exception->getResponse()->json()['detail'], $exception->getCode()); } //return json_decode($response->getBody(), true); return $this->handleSuccessfulResponse($response, $key); }