Compress into a zip file and upload via Curl

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.