时间:2021-07-01 10:21:17 帮助过:17人阅读
$post_url = "http://domain.com/post.php";
$post_data = array(
'color' => array('red', 'green', 'blue'),
'img' => "@d:\image.jpg"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, ($post_data));
if (false === curl_exec($ch)) {
echo "Curl_error: " . curl_error($ch);
}
curl_close($ch);
$post_data = array("method" => $_POST["method"],
"mode" => $_POST["mode"],
"product[name]" => $_POST["product"],
"product[cost]" => $_POST["product"]["cost"],
"product[thumbnail]" => "@{$_FILES["thumbnail"]["tmp_name"]}");
$post_url = "http://localhost/test/test1.php";
$post_data = array(
'a[1]' => 'red',
'b[2]' => 'red',
'c[3]' => 'red',
'e[1]' => "@d:\img.gif",
'e[2]' => "@d:\\2.gif"
);
//通过curl模拟post的请求;
function SendDataByCurl($url,$data=array()){
//对空格进行转义
$url = str_replace(' ','+',$url);
$ch = curl_init();
//设置选项,包括URL
curl_setopt($ch, CURLOPT_URL, "$url");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch,CURLOPT_TIMEOUT,3); //定义超时3秒钟
// POST数据
curl_setopt($ch, CURLOPT_POST, 1);
// 把post的变量加上
curl_setopt($ch, CURLOPT_POSTFIELDS, ($data)); //所需传的数组用http_bulid_query()函数处理一下,就ok了
//执行并获取url地址的内容
$output = curl_exec($ch);
$errorCode = curl_errno($ch);
//释放curl句柄
curl_close($ch);
if(0 !== $errorCode) {
return false;
}
return $output;
}
$url = "http://localhost/test/test1.php";
//通过curl的post方式发送接口请求
echo SendDataByCurl($url,$post_data);
?>