青点数据

PHP DEMO代码集成示例

更新时间:2021-06-10 16:28:43

1、任务创建与文件上传代码

$url = "http://api.qd153.com/filter/upload";
$post_data = array(
    "appkey" => "hjsahjdh", //替换您自己的appkey
    "appsecret" => "hjhjhjs", //替换您自己的appsecret
    "task_name" => "test",  //任务名字
    "task_type" => 1,    //任务类型,具体含义可参考文档
    "number_type" => 86   //号码类型/国家区号,具体可参考文档
);
if (version_compare(PHP_VERSION, '5.5.6', '<')) {
    $post_data["file"] = '@' . realpath($filePath);//PHP5.5.6以下版本的上传文件方法,根据自己服务器版本选择
} else {
    $post_data["file"] = new \CURLFile(realpath($filePath));//PHP5.5.6以上版本的上传文件方法,根据自己服务器版本选择
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$output = curl_exec($ch);
curl_close($ch);
$json = json_decode($output, true);
if (empty($json) || $json['code'] != 0) {
    //上传成功的处理逻辑
}else{  //上传失败的处理逻辑}


2、任务进度查询(建议每次查询时间间隔1分钟或以上,防止频繁请求屏蔽IP)

$url = "http://api.qd153.com/filter/findtask";
$post_data = array(
    "appkey" => "hjsahjdh", //替换您自己的appkey
    "appsecret" => "hjhjhjs", //替换您自己的appsecret
    "id" => "test",  //任务ID,由上传任务接口获得
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$output = curl_exec($ch);
curl_close($ch);
$json = json_decode($output, true);
if (empty($json) || $json['code'] != 0) {
    //查询成功的处理逻辑
}


3、任务检测完成后,文件下载

// 方式一:以下代码是PHP下载文件保存到自己服务器
$url= "http://api.qd153.com/filter/dowload?appkey=hjsahjdh&appsecret=hjhjhjs&id=5";//需要替换成您自己的appkey和任务ID<br/>
$filePath = "./test.txt";
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$file_content = curl_exec($ch);
curl_close($ch);
$downloaded_file = fopen($filePath, 'w');
fwrite($downloaded_file, $file_content);
fclose($downloaded_file);
 
// 方式二:以下代码是浏览器远程下载文件
$dowloadFileName = "testfilename.txt";
$dow_url = "http://api.qd153.com/filter/dowload?appkey=hjsahjdh&appsecret=hjhjhjs&id=5";//需要替换成您自己的appkey和任务ID
ob_start();
$ua = $_SERVER["HTTP_USER_AGENT"];
header('Content-Description: File Transfer');
header('Content-Type: application/zip');
header("Accept-Ranges:  bytes ");
header('Content-Disposition: attachment; filename="' . $dowloadFileName . '"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
ob_clean();
flush();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $dow_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $buffer) {
    echo $buffer;
    return strlen($buffer);
});
curl_exec($ch);
curl_close($ch);


客服QQ:1101613211

 

深圳市青点科技有限公司 版权所有 Copyright@2016-2021 AII Right Reserved

粤ICP备20070264号