在php中使用Restful服务?

问题描述 投票:0回答:1

我正在尝试使用以下代码使用Restful服务:

$dat=array(
        'entidad' => "F001",'tipoIdentificacion' => 'CC','numeroIdentificacion' => '1020442757'
    );


$postdata =http_build_query( $dat );
    
//print_r($postdata);


$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-Type: application/x-www-form-urlencoded',
        'content' => $postdata
    )
);


$context  = stream_context_create($opts);
//var_dump($context);
$result=file_get_contents('http://172.18.131.195:9090/lince/rest/pazysalvo1/avaldescfin',FILE_USE_INCLUDE_PATH, $context);
//echo $result;
//$result = json_decode(file_get_contents('http://172.18.131.195:9090/lince/rest/pazysalvo1/avaldescfin?', false, $context),true);

print_r($result);

但是它无法识别我正在发送的参数,因此返回空值:

{"entidad":"F001","tipoIdentificacion":null,"numeroIdentificacion":null,"codPersona":0,"estadoConsulta":"1","avales":[]}

我应该如何发送参数?

php rest web-services
1个回答
0
投票

我无法以这种亚洲方式做到这一点,而以另一种方式给出了解决方案。

//set POST variables
$url = 'http://172.18.131.195:9090/lince/rest/pazysalvo1/avaldescfin';
$fields = array(
        'entidad' => 'F001','tipoIdentificacion' =>$tipoid,'numeroIdentificacion' => $numid
    );

//url-ify the data for the POST
foreach($fields as $key=>$value) 
{ 
    $fields_string .= $key.'='.$value.'&'; 
}
rtrim($fields_string, '&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);


//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

$result=json_decode($result,true);

/*echo "<pre>";
print_r($result);
echo "</pre>";*/
© www.soinside.com 2019 - 2024. All rights reserved.