簡體   English   中英

消耗使用PHP創建的Rest WebService

[英]Consuming a Rest WebService created using PHP

我在互聯網上發現了PHP中非常簡單的REST WebService

<?php
class MVRest {

    private $supportedMethods;

    public function __construct($supportedMethods) {
        $this->supportedMethods = $supportedMethods;
    }

    public function handleRawRequest($_SERVER, $_GET, $_POST) {
       $url = $this->getFullUrl($_SERVER);
       $method = $_SERVER['REQUEST_METHOD'];
       switch ($method) {
              case 'GET':
          case 'HEAD':
            $arguments = $_GET;
            break;
          case 'POST':
            $arguments = $_POST;
            break;
          case 'PUT':
          case 'DELETE':
            parse_str(file_get_contents('php://input'), $arguments);
            break;
        }
        $accept = $_SERVER['HTTP_ACCEPT'];
        $this->handleRequest($url, $method, $arguments, $accept);
      }

      protected function getFullUrl($_SERVER) {
        $protocol = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
        $location = $_SERVER['REQUEST_URI'];
        if ($_SERVER['QUERY_STRING']) {
          $location = substr($location, 0, strrpos($location, $_SERVER['QUERY_STRING']) - 1);
        }
        return $protocol.'://'.$_SERVER['HTTP_HOST'].$location;
      }

      public function handleRequest($url, $method, $arguments, $accept) {
        switch($method) {
          case 'GET':
            $this->performGet($url, $arguments, $accept);
            break;
          case 'HEAD':
            $this->performHead($url, $arguments, $accept);
            break;
          case 'POST':
            $this->performPost($url, $arguments, $accept);
            break;
          case 'PUT':
            $this->performPut($url, $arguments, $accept);
            break;
          case 'DELETE':
            $this->performDelete($url, $arguments, $accept);
            break;
          default:
            /* 501 (Not Implemented) for any unknown methods */
            header('Allow: ' . $this->supportedMethods, true, 501);
        }
      }

      protected function methodNotAllowedResponse() {
        /* 405 (Method Not Allowed) */
        header('Allow: ' . $this->supportedMethods, true, 405);
      }

      public function performGet($url, $arguments, $accept) {    
        echo $url.'<br />';
        print_r($arguments);
        echo $accept.'<br />';
      }

      public function performHead($url, $arguments, $accept) {
        $this->methodNotAllowedResponse();
      }

      public function performPost($url, $arguments, $accept) {
        $fp = fopen('data.txt', 'w');
        fwrite($fp, '1');
        fwrite($fp, '23');
        fclose($fp);
        echo 'GOTTED';
      }

      public function performPut($url, $arguments, $accept) {
        $this->methodNotAllowedResponse();
      }

      public function performDelete($url, $arguments, $accept) {
        $this->methodNotAllowedResponse();
      }

    }

    $rest = new MVRest("GET, POST");
    $rest->handleRawRequest($_SERVER, $_GET, $_POST);

我使用jQuery消費,並且效果很好,但是當我使用android消費時。 我得到一個IOException。 io.getMessage()返回了mydomain.com。

我的Android代碼。 有什么問題嗎?

    HttpPost postMethod = new HttpPost("http://mydomain.com/rest.php");
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("name","paulo"));
    nameValuePairs.add(new BasicNameValuePair("last_name","fernandes"));
    try {  
        postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        DefaultHttpClient hc = new DefaultHttpClient();
        HttpResponse response = hc.execute(postMethod);
        Toast.makeText(context, response.getStatusLine().toString(), Toast.LENGTH_LONG).show();
    } catch (ClientProtocolException e) {  
        Toast.makeText(context, "1: "+e.getMessage(), Toast.LENGTH_LONG).show();
        e.printStackTrace();  
    } catch (IOException e) {  
        Toast.makeText(context, "2: "+e.getMessage(), Toast.LENGTH_LONG).show();
        e.printStackTrace();  
    }  
    Toast.makeText(context, result, Toast.LENGTH_LONG).show();

謝謝

似乎是標題問題,可能是您的應用響應正常200

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM