Zend_HTTP_Client不会让我发布任何数据

129*_*291 6 php web-services zend-framework zend-http-client

我一直在搜索stackoverflow和Google以解决我的问题.

我用Zend Framework创建了两个项目 - Project1而且Project2- 我希望在其中一个项目上实现Web服务.我们的想法是Project1使用POST 向JSON字符串发送并接收一个JSON,其中包含与该变量相关的所有详细信息.现在我已经创建了一个TestController 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();

}
Run Code Online (Sandbox Code Playgroud)

上面的代码有效.它dataActionProject1控制器读取并给出一个回显的输出.但是,当我尝试这个:

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();

}
Run Code Online (Sandbox Code Playgroud)

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

public function dataAction(){

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

    var_dump($_POST);

    die();      

}
Run Code Online (Sandbox Code Playgroud)

我得到一个输出:NULL数组(0){} ....当我在客户端尝试它时,我得到相同的输出.另外要说..我也尝试打开php://输入文件,但得到一个空字符串...

我错过了什么?我从早上开始对自己进行搜索感到沮丧,但没有解决方案.

提前感谢您的回复.

dre*_*010 5

这是你缺少的:

$json = json_encode($data);
$client->setRawData($json, 'application/json')->request('POST');
Run Code Online (Sandbox Code Playgroud)

发送POST请求但POST主体中的数据不是url编码的字符串,而是原始的JSON.

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

dataAction如果您想在POST正文中接收JSON数据,解决方案是使用类似的东西.

$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.";
}
Run Code Online (Sandbox Code Playgroud)

编辑:这是你可以搞砸的完整代码.

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;
}
Run Code Online (Sandbox Code Playgroud)