{ 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()) {
/** * Sends a request and returns a response * * @param CartRecover_Request $request * @return Cart_Recover_Response */ public function sendRequest(CartRecover_Request $request) { $this->client->setUri($request->getUri()); $this->client->setParameterGet($request->getParams()); $this->client->setMethod($request->getMethod()); $this->client->setHeaders(array('Accept' => 'application/json')); $this->response = $this->client->send(); if ($this->response->getHeaders()->get('Content-Type')->getFieldValue() != 'application/json') { throw new CartRecover_Exception_UnexpectedValueException("Unknown response format."); } $body = json_decode($this->response->getContent(), true); $response = new CartRecover_Response(); $response->setRawResponse($this->response->toString()); $response->setBody($body); $response->setHeaders($this->response->getHeaders()->toArray()); $response->setStatus($this->response->getReasonPhrase(), $this->response->getStatusCode()); return $response; }
/** * @param string $par Query Parameter * @param string $geoCoderUrl Url of the geo location service * @param string $land language * * @return array|mixed|string */ public function __invoke($par, $geoCoderUrl, $land) { $client = new \Zend\Http\Client($geoCoderUrl); $client->setMethod('GET'); // more countries 'country' => 'DE,CH,AT' // with countryCode 'zoom' => 2 $plz = 0 < (int) $par ? 1 : 0; $client->setParameterGet(array('q' => $par, 'country' => 'DE', 'plz' => $plz, 'zoom' => 1)); $response = $client->send(); $result = $response->getBody(); $result = json_decode($result); $result = (array) $result->result; return $result; }
/** * This method call Bit.ly to shorten a given URL. * @param unknown_type $url * @return unknown */ public function shortenUrl($url) { if ($this->getOptions()->getBitlyApiKey() && $this->getOptions()->getBitlyUsername()) { $client = new \Zend\Http\Client($this->getOptions()->getBitlyUrl()); $client->setParameterGet(array('format' => 'json', 'longUrl' => $url, 'login' => $this->getOptions()->getBitlyUsername(), 'apiKey' => $this->getOptions()->getBitlyApiKey())); $result = $client->send(); if ($result) { $jsonResult = \Zend\Json\Json::decode($result->getBody()); if ($jsonResult->status_code == 200) { return $jsonResult->data->url; } } } return $url; }
public function __invoke($par) { $config = $this->getController()->getServiceLocator()->get('config'); if (empty($config['geocoder_cross_url'])) { throw new \InvalidArgumentException('Now Service-Adress for Geo-Service available'); } $client = new \Zend\Http\Client($config['geocoder_photon_url']); $client->setMethod('GET'); // more countries 'country' => 'DE,CH,AT' // with countryCode 'zoom' => 2 $plz = 0 < (int) $par ? 1 : 0; $client->setParameterGet(array('q' => $par, 'country' => 'DE', 'plz' => $plz, 'zoom' => 1)); $response = $client->send(); $result = $response->getBody(); $result = json_decode($result); $result = (array) $result->result; return $result; }
/** * Performs HTTP request to given $url using given HTTP $method. * Send additinal query specified by variable/value array, * On success returns HTTP response without headers, false on failure. * * @param string $url OpenID server url * @param string $method HTTP request method 'GET' or 'POST' * @param array $params additional qwery parameters to be passed with * @param int &$staus HTTP status code * request * @return mixed */ protected function _httpRequest($url, $method = 'GET', array $params = array(), &$status = null) { $client = $this->_httpClient; if ($client === null) { $client = new \Zend\Http\Client($url, array('maxredirects' => 4, 'timeout' => 15, 'useragent' => 'Zend_OpenId')); } else { $client->setUri($url); } $client->resetParameters(); if ($method == 'POST') { $client->setMethod(\Zend\Http\Client::POST); $client->setParameterPost($params); } else { $client->setMethod(\Zend\Http\Client::GET); $client->setParameterGet($params); } try { $response = $client->request(); } catch (\Exception $e) { $this->_setError('HTTP Request failed: ' . $e->getMessage()); return false; } $status = $response->getStatus(); $body = $response->getBody(); if ($status == 200 || $status == 400 && !empty($body)) { return $body; } else { $this->_setError('Bad HTTP response'); return false; } }
/** * Get comments on obalkyknih.cz associated with this record. * * @return array */ public function getObalkyKnihComments() { $isbnArray = $this->getBibinfoForObalkyKnihV3(); $isbnJson = json_encode($isbnArray); $client = new \Zend\Http\Client('http://cache.obalkyknih.cz/api/books'); $client->setParameterGet(array('multi' => '[' . $isbnJson . ']')); $response = $client->send(); $responseBody = $response->getBody(); $phpResponse = json_decode($responseBody, true); if (isset($phpResponse[0]['rating_url'])) { echo '<p>Hodnocení knihy:</p><img src="' . $phpResponse[0]['rating_url'] . '"/>'; } $commentArray = array(); $i = 0; foreach ($phpResponse[0]['reviews'] as $review) { $com = new \stdClass(); $com->firstname = $review->library_name; $com->created = $review->created; $com->comment = $review->html_text; $commentArray[$i] = $com; $i++; } return $commentArray; }
/** * 构造POST和GET组合的请求 返回相应请求 * * @param string $url * @param array $get * @param array $post * @param boolean $returnObj * @return Zend_Http_Response false */ function doRequest($url, $get = array(), $post = array(), $returnObj = false) { try { $url = trim($url); if (!filter_var($url, FILTER_VALIDATE_URL)) { throw new Exception('Invalid URL'); return false; } $client = new Zend\Http\Client(); $client->setUri($url); if (count($get) > 0 && is_array($get)) { $client->setParameterGet($get); } if (count($post) > 0 && is_array($post)) { $client->setParameterPost($post); } $client->setEncType(Zend\Http\Client::ENC_URLENCODED); $client->setOptions(array('maxredirects' => 5, 'strictredirects' => false, 'useragent' => 'Zend\\Http\\Client', 'timeout' => 10, 'adapter' => 'Zend\\Http\\Client\\Adapter\\Socket', 'httpversion' => Request::VERSION_11, 'storeresponse' => true, 'keepalive' => false, 'outputstream' => false, 'encodecookies' => true, 'argseparator' => null, 'rfc3986strict' => false)); if (!empty($post)) { $client->setMethod(Request::METHOD_POST); } else { $client->setMethod(Request::METHOD_GET); } $response = $client->send(); return $returnObj ? $response : $response->getBody(); } catch (Exception $e) { fb(exceptionMsg($e), \FirePHP::LOG); return false; } }