Class test{
Curl Upload
function Testpost () {
$url = "Http://liling.gov.gsp365.cn/Api/Upload/postFile";
$file = ' D:\phpStudy\WWW\testcurl\json.zip ';
$data = Array (
' Username ' = ' test ',
' pwd ' = ' 123456 ',
' UploadFile ' = new Curlfile ($file)
);
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, curlopt_followlocation, 1);
curl_setopt ($ch, Curlopt_post, 1);
curl_setopt ($ch, Curlopt_postfields, $data);
$response = curl_exec ($ch);
Curl_close ($ch);
Print_r ($response);
}
Compress files
function Addfiletozip ($path, $zip) {
$handler =opendir ($path); Opens the current folder specified by $path.
while (($filename =readdir ($handler))!==false) {
if ($filename! = "." && $filename! = "...") {//Folder file name is '. ' and '. ', do not operate on them
if (Is_dir ($path. " /". $filename)) {//If an object read is a folder, the recursive
$this->addfiletozip ($path. " /". $filename, $zip);
}else{//Adding files to a Zip object
if ($filename = = ' Json.json ' | | $filename = = ' Json2.json ' | | $filename = = ' Json3.json ')
$zip->addfile ($filename);
}
}
}
@closedir ($path);
$this->testpost ();
}
Calling the Addfiletozip method
function Addzip () {
$zip =new ziparchive ();
if ($zip->open (' Json.zip ', ziparchive::overwrite) = = = TRUE) {
$this->addfiletozip ('./', $zip); Invokes the method, operates on the root directory to be packaged, and passes the Ziparchive object to the method
$zip->close (); Close the processed zip file
}
}
Querying data from a database and storing it as a JSON file
function Jsondata () {
$pdo = new PDO ("Mysql:host=localhost;dbname=json", "root", "root");
$rs = Query ("SELECT * from Drugin"), $pdo
while ($row = Fetch (PDO::FETCH_ASSOC) $rs) {
$res [] = $row;
}
$data = eval (' Return '. Iconv (' GBK ', ' Utf-8 ', Var_export ($res, True)).
$phpjson = Json_encode ($data, Json_unescaped_unicode);
$files = "Json.json";
File_put_contents ($files, $phpjson);
$this->addzip ();
}
}
$mytest =new test ();
$mytest->jsondata ();
?>
Compress into a zip file and upload via Curl