0

尝试使用 curl 函数从 php 客户端调用 java 服务。我的 api 期待一个像下面这样的 json 请求正文,

 {"personRequest":{"emailId":"rupanjan@gmail.com","isActive":"true"}}

而我的php客户端代码如下,

    $postData['personRequest'] = array(
        'emailId' => $emailId,
        'isActive' => "true"
    );


    //$data = Array();
    //$data['personRequest']['isActive'] = "true";
    //$postFields = json_encode($data);

    $postFields = json_encode($postData);

    $url = "$server_url" . "service/membership/update";
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($postFields))
    );

    $result = curl_exec($ch);

    var_dump(curl_getinfo($ch));
    curl_close($ch);

    echo $ch;
    print_r($result);

执行后它显示一个空白响应,我也可以访问服务器代码,试图在我的 java 代码中放置一个调试点,似乎 http 调用根本没有到达我的服务器..

刚才执行了 var_dump(curl_getinfo($ch)); 得到以下输出,array(22) { ["url"]=> string(66) " http://9splatform.com/sample-server/service/membership/update " ["content_type"]=> NULL ["http_code "]=> int(302) ["header_size"]=> int(306) ["request_size"]=> int(215) ["filetime"]=> int(-1) ["ssl_verify_result"]=> int (0) ["redirect_count"]=> int(0) ["total_time"]=> float(0.004441) ["namelookup_time"]=> float(0.000245) ["connect_time"]=> float(0.000705) ["pretransfer_time "]=> 浮动(0.000714) ["size_upload"]=> 浮动(65) ["size_download"]=> 浮动(0) ["speed_download"]=> 浮动(0) ["speed_upload"]=>http://9splatform.com/sample-server/login.html;jsessionid=A32AF520FE766BB5D444823D1419F4M9 “}

这意味着这是重定向到登录页面,任何人都可以帮助我......我不知道为什么会这样......

4

2 回答 2

0
in LOGIN page
================

$url = $server_url . "login.html";
        $postdata = http_build_query(array(
            'username' => $username,
            'password' => $password
                ));

        $tmp_fname = tempnam("/tmp", "COOKIE");
        $userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6";

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $tmp_fname);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
        curl_setopt($ch, CURLOPT_TIMEOUT, 120);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/x-www-form-urlencoded',
            'Accept: application/json')
        );
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
        $result = curl_exec($ch);
        curl_close($ch);


in SUBSEQUENT requests
===========================

I have to add the cookies for every protected resource call...

$id = $this->input->post('id');
        $emailId = $this->input->post('emailId');
        $password = $this->input->post('password');
        $tmp_fname = $this->session->userdata('cookies_file');
        $userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6";

        $personObj['personRequest'] = array(
            'emailId' => $emailId,
            'isActive' => "true"
        );

        $postdata = json_encode($personObj);


        $url = $server_url . 'service/membership/update';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $tmp_fname);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
        curl_setopt($ch, CURLOPT_TIMEOUT, 120);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Accept: application/json')
        );
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
        $result = curl_exec($ch);
        curl_close($ch);
于 2013-11-06T13:47:59.030 回答
0

你应该使用curl_setopt($ch, CURLOPT_POST, 1);而不是curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");

如果上述方法不起作用,请完全按照此操作

<?php
    $postData['personRequest'] = array(
        'emailId'=>"rupanjan@gmail.com",
        'isActive' => "true"
    );

    $postFields = json_encode($postData['personRequest']); /JSON encoding the array

    $url = "$server_url" . "service/membership/update";
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$postFields);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //Removed the header... 
    $result = curl_exec($ch);
    curl_close($ch);

    echo $result;
于 2013-11-04T17:56:41.427 回答