当前位置: 首页>>代码示例>>PHP>>正文


PHP Zend_Controller_Request_Abstract::dispatchError方法代码示例

本文整理汇总了PHP中Zend_Controller_Request_Abstract::dispatchError方法的典型用法代码示例。如果您正苦于以下问题:PHP Zend_Controller_Request_Abstract::dispatchError方法的具体用法?PHP Zend_Controller_Request_Abstract::dispatchError怎么用?PHP Zend_Controller_Request_Abstract::dispatchError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Zend_Controller_Request_Abstract的用法示例。


在下文中一共展示了Zend_Controller_Request_Abstract::dispatchError方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: handleRequestBody

 /**
  * PHP only parses the body into $_POST if its a POST request
  * this parses the reqest body in accordance with RFC2616 spec regardless of the HTTP method
  */
 private function handleRequestBody(Zend_Controller_Request_Abstract $request)
 {
     $header = strtolower($request->getHeader('Content-Type'));
     // cleanup the charset part
     $header = current(explode(';', $header));
     // detect request body content type
     foreach ($this->requestTypes as $contentType) {
         if ($header == $contentType) {
             break;
         }
     }
     // extract the raw body
     $rawBody = $request->getRawBody();
     // treat these two separately because of the way PHP treats POST
     if (in_array($contentType, array('multipart/form-data', 'application/x-www-form-urlencoded'))) {
         // PHP takes care of everything for us in this case lets just modify the $_FILES array
         if ($request->isPost() && $contentType == 'multipart/form-data') {
             // if there are files, lets modify the array to match what we've done below
             foreach ($_FILES as &$file) {
                 $data = file_get_contents($file['tmp_name']);
                 $file['content'] = base64_encode($data);
             }
             // reset the array pointer
             unset($file);
         } else {
             switch ($contentType) {
                 case 'application/x-www-form-urlencoded':
                     parse_str($rawBody, $_POST);
                     break;
                     // this is wher the magic happens
                     // creates the $_FILES array for none POST requests
                 // this is wher the magic happens
                 // creates the $_FILES array for none POST requests
                 case 'multipart/form-data':
                     // extract the boundary
                     parse_str(end(explode(';', $request->getHeader('Content-Type'))));
                     if (isset($boundary)) {
                         // get rid of the boundary at the edges
                         if (preg_match(sprintf('/--%s(.+)--%s--/s', $boundary, $boundary), $rawBody, $regs)) {
                             // split into chuncks
                             $chunks = explode('--' . $boundary, trim($regs[1]));
                             foreach ($chunks as $chunk) {
                                 // parse each chunk
                                 if (preg_match('/Content-Disposition: form-data; name="(?P<name>.+?)"(?:; filename="(?P<filename>.+?)")?(?P<headers>(?:\\r|\\n)+?.+?(?:\\r|\\n)+?)?(?P<data>.+)/si', $chunk, $regs)) {
                                     // dedect a file upload
                                     if (!empty($regs['filename'])) {
                                         // put aside for further analysis
                                         $data = $regs['data'];
                                         $headers = $this->parseHeaders($regs['headers']);
                                         // set our params variable
                                         $_FILES[$regs['name']] = array('name' => $regs['filename'], 'type' => $headers['Content-Type'], 'size' => mb_strlen($data), 'content' => base64_encode($data));
                                         // otherwise its a regular key=value combination
                                     } else {
                                         $_POST[$regs['name']] = trim($regs['data']);
                                     }
                                 }
                             }
                         }
                     }
                     break;
             }
         }
         $request->setParams($_POST + $_FILES);
     } elseif (!empty($rawBody)) {
         // seems like we are dealing with an encoded request
         try {
             switch ($contentType) {
                 case 'text/javascript':
                 case 'application/json':
                 case 'application/javascript':
                     $_POST = (array) Zend_Json::decode($rawBody, Zend_Json::TYPE_OBJECT);
                     break;
                 case 'text/xml':
                 case 'application/xml':
                     $json = @Zend_Json::fromXml($rawBody);
                     $_POST = (array) Zend_Json::decode($json, Zend_Json::TYPE_OBJECT)->request;
                     break;
                 case 'text/php':
                 case 'application/x-httpd-php':
                 case 'application/x-httpd-php-source':
                     $_POST = (array) unserialize($rawBody);
                     break;
                 default:
                     $_POST = (array) $rawBody;
                     break;
             }
             $request->setParams($_POST);
         } catch (Exception $e) {
             $request->dispatchError(REST_Response::BAD_REQUEST, 'Invalid Payload Format');
             return;
         }
     }
 }
开发者ID:netconstructor,项目名称:restful-zend-framework,代码行数:97,代码来源:RestHandler.php


注:本文中的Zend_Controller_Request_Abstract::dispatchError方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。