从php发布到Flask

时间:2015-06-26 14:43:30

标签: php python curl flask

enter image description here

我有一个php应用程序,它将变量信息发布到烧瓶应用程序(进行一些计算并返回结果)。我在win7上本地运行

当我使用邮递员的帖子测试网址“127.0.0.1:5000/index”时,我获得了200状态代码(屏幕截图)。然而,当php应用程序发布到烧瓶应用程序时,我得到:

The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

我正在使用CURL,详细输出是:

* About to connect() to 127.0.0.1 port 5000 (#0)
*   Trying 127.0.0.1...
* connected
* Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0)
> POST /index/ HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)
Host: 127.0.0.1:5000
Accept: */*
Content-Length: 338
Expect: 100-continue
Content-Type: multipart/form-data; boundary=----------------------------d5cb02e2edea
< HTTP/1.1 100 Continue
* HTTP 1.0, assume close after body
< HTTP/1.0 404 NOT FOUND
< Content-Type: text/html
< Content-Length: 233
< Server: Werkzeug/0.10.4 Python/2.7.5

我的php代码如下:

   $data= array('a'=>$a, 'token'=>$token);
   $url="http://127.0.0.1:5000/index/";
   $output = $this->my_model->get_data($url, $data);

public function get_data($url,$postFieldArray=FALSE) {

$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)");
if ($postFieldArray!= FALSE) {
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postFieldArray); //for django
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_STDERR, $verbose);
curl_setopt($ch, CURLOPT_URL, $url);
$html = curl_exec($ch);
curl_close($ch);
.......

return $result;
}

Simplified Flask app:

app = Flask(__name__)
app.debug = True

@app.route('/')
def hello_world():
    return 'Hello World!'

@app.route('/index',methods=['POST'])
def index():

    token = request.form['token']
    a = request.form['a']
    ......
    return

if __name__ == '__main__':
    app.run()

我做错了什么?

1 个答案:

答案 0 :(得分:1)

PHP代码中的$url变量中有一个尾部斜杠。这不会起作用,因为你的Flask代码中没有斜杠。查看here以获取更多信息,请参阅&#34;唯一网址/重定向行为&#34;