但是我這幾天碰到一個神奇的網站,資料是塞在request payload裡面的,爬了好多文才知道該怎麼正確的丟...
$post_data = 'path=down重點如下
month=2018/05/31
cnt=1
itemno=22';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_TIMEOUT, 1); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); // basic auth 認證}
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$result=curl_exec ($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code
curl_close ($ch);
- Content-Type 要設為 application/x-www-form-urlencoded 不要被browser的 text/plain 騙了
- 超奇琶的變數設定方法,請看上方的$post_data,直接用 path=down這樣設定變數內容,然後如果有多個變數,直接用換行切換,直接用換行切換,直接用換行切換,很重要所以要說三次...