基本使用
使用 CURLOPT_URL 設定連結網址,然後執行即可。順利的話就可以取得回傳網頁原始碼
1 2 3 4 |
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://www.ted.com/'); curl_exec($ch); curl_close($ch); |
--
不直接顯示取得內容
將 CURLOPT_RETURNTRANSFER 設定為 true 即可
1 2 3 4 5 6 |
<?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://www.ted.com'); curl_setopt($ch, CURLOPT_RETURNTRANSFER , 1); // 不直接出現回傳值 curl_exec($ch); curl_close($ch); |
--
加上 POST 資料
增加 CURLOPT_POSTFIELDS 參數,傳遞 array() 陣列
Google+ 在 OAuth 內的資訊必須使用 POST 傳遞,使用方法大致如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
$send_url = $this->accounts_oauth2_token; $token_post = array( "code" => $code, "client_id" => $this->client_id, "client_secret" => $this->client_secret, "redirect_uri" => $this->redirect_uri, "grant_type" => "authorization_code" ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $send_url); curl_setopt($ch, CURLOPT_POSTFIELDS, $token_post); curl_setopt($ch, CURLOPT_RETURNTRANSFER , 1); // 不直接出現回傳值 $response = curl_exec($ch); // 回傳 access_token curl_close($ch); |
--
取得處理狀態
curl_getinfo() 可以得知執行後結果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $m3u8); curl_setopt($ch, CURLOPT_REFERER, 'http://hichannel.hinet.net/radio/index.do' ); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_exec($ch); $info = curl_getinfo($ch); curl_close($ch); // 不可用資源回傳 410 if ( $info['http_code'] == '200' ) { echo $m3u8; break; } else { //echo $m3u8; echo $i++; //sleep(1); } |
--
設定或增加其他功能、參數
1 2 3 4 5 |
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $URL .'/adv.do'); curl_setopt($ch, CURLOPT_REFERER, 'http://hichannel.hinet.net' ); curl_exec($ch); curl_close($ch); |
設定 REFERER 參照來源網址
--
如何得知送出的 Head ?
步驟
- 設定 CURLINFO_HEADER_OUT 為 true
- 使用 curl_getinfo() 取得輸出結果, 送出 Head 也在結果內
1 2 3 4 5 6 7 8 9 10 11 |
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://radio-hichannel.cdn.hinet.net/crossdomain.xml'); curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36'); curl_setopt($ch, CURLOPT_REFERER, 'http://hichannel.hinet.net/radio/index.do' ); curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Requested-With: ShockwaveFlash/21.0.0.216", "Host: radio-hichannel.cdn.hinet.net")); curl_setopt($ch, CURLINFO_HEADER_OUT, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER , 1); $crossdomain = curl_exec($ch); $info = curl_getinfo($ch); curl_close($ch); |
--
增加自定義 Head
1 2 3 4 |
$ch = curl_init(); curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Requested-With: XMLHttpRequest")); $result = curl_exec ($ch); curl_close ($ch); |
--
增加自定義 - 傳遞連線 IP
1 |
curl_setopt($ch, CURLOPT_HTTPHEADER , array("REMOTE_ADDR: $ip")); |
HEAD 名稱前面強制加上 HTTP_ 開頭,因此取得 IP 時必須相互搭配
1 2 3 4 5 6 7 8 9 |
Array ( [REDIRECT_STATUS] => 200 [HTTP_HOST] => soundu2.mj-app.com.tw [HTTP_ACCEPT] => */* [HTTP_REMOTE_ADDR] => 8.8.8.8 [CONTENT_LENGTH] => 177 : : |
--
目的端 PHP 取得 IP 程式方法
首先請先看以下參考連結
這裡討論到一段網路普遍「謠傳」的取得 IP 代碼
1 2 3 4 5 6 7 |
if (!empty($_SERVER['HTTP_CLIENT_IP'])) { $ip = $_SERVER['HTTP_CLIENT_IP']; } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ip = $_SERVER['REMOTE_ADDR']; } |
大部分抄這段程式碼的人都不知道為什麼要先取 HTTP_CLIENT_IP 和 HTTP_X_FORWARDED_FOR 這兩個值,等學會 curl 之後就會知道大大說這是唬爛是正確的。
1 2 3 4 5 6 7 8 9 |
<?php if (!empty($_SERVER['HTTP_CLIENT_IP'])) { $ip = $_SERVER['HTTP_CLIENT_IP']; } else { $ip = $_SERVER['REMOTE_ADDR']; } $ss = print_r($_SERVER, true); file_put_contents('/tmp/curl.log', $ss, FILE_APPEND | LOCK_EX); |
想要知道接收端資訊必須另外儲存,文字檔、資料庫等都可以,為了方便這裡使用文字檔
--
隱藏 _SERVER['REMOTE_ADDR']
1 2 |
//curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); // 尚未知道確切用途 curl_setopt($ch, CURLOPT_PROXY , "186.90.211.39:8080"); |
設定使用 Proxy Server 代理連線,對方取得的 REMOTE_ADDR 就是 Proxy Server 的 IP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
Array ( [UNIQUE_ID] => V0MapsCoAAMAAYZQ9qcAAAAD [HTTP_HOST] => hoyo.idv.tw [HTTP_CONNECTION] => close [HTTP_VIA] => 1.0 tinyproxy (tinyproxy/1.8.2) [HTTP_ACCEPT] => */* [HTTP_CLIENT_IP] => 8.8.8.8 [PATH] => /sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin:/root/bin:/usr/local/texlive/2010/bin/amd64-freebsd [SERVER_SIGNATURE] => [SERVER_SOFTWARE] => Apache/2.2.25 (FreeBSD) PHP/5.5.4 mod_ssl/2.2.25 OpenSSL/0.9.8x DAV/2 [SERVER_NAME] => hoyo.idv.tw [SERVER_ADDR] => 192.168.0.3 [SERVER_PORT] => 80 [REMOTE_ADDR] => 186.90.211.39 [DOCUMENT_ROOT] => /WEBSite/www/WWW [SERVER_ADMIN] => pc@hoyo.idv.tw [SCRIPT_FILENAME] => /WEBSite/www/WWW/curlx.php [REMOTE_PORT] => 43403 [GATEWAY_INTERFACE] => CGI/1.1 [SERVER_PROTOCOL] => HTTP/1.0 [REQUEST_METHOD] => GET [QUERY_STRING] => [REQUEST_URI] => /curlx.php [SCRIPT_NAME] => /curlx.php [PHP_SELF] => /curlx.php [REQUEST_TIME_FLOAT] => 1464015526.863 [REQUEST_TIME] => 1464015526 ) |
--
送出 HTTP Auth
如果有一個網頁使用 HTTP Auth 使用者認證,該如何使用 PHP curl 登錄取得資料?
以下是 zopim 的取得聊天紀錄範例,本來是使用 Linux curl 指令操作的,例如:
1 |
curl https://www.zopim.com/api/v2/chats -v -u username:password |
PHP 可以這樣實現
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$username = ZOPIM_Username; $password = ZOPIM_Password; $URL = 'https://www.zopim.com/api/v2/chats'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$URL); curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_USERPWD, $username.':'.$password ); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); $result = curl_exec ($ch); $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code curl_close ($ch); |
--
Timeout 以及取得 Timeout 錯誤
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php $url = ''; $JsonPost = ''; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POSTFIELDS, $JsonPost); curl_setopt($ch, CURLOPT_RETURNTRANSFER , 1); curl_setopt($ch, CURLOPT_TIMEOUT, 10); $Response = curl_exec($ch); if (curl_errno($ch) == 28){ echo '主機無法連接,請稍後重新測試!'; } else{ echo $Response; } curl_close($ch); |
--
上傳檔案 PHP 5.4 以下
將檔案存放路徑的檔案名稱前加上 @ 即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// curl $url = $DocServer.'/DocSharing/fileUploadForeign'; $post = array( 'siteId' => xuedianyun_siteId, 'timestamp' => $timestamp, 'convertTools' => 'msoffice', 'authId' => $authId, 'createUserName' => 'hoyo', 'createUserID' => 'hoyo', 'upfile' => '@1-2.ppt' ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); curl_setopt($ch, CURLOPT_RETURNTRANSFER , 1); $Response = curl_exec($ch); curl_close($ch); |
--
上傳檔案 PHP 5.5 以上
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php $aPost = array( 'default_file' => 'html_version.html', 'expiration' => (2*31*24*60*60) ) if ((version_compare(PHP_VERSION, '5.5') >= 0)) { $aPost['file'] = new CURLFile($localFile); curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true); } else { $aPost['file'] = "@".$localFile; } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $apiurl); curl_setopt($ch, CURLOPT_TIMEOUT, 120); curl_setopt($ch, CURLOPT_BUFFERSIZE, 128); curl_setopt($ch, CURLOPT_POSTFIELDS, $aPost); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $sResponse = curl_exec ($ch); |
如要沿用 5.4 舊方法則設定 curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); 即可
--
HTTP 417 - Expectation Failed
1 |
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); |
--
25,359 total views, 6 views today