<?php // create curl resource $ch = curl_init(); // set url curl_setopt($ch, CURLOPT_URL, "baidu.com"); //return the transfer as a string curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // $output contains the output string $output = curl_exec($ch); //echo output echo $output; // close curl resource to free up system resources curl_close($ch); ?>
//通过curl进行GET请求的案例 <?php // create curl resource $ch = curl_init(); // set url curl_setopt($ch, CURLOPT_URL, "https://github.com/search?q=react"); //return the transfer as a string curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // $output contains the output string $output = curl_exec($ch); //echo output echo $output; // close curl resource to free up system resources curl_close($ch); ?>
//testRespond.php
<?php
$phpInput=file_get_contents('php://input');
echo urldecode($phpInput);
?>
<?php $data=array( "name" => "Lei", "msg" => "Are you OK?" ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://测试服务器的IP马赛克/testRespond.php"); curl_setopt($ch, CURLOPT_POST, 1); //The number of seconds to wait while trying to connect. Use 0 to wait indefinitely. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); curl_setopt($ch, CURLOPT_POSTFIELDS , http_build_query($data)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); echo $output; curl_close($ch); ?>
<?php
$data='{"name":"Lei","msg":"Are you OK?"}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://测试服务器的IP马赛克/testRespond.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length:' . strlen($data)));
curl_setopt($ch, CURLOPT_POSTFIELDS , $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
echo $output;
curl_close($ch);
?>
<?php
if($_FILES){
$filename = $_FILES['upload']['name'];
$tmpname = $_FILES['upload']['tmp_name'];
//保存图片到当前脚本所在目录
if(move_uploaded_file($tmpname,dirname(__FILE__).'/'.$filename)){
echo ('上传成功');
}
}
?>
<?php
$data = array('name'=>'boy', "upload"=>"@boy.png");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://远程服务器地址马赛克/testRespond.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_POSTFIELDS , $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
echo $output;
curl_close($ch);
?>
<?php
$data = array('name'=>'boy', "upload"=>"");
$ch = curl_init();
$data['upload']=new CURLFile(realpath(getcwd().'/boy.png'));
curl_setopt($ch, CURLOPT_URL, "http://115.29.247.189/test/testRespond.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_POSTFIELDS , $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
echo $output;
curl_close($ch);
?>
<?php
$ch = curl_init();
$fp=fopen('./girl.jpg', 'w');
curl_setopt($ch, CURLOPT_URL, "http://远程服务器地址马赛克/girl.jpg");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_FILE, $fp);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
fclose($fp);
$size = filesize("./girl.jpg");
if ($size != $info['size_download']) {
echo "下载的数据不完整,请重新下载";
} else {
echo "下载数据完整";
}
curl_close($ch);
?>
function curl_auth($url,$user,$passwd){
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_USERPWD => $user.':'.$passwd,
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true
]);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$authurl = 'http://要请求HTTP认证的地址';
echo curl_auth($authurl,'vace','passwd');
<?php //设置post的数据 $post = array ( 'email' => '账户', 'pwd' => '密码' ); //登录地址 $url = "登陆地址"; //设置cookie保存路径 $cookie = dirname(__FILE__) . '/cookie.txt'; //登录后要获取信息的地址 $url2 = "登陆后要获取信息的地址"; //模拟登录 login_post($url, $cookie, $post); //获取登录页的信息 $content = get_content($url2, $cookie); //删除cookie文件 @ unlink($cookie); var_dump($content); ?>
login_post($url, $cookie, $post)
get_content($url2, $cookie)
//模拟登录
function login_post($url, $cookie, $post) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
curl_exec($curl);
curl_close($curl);
}
//登录成功后获取数据
function get_content($url, $cookie) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
$rs = curl_exec($ch);
curl_close($ch);
return $rs;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有