public static function sendRequest($url, $postString = '', $method = \Zend\Http\Request::METHOD_POST) { $client = new \Zend\Http\Client(); $client->setAdapter(new \Zend\Http\Client\Adapter\Curl()); $request = new \Zend\Http\Request(); $request->setUri($url); $request->setMethod($method); if (is_array($postString)) { $params = $postString; $postString = ''; foreach ($params as $key => $value) { $postString .= $key . '=' . urlencode($value) . '&'; } $postString = substr($postString, 0, strlen($postString) - 1); } $request->setContent($postString); $response = $client->dispatch($request); return $response->getContent(); }
} if (count($incoming_urls) < 1) { $error->add(t('You must specify at least one valid URL.')); } } $import_responses = array(); $files = array(); // if we haven't gotten any errors yet then try to process the form if (!$error->has()) { // itterate over each incoming URL adding if relevant foreach ($incoming_urls as $this_url) { // try to D/L the provided file $request = new \Zend\Http\Request(); $request->setUri($this_url); $client = new \Zend\Http\Client(); $response = $client->dispatch($request); if ($response->isSuccess()) { $headers = $response->getHeaders(); $contentType = $headers->get('ContentType')->getFieldValue(); $fpath = $file->getTemporaryDirectory(); // figure out a filename based on filename, mimetype, ??? if (preg_match('/^.+?[\\/]([-\\w%]+\\.[-\\w%]+)$/', $request->getUri(), $matches)) { // got a filename (with extension)... use it $fname = $matches[1]; } elseif ($contentType) { // use mimetype from http response $fextension = Core::make("helper/mime")->mimeToExtension($contentType); if ($fextension === false) { $error->add(t('Unknown mime-type: %s', $contentType)); } else { // make sure we're coming up with a unique filename
public function getToken($data = null) { $config = $this->getConfig(); $request = new \Zend\Http\Request(); $request->setUri($config['zf-oauth2']['oauthServerUri']); $request->setMethod('POST'); $client = new \Zend\Http\Client(); $adapter = new \Zend\Http\Client\Adapter\Curl(); $client->setAdapter($adapter); $adapter->setOptions(array('curloptions' => array(CURLOPT_POST => 1, CURLOPT_POSTFIELDS => 'grant_type=client_credentials', CURLOPT_HTTPAUTH => CURLAUTH_BASIC, CURLOPT_USERPWD => $config['zf-oauth2']['client'] . ':' . $config['zf-oauth2']['client_pwd'], CURLOPT_RETURNTRANSFER => 1, CURLOPT_SSL_VERIFYPEER => FALSE, CURLOPT_SSL_VERIFYHOST => FALSE))); $response = $client->dispatch($request); // I need to extract only the JSon content preg_match('~\\{(?:[^{}]|(?R))*\\}~', $response->getContent(), $jsonArray); // From the JSonArray I only need the first element - 0 $response = json_decode($jsonArray[0], true); $token = $response['access_token']; // Instantiate the AccessTokens Controller in order to be able to update the record $OauthAccessTokensController = $this->getServiceLocator()->get('OauthAccessTokensController'); $OauthAccessTokensController->setValidateOAuth(false); $responseOAT = $OauthAccessTokensController->update($token, array(USERID => $data[USERID] . '')); if ($responseOAT->getVariable('status') == STATUS_FAILED) { // TODO - Log to admin, Something bad happened, this should work without any problem throw new \Exception($responseOAT->getVariable('msg')); } else { $response[USERID] = $data[USERID]; } /* if ( $OauthAccessTokensController->getResponse( )->getStatusCode( ) !== 200 ) { // TODO - Log to admin, Something bad happened, this should work without any problem throw new CommonException ( array( 'messageId' => 'globals.query.record_not_found' ,'parms' => array( 'id' => $token ) ,EXCEPTION_ERRORKEY => 404 ) ); } else { $response[ USERID ] = $data[ USERID ]; } */ return $response; }