一、post传递数据
$ci = curl_init($url);
curl_setopt($ci, CURLOPT_HEADER, 0);
curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ci, CURLOPT_POST, 1);
curl_setopt($ci,CURLOPT_POSTFIELDS,$data);
$return = curl_exec($ci);
echo $return;
curl_close($ch);
二、get传递数据
$url="www.baidu.com?id=1";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
echo $response;
三、post传递数据给java
(一)单个键值对
$url="www.baidu.com";
$requestApi = curl_init();
curl_setopt($requestApi, CURLOPT_URL, $url);
curl_setopt($requestApi, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($requestApi, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($requestApi, CURLOPT_HEADER, 0); //将头文件的信息作为数据流输出
$header[] = 'Content-type:application/x-www-form-urlencoded';
curl_setopt($requestApi, CURLOPT_HTTPHEADER, $header);
curl_setopt($requestApi, CURLOPT_POST, 1);
$arr=array(
'name'=>'huahua',
'age'=>20
);
$data = http_build_query($arr);
curl_setopt($requestApi, CURLOPT_POSTFIELDS, $data);
curl_setopt($requestApi, CURLOPT_RETURNTRANSFER, 1); //返回获取的输出文本流
$result = curl_exec($requestApi);
curl_close($requestApi);
(二)整个键值对
$url="www.baidu.com";
$requestApi = curl_init();
curl_setopt($requestApi, CURLOPT_URL, $url);
curl_setopt($requestApi, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($requestApi, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($requestApi, CURLOPT_HEADER, 0); //将头文件的信息作为数据流输出
$header[] = 'Content-type:application/x-www-form-urlencoded';
curl_setopt($requestApi, CURLOPT_HTTPHEADER, $header);
curl_setopt($requestApi, CURLOPT_POST, 1);
$arr=array(
'name'=>'huahua',
'age'=>20
);
$postData = [
'postData' => json_encode($arr)
];
$data = http_build_query($postData);
curl_setopt($requestApi, CURLOPT_POSTFIELDS, $data);
curl_setopt($requestApi, CURLOPT_RETURNTRANSFER, 1); //返回获取的输出文本流
$result = curl_exec($requestApi);
curl_close($requestApi);