/**
* @param $url 请求网址
* @param bool $params 请求参数
* @param int $ispost 请求方式
* @param int $https https协议
* @return bool|mixed
*/
public static function get($url, $params = false, $ispost = 0, $https = 0)
{
$httpInfo = array();
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($https) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在
}
if ($ispost) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_URL, $url);
} else {
if ($params) {
if (is_array($params)) {
$params = http_build_query($params);
}
curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
} else {
curl_setopt($ch, CURLOPT_URL, $url);
}
}
$response = curl_exec($ch);
if ($response === FALSE) {
//echo "cURL Error: " . curl_error($ch);
return false;
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$httpInfo = array_merge($httpInfo, curl_getinfo($ch));
curl_close($ch);
return $response;
}
原文链接:https://learnku.com/articles/2768/easy-to-use-curl-class
$result = curl::get("http://172.16.112.3/api.php",'', true);
$index=json_decode($result);
return $index;
$.ajax({
async: true,
url: "http://172.16.112.3/api.php",
type: "GET",
dataType: "jsonp", // 返回的数据类型,设置为JSONP方式
jsonp: 'callback', //指定一个查询参数名称来覆盖默认的 jsonp 回调参数名 callback
jsonpCallback: 'handleResponse', //设置回调函数名
success: function (response, status, xhr) {
console.log('状态为:' + status + ',状态是:' + xhr.statusText);
console.log(response);
}
});
window.onload = function () {
function jsonp(obj) {
//定义一个处理Jsonp返回数据的回调函数
window["callback"] = function (object) {
obj.success(object);
}
var script = document.createElement("script");
//组合请求URL
script.src = obj.url + "?callback=callback";
for (key in obj.data) {
script.src += "&" + key + "=" + obj.data[key];
}
//将创建的新节点添加到BOM树上
document.getElementsByTagName("body")[0].appendChild(script);
}
jsonp({
url: "http://172.16.112.3/api.php",
success: function (obj) {
console.log(obj);
}
});
}
参考文档: https://guzzle-cn.readthedocs.io/zh_CN/latest/overview.html#installation
setAttribute() 这一行是强制性的,它会告诉 PDO 禁用模拟预处理语句,并使用 real parepared statements 。这可以确保 SQL 语句和相应的值在传递到 mysql 服务器之前是不会被 PHP 解析的(禁止了所有可能的恶意 SQL 注入攻击)。虽然你可以配置文件中设置 字符集的属性 (charset=utf8),但是需要格外注意的是,老版本的 PHP( < 5.3.6)在 DSN 中是忽略字符参数的。
我们来看一段完整的代码使用实例:
$user=$_POST['user']; $pass=$_POST['pass'];
$dbh = new \PDO("mysql:host=localhost; dbname=zz", "root", "root");
$dbh->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
//禁用prepared statements的仿真效果
// $dbh->exec ("set names 'utf8'");
$sql="select * from test where user=? and pass=?";
$stmt = $dbh->prepare($sql);
$exeres = $stmt->execute(array($user, $pass));
if ($exeres) {
//while条件为真时,输出$row,
while
($row = $stmt->fetch(\PDO::FETCH_ASSOC)){
print_r($row);die();
} //失败输出登录失败
print_r("登录失败");die();
}
当调用 prepare () 时,查询语句已经发送给了数据库服务器,此时只有占位符?发送过去,没有用户提交的数据;当调用到 execute () 时,用户提交过来的值才会传送给数据库,他们是分开传送的,两者独立的,SQL 攻击者没有一点机会。
以下几种情况,PDO 并不能帮助你防范 SQL 注入
你不能让占位符?代替一组值,如:
SELECT * FROM blog WHERE userid IN ( ? );
你不能让占位符代替数据表名或列名,如:
SELECT * FROM blog ORDER BY ?;
你不能让占位符?代替任何其他 SQL 语法,如:
SELECT EXTRACT( ? FROM datetime_column) AS variable_datetime_element FROM blog;
模拟请求
$postData=file_get_contents('C:\Users\ASUS\Pictures\Saved Pictures\2.jpg');
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://www.think.hk/upload1');
curl_setopt($curl, CURLOPT_USERAGENT,'Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // stop verifying certificate
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));//注意加这行代码
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$r = curl_exec($curl);
curl_close($curl);
print_r($r);
接收二进制
$xmlstr = file_get_contents('php://input') ? file_get_contents('php://input') : gzuncompress($GLOBALS['HTTP_RAW_POST_DATA']);//得到post过来的二进制原始数据
//echo $xmlstr;die;
$filename=time().'.png';
$info=file_put_contents($filename,$xmlstr);
if ($info) {
$result = [
'error' => '成功',
'url' => $filename
];
} else {
$result = ['error' => 1,
'message' => 404
];
}
return json_encode($result);
https://learnku.com/articles/27000