例子:
<?php
/**
* curl模拟提交json格式的数据
* edit www.
*/
$data = array("name" => "Hagrid", "age" => "36");
$data_string = json_encode($data);
$ch = curl_init('http://api.local/rest/users');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
?>
例子:
/**
* 过滤页面中bom
* edit www.
*/
function checkBOM ($filename) {
$contents = file_get_contents($filename);
$charset[1] = substr($contents, 0, 1);
$charset[2] = substr($contents, 1, 1);
$charset[3] = substr($contents, 2, 1);
if (ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191) {
$rest = substr($contents, 3);
rewrite $rest;
}else{
return false;
}
}
?>
您可能感兴趣的文章:
PHP实例:检测并清除文件开头的BOM信息
Php批量去除bom头信息的实现代码
php去掉bom头的代码分享
检测php文件是否有bom头的代码
批量清除php文件中bom的方法
检查并清除php文件中bom的函数
有关 UTF-8 BOM 导致样式错乱的解决方法
BOM与DOM的区别分析
有关UTF-8 编码中BOM的检测与删除
例子:
<?php
/**
* curl提交GET,POST,Cookie
* edit www.
*/
$get_data = array (
"get1"=> "get1",
"get2" => "get2",
"get3" => "get3"
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://test.test.com/test.php?'.http_build_query($get_data));
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11');
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$post_data = array (
"p1" => "test1",
"p2" => "test2",
"p3" => "test3"
);
curl_setopt($curl, CURLOPT_POST, true);
//["CONTENT_TYPE"]=> string(70) "multipart/form-data; boundary=------077a996f5afe"
//要发送文件,在文件名前面加上@前缀并使用完整路径。
//使用数组提供post数据时,CURL组件大概是为了兼容@filename这种上传文件的写法,默认把content_type设为了multipart/form-data。
//对部分服务器不兼容。
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
//["CONTENT_TYPE"]=> string(33) "application/x-www-form-urlencoded"
//curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post_data));
//在没有需要上传文件的情况下,尽量对post提交的数据进行http_build_query,然后发送出去,兼容性好,数据包发送少。
$cookies = array(
'c1'=>'v1',
'c2'=>'v2',
'c3'=>'v3',
);
$cookies_string = '';
foreach($cookies as $name=>$value){
$cookies_string .= $name.'='.$value.';';
}
curl_setopt($curl, CURLOPT_COOKIE, $cookies_string);
$result = curl_exec($curl);
curl_close($curl);
var_dump($result);
exit;
?>