您好,登錄后才能下訂單哦!
實現原理:
通過curl工具模擬登錄,然后調用相關接口發送數據進行各種操作。
需要掌握知識點:
代碼示范
curl的GET操作
private function projectCollections(): array
{
$ts = microtime(true) * 1000;
$api = "http://www.****.com/json/projectCollections?status=1&username={$this->userName}&limit=1&projectID={$this->projectID}&ts=" . $ts; //API地址
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $api);
curl_setopt($curl, CURLOPT_HEADER, 0);//是否顯示頭信息
curl_setopt($curl, CURLOPT_COOKIEJAR, $this->cookie); //設置Cookie信息保存在指定的文件中
curl_setopt($curl, CURLOPT_COOKIEFILE, $this->cookie); //發送cookie信息
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_REFERER, "http://www.***.com/details/v5?id={$this->projectID}&isView=true");
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36");
$json = curl_exec($curl);
curl_close($curl);
\Log::error(var_export($json, true));
return json_decode($json, true);
//{"meta":{"total":"0","start":"1","size":"0"},"data":[]}
}
//收藏
private function addCollection()
{
$api = "http://www.****.com/sjc/api/project/collection/add";
$postData = [
'id' => (string)$this->projectID,
];
$data = json_encode($postData);
$length = strlen($data);
$headers = [
//'Origin:http://www.ilab-x.com',
//'Host:www.ilab-x.com',
"Content-type: application/json",
'Content-Length: ' . $length,
];
\Log::error(var_export($postData, true));
\Log::error(var_export($data, true));
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $api);
curl_setopt($curl, CURLOPT_HEADER, 0);//是否顯示頭信息
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_COOKIEJAR, $this->cookie); //設置Cookie信息保存在指定的文件中
curl_setopt($curl, CURLOPT_COOKIEFILE, $this->cookie); //發送cookie信息
curl_setopt($curl, CURLOPT_POST, 1);//post方式提交
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);//注意,這里提交json格式
curl_setopt($curl, CURLOPT_REFERER, "http://www.****.com/details/v5?id={$this->projectID}&isView=true");
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36");
curl_setopt($curl, CURLOPT_ENCODING, 'deflate');
$json = curl_exec($curl);
curl_close($curl);
\Log::error(var_export($headers, true));
\Log::error(var_export($json, true));
return json_decode($json, true);
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。