Consultez la FAQ sur le ZF avant de poster une question
Vous n'êtes pas identifié.
Bonjour à tous,
Voilà je veux convertir le code suivant en quelque chose de plus zend compliant :
Code d'origine :
[lang=php]protected function executeRequest($url, $curl_params = array()) { $defaultParams = array( CURLOPT_HEADER => TRUE, CURLOPT_RETURNTRANSFER => TRUE, CURLINFO_HEADER_OUT => TRUE, CURLOPT_HTTPAUTH => CURLAUTH_BASIC, CURLOPT_ENCODING=> 'UTF-8', CURLOPT_USERPWD => $this->key.':', CURLOPT_HTTPHEADER => array( 'Expect:' ) ); $session = curl_init($url); $curl_options = array(); foreach ($defaultParams as $defkey => $defval) { if (isset($curl_params[$defkey])) $curl_options[$defkey] = $curl_params[$defkey]; else $curl_options[$defkey] = $defaultParams[$defkey]; } foreach ($curl_params as $defkey => $defval) if (!isset($curl_options[$defkey])) $curl_options[$defkey] = $curl_params[$defkey]; curl_setopt_array($session, $curl_options); $response = curl_exec($session); $index = strpos($response, "\r\n\r\n"); if ($index === false && $curl_params[CURLOPT_CUSTOMREQUEST] != 'HEAD') throw new PrestaShopWebserviceException('Mauvaise requête.'); $header = substr($response, 0, $index); $body = substr($response, $index + 4); $headerArrayTmp = explode("\n", $header); $headerArray = array(); foreach ($headerArrayTmp as &$headerItem) { $tmp = explode(':', $headerItem); $tmp = array_map('trim', $tmp); if (count($tmp) == 2) $headerArray[$tmp[0]] = $tmp[1]; } if (array_key_exists('PSWS-Version', $headerArray)) { if ( version_compare(PrestaShopWebservice::psCompatibleVersionsMin, $headerArray['PSWS-Version']) == 1 || version_compare(PrestaShopWebservice::psCompatibleVersionsMax, $headerArray['PSWS-Version']) == -1 ) throw new \Exception('Librairie incompatible avec cette version de Prestashop.'); } if ($this->debug) { $this->printDebug('HTTP REQUEST HEADER', curl_getinfo($session, CURLINFO_HEADER_OUT)); $this->printDebug('HTTP RESPONSE HEADER', $header); } $status_code = curl_getinfo($session, CURLINFO_HTTP_CODE); if ($status_code === 0) throw new PrestaShopWebserviceException('CURL Error: '.curl_error($session)); curl_close($session); if ($this->debug) { if ($curl_params[CURLOPT_CUSTOMREQUEST] == 'PUT' || $curl_params[CURLOPT_CUSTOMREQUEST] == 'POST') $this->printDebug('XML SENT', $curl_params[CURLOPT_POSTFIELDS]); if ($curl_params[CURLOPT_CUSTOMREQUEST] != 'DELETE' && $curl_params[CURLOPT_CUSTOMREQUEST] != 'HEAD') $this->printDebug('RETURN HTTP BODY', $body); } return array('status_code' => $status_code, 'response' => $body, 'header' => $header); }[/lang]
Voici le code que j'ai proposé sauf qu'à l'ajout j'ai une bonne erreur 500, et pas au listing qui marche nickel :
[lang=php]protected function executeRequest($url, $curl_params = array()) { //var_dump($curl_params); $request = new \Zend\Http\Request(); $request->setUri($url); $request->setMethod($curl_params['10036']); $client = new \Zend\Http\Client(); $adapter = new \Zend\Http\Client\Adapter\Curl(); $client->setAdapter($adapter); if($curl_params['10036'] == "GET"){ $adapter->setOptions(array( 'curloptions' => array( CURLOPT_HEADER => TRUE, CURLOPT_RETURNTRANSFER => TRUE, CURLINFO_HEADER_OUT => TRUE, CURLOPT_HTTPAUTH => CURLAUTH_BASIC, CURLOPT_ENCODING=> 'UTF-8', CURLOPT_USERPWD => $this->key.':', CURLOPT_HTTPHEADER => array( 'Expect:' ) ) )); } elseif($curl_params['10036'] == "POST"){ $adapter->setOptions(array( 'curloptions' => array( CURLOPT_HEADER => TRUE, CURLOPT_RETURNTRANSFER => TRUE, CURLINFO_HEADER_OUT => TRUE, CURLOPT_HTTPAUTH => CURLAUTH_BASIC, CURLOPT_ENCODING=> 'UTF-8', CURLOPT_USERPWD => $this->key.':', CURLOPT_HTTPHEADER => array( 'Expect:' ), CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => 'xml='.$curl_params['10015'] ) )); } $response = $client->dispatch($request); //var_dump($response); $index = strpos($response, "\r\n\r\n"); if ($index === false && $curl_params[CURLOPT_CUSTOMREQUEST] != 'HEAD') throw new PrestaShopWebserviceException('Mauvaise requête.'); $header = substr($response, 0, $index); $body = substr($response, $index + 4); $headerArrayTmp = explode("\n", $header); $headerArray = array(); foreach ($headerArrayTmp as &$headerItem) { $tmp = explode(':', $headerItem); $tmp = array_map('trim', $tmp); if (count($tmp) == 2) $headerArray[$tmp[0]] = $tmp[1]; } if (array_key_exists('PSWS-Version', $headerArray)) { if ( version_compare(PrestaShopWebservice::psCompatibleVersionsMin, $headerArray['PSWS-Version']) == 1 || version_compare(PrestaShopWebservice::psCompatibleVersionsMax, $headerArray['PSWS-Version']) == -1 ) throw new \Exception('Librairie incompatible avec cette version de Prestashop.'); } if ($this->debug) { $this->printDebug('HTTP RESPONSE HEADER', $header); } $status_code = $response->getStatusCode(); if ($status_code === 0) throw new PrestaShopWebserviceException('CURL Error ! '); if ($this->debug) { if ($curl_params[CURLOPT_CUSTOMREQUEST] == 'PUT' || $curl_params[CURLOPT_CUSTOMREQUEST] == 'POST') $this->printDebug('XML SENT', $curl_params[CURLOPT_POSTFIELDS]); if ($curl_params[CURLOPT_CUSTOMREQUEST] != 'DELETE' && $curl_params[CURLOPT_CUSTOMREQUEST] != 'HEAD') $this->printDebug('RETURN HTTP BODY', $body); } return array('status_code' => $status_code, 'response' => $body, 'header' => $header); }[/lang]
Merci pour vos éclaircissements ...
Dernière modification par amelie (14-02-2013 16:31:52)
Hors ligne