dongzhou1865 2018-10-17 04:48
浏览 75

尝试在cloud9中使用REST服务器时出现“错误502 - 错误网关”

I have a simple working RESTful client/server application on my desktop(XAMPP).

I recently moved to Cloud9.

But I am getting "Error 502- Bad Gateway".

Here is the file structure:

my_php
      PHP_Study/
               WebServices/
                           Client/
                                  index.php
                          Server/
                                  index.php

APP_ROOT_PATH is set to /home/ubuntu/workspace/PHP_Study.

This is the code for Client/index.php

<html>
<body>
<form method="POST" action="" >
    <input type="text" name="name" >
    </br>
    <input type="submit" name="submit">
</form>
</body>
</html>

<?php

 if ( isset( $_POST['submit'] ) )
 {
    //echo "I am inside <br>";
    $RootPath = getenv('APP_ROOT_PATH');
    echo 'RootPath = ' . $RootPath . '<br>';
    $name = $_POST['name'];

    //$url = "https://my-php-dguai.c9users.io/PHP_Study/WebServices/?book=$name";
    //$url = "https://my-php-dguai.c9users.io/my_php/PHP_Study/WebServices/?book=$name";
    //$url = "https://my-php-dguai.c9users.io/WebServices/Server/?book=$name";
    //$url = "https://my-php-dguai.c9users.io/my_php/PHP_Study/WebServices/Server/Index.php?book=$name";
    //$url = "https://ide.c9.io/dguai/my_php/PHP_Study/WebServices/Server/Index.php?book=$name";
    //$url = "https://dguai-my-php-3600949:8080/ide.c9.io/dguai/my_php/PHP_Study/WebServices/Server/Index.php?book=$name";
     $url = "https://dguai-my-php-3600949:8080/WebServices/Server/Index.php?book=$name";

     $client = curl_init( $url );
     curl_setopt( $client, CURLOPT_RETURNTRANSFER, 1 ); 
     $response = curl_exec( $client );
     var_dump($response);
     //echo $response;
     $output =  json_decode( $response );
     echo "It is listed $" . $output->data;
 }

?>

Here is code for Server/index.php

include "functions.php";

if( !empty( $_GET['book'] ) )
{

   $price = getPrice( $_GET['book'] );

   if( empty( $price ) )
   {
       deliver_response( 200, "Book NOT found", NULL );
   }
   else
   {
      deliver_response( 200, "Book found", $price );
   } 
 }
 else
 {
    deliver_response( 400, "Input missing", NULL );
 }

 function deliver_response( $status, $status_message, $data )
 {
     header( "HTTP/1.1 $status $status_message" );   

     $response['status'] = $status;
     $response['status_message'] = $status_message;
     $response['data'] = $data;

     $json_response = json_encode( $response );

     echo $json_response;

  }
 ?>

Server/functions.php

 function getPrice( $findBook )
 {
    $Books = array( 
                    "Java" => 30,
                    "C" => 25,
                    "PHP" => 40,
                );

    foreach( $Books as $Book=>$Price )
    {
        if( $findBook == $Book )
        {
           return $Price;
           return;
        }
    }
}

?>

Thank you.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看