Zend_HTTP_Client不会让我发布任何数据 [英] Zend_HTTP_Client wont let me POST any data

查看:92
本文介绍了Zend_HTTP_Client不会让我发布任何数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在搜索stackoverflow和Google方面的问题的解决方案.

I have been searching all over stackoverflow and Google for a solution to my problem.

我已经用Zend Framework创建了两个项目-Project1Project2-我想在其中一个项目上实现Web服务.这个想法是将JSON字符串发送到Project1并使用POST接收返回与该变量关联的所有详细信息的JSON.现在,我在Project2上创建了一个TestController:

I have created two projects with Zend Framework - Project1 and Project2 - and I want to implement web services on one of them. The idea is to send a JSON-string to Project1 and receive back a JSON with all the details associated with that variable using POST. Now I have created a TestController on Project2:

public function indexAction(){

    $uri = 'http://project1.com/WebService/data';

    $config = array(
        'adapter'   => 'Zend_Http_Client_Adapter_Curl',
        'curloptions' => array(CURLOPT_FOLLOWLOCATION => true),
    );
    $client = new Zend_Http_Client($uri, $config);

    $request = $client->request('POST');

    print_r($request->getBody());

    exit();

}

上面的代码有效.它从Project1控制器读取dataAction,并为我提供回显内容的输出.但是当我尝试这样做时:

The above code works. It reads the dataAction from the Project1 controller and gives me an output of whatever is echoed. But when I try this:

public function indexAction(){

    $uri = 'http://project1.com/WebService/data';

    $config = array(
        'adapter'   => 'Zend_Http_Client_Adapter_Curl',
        'curloptions' => array(CURLOPT_FOLLOWLOCATION => true),
    );
    $client = new Zend_Http_Client($uri, $config);

    $data = array(
            'userID'      => 'TEST TEST',
            'value'       => 1,
            'description' => 'ABCDEFG',
    );

    $request = $client->request('POST');

            $json = json_encode($data);

            $client->setRawData($json, 'application/json')->request('POST');

    exit();

}

在服务器端,当我尝试在dataAction内部显示时:

And on the server side when I try displaying inside dataAction:

public function dataAction(){

    var_dump($this->getRequest()->getParam('var-name'));

    var_dump($_POST);

    die();      

}

我得到这样的输出:NULL array(0){}....当我在客户端尝试它时,我得到相同的输出.另外要提..我也尝试打开php://input文件,但是得到了一个空字符串...

I get an output of this: NULL array(0) { } .... I get the same output when I try it on the client side. Also to mention.. I also tried opening the php://input file but got an empty string...

我想念什么???自从早上以来,我一直在寻找它,这让我很沮丧,但是没有解决办法.

What am I missing??? I have frustrated myself searching on it since morning but got no solution.

提前感谢您的回复.

推荐答案

以下是您缺少的内容:

$json = json_encode($data);
$client->setRawData($json, 'application/json')->request('POST');

发送POST请求,但POST正文中的数据不是url编码的字符串,而是纯JSON.

sends a POST request but the data in the POST body is not a url-encoded string, instead it is just raw JSON.

调用$this->getRequest()->getParam('foo')会查看PHP超全局变量$_GET$_POST,它们将不包含任何JSON参数.之所以为空是因为PHP无法解析POST数据,因为它是JSON而不是HTTP url编码的内容.

Calling $this->getRequest()->getParam('foo') looks at the PHP superglobals $_GET and $_POST which will not contain any of the JSON parameters. The reason it will be empty is because PHP couldn't parse the POST data since it was JSON and not HTTP url-encoded content.

解决方案是,如果要在POST正文中接收JSON数据,请在dataAction中使用类似的内容.

The solution is to use something like this in the dataAction if you want to receive JSON data in the POST body.

$post = $this->getRequest()->getRawBody();

try {
    $json = Zend_Json::decode($post);

    // now access parameters from $json array
} catch (Zend_Json_Exception $ex) {
    echo "Failed to decode request, POST did not contain valid JSON.";
}

这是您可以弄乱的完整代码.

Here is the full code you can mess with.

public function requestAction()
{
    // CHANGE THIS
    $uri = 'http://playground/zendapp/public/index/data';

    $config = array(
            'adapter'   => 'Zend_Http_Client_Adapter_Curl',
            'curloptions' => array(CURLOPT_FOLLOWLOCATION => true),
    );
    $client = new Zend_Http_Client($uri, $config);

    $data = array(
            'userID'      => 'TEST TEST',
            'value'       => 1,
            'description' => 'ABCDEFG',
    );

    $json = json_encode($data);

    $resp = $client->setRawData($json, 'application/json')->request('POST');

    var_dump($resp->getBody());

    exit();

}

public function dataAction()
{
    $post = $this->getRequest()->getRawBody();

    try {
        $json = Zend_Json::decode($post);

        print_r($json);
    } catch (Exception $ex) {
        echo "failed to decode json";
    }

    exit;
}

这篇关于Zend_HTTP_Client不会让我发布任何数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆