bool curl_setopt (int ch, string option, mixed value)
curl_setopt()函數將為一個curl會話設置選項,option參數是你想要的設置.
value是這個選項給定的值,該例子為模擬登錄,代碼如下:
- $cookie_file = tempnam('./temp','cookie');//保存臨時cookie
- $login_url = 'http://xxx/login.php';//登錄頁提交地址
- $post_fields = 'username=&password=';//傳值參數
- $ch = curl_init($login_url);
- curl_setopt($ch, curlopt_header, 0);
- curl_setopt($ch, curlopt_returntransfer, 1);
- curl_setopt($ch, curlopt_post, 1);
- curl_setopt($ch, curlopt_postfields, $post_fields);
- curl_setopt($ch, curlopt_cookiejar, $cookie_file);
- curl_exec($ch);
- curl_close($ch);
下列選項的值將被作為長整形使用(在option參數中指定):curlopt_infilesize: 當你上傳一個文件到遠程站點,這個選項告訴php你上傳文件的大小。
curlopt_verbose:如果你想curl報告每一件意外的事情,設置這個選項為一個非零值.
curlopt_header:如果你想把一個頭包含在輸出中,設置這個選項為一個非零值.
curlopt_noprogress:如果你不會php為curl傳輸顯示一個進程條,設置這個選項為一個非零值.
注意:php自動設置這個選項為非零值,你應該僅僅為了調試的目的來改變這個選項.
curlopt_nobody:如果你不想在輸出中包含body部分,設置這個選項為一個非零值.
curlopt_failonerror:如果你想讓php在發生錯誤(http代碼返回大于等于300)時,不顯示,設置這個選項為一人非零值,默認行為是返回一個正常頁,忽略代碼.
curlopt_upload:如果你想讓php為上傳做準備,設置這個選項為一個非零值.
curlopt_post:如果你想php去做一個正規的http post,設置這個選項為一個非零值,這個post是普通的 application/x-www-from-urlencoded 類型,多數被html表單使用.
curlopt_ftplistonly:設置這個選項為非零值,php將列出ftp的目錄名列表.
curlopt_ftpappend:設置這個選項為一個非零值,php將應用遠程文件代替覆蓋它.
curlopt_netrc:設置這個選項為一個非零值,php將在你的 ~./netrc 文件中查找你要建立連接的遠程站點的用戶名及密碼.
curlopt_followlocation:設置這個選項為一個非零值(象 'location: ')的頭,服務器會把它當做http頭的一部分發送(注意這是遞歸的,php將發送形如'location: '的頭).
curlopt_put:設置這個選項為一個非零值去用http上傳一個文件,要上傳這個文件必須設置curlopt_infile和curlopt_infilesize選項.
curlopt_mute:設置這個選項為一個非零值,php對于curl函數將完全沉默.
curlopt_timeout:設置一個長整形數,作為最大延續多少秒.
curlopt_low_speed_limit:設置一個長整形數,控制傳送多少字節.
curlopt_low_speed_time:設置一個長整形數,控制多少秒傳送.
curlopt_low_speed_limit規定的字節數.
curlopt_resume_from: 傳遞一個包含字節偏移地址的長整形參數(你想轉移到的開始表單).
curlopt_sslversion:傳遞一個包含ssl版本的長參數,默認php將被它自己努力的確定,在更多的安全中你必須手工設置.
curlopt_timecondition:傳遞一個長參數,指定怎么處理curlopt_timevalue參數,你可以設置這個參數為timecond_ifmodsince 或 timecond_isunmodsince,這僅用于http.
curlopt_timevalue:傳遞一個從1970-1-1開始到現在的秒數,這個時間將被curlopt_timevalue選項作為指定值使用,或被默認timecond_ifmodsince使用.
下列選項的值將被作為字符串:
curlopt_url:這是你想用php取回的url地址,你也可以在用curl_init()函數初始化時設置這個選項.
curlopt_userpwd:傳遞一個形如[username]:[password]風格的字符串,作用php去連接.
curlopt_proxyuserpwd:傳遞一個形如[username]:[password] 格式的字符串去連接http代理.
curlopt_range:傳遞一個你想指定的范圍,它應該是'x-y'格式,x或y是被除外的,http傳送同樣支持幾個間隔,用逗句來分隔(x-y,n-m).
curlopt_postfields:傳遞一個作為http “post”操作的所有數據的字符串.
curlopt_referer:在http請求中包含一個'referer'頭的字符串.
curlopt_useragent:在http請求中包含一個'user-agent'頭的字符串.
curlopt_ftpport:傳遞一個包含被ftp 'post'指令使用的ip地址,這個post指令.
告訴遠程服務器去連接我們指定的ip地址,這個字符串可以是一個ip地址,一個主機名,一個網絡界面名(在unix下),或是‘-’(使用系統默認ip地址).
curlopt_cookie: 傳遞一個包含http cookie的頭連接。
curlopt_sslcert: 傳遞一個包含pem格式證書的字符串。
curlopt_sslcertpasswd: 傳遞一個包含使用curlopt_sslcert證書必需的密碼。
curlopt_cookiefile: 傳遞一個包含cookie數據的文件的名字的字符串。這個
cookie文件可以是netscape格式,或是堆存在文件中的http風格的頭。
curlopt_customrequest: 當進行http請求時,傳遞一個字符被get或head使用,為進行delete或其它操作是有益的.
注意:在確認你的服務器支持命令先不要去這樣做,下列的選項要求一個文件描述(通過使用fopen()函數獲得):
curlopt_file: 這個文件將是你放置傳送的輸出文件,默認是stdout.
curlopt_infile: 這個文件是你傳送過來的輸入文件。
curlopt_writeheader: 這個文件寫有你輸出的頭部分。
curlopt_stderr: 這個文件寫有錯誤而不是stderr。
- <?php
- function curl_redir_exec($ch,$debug="")
- {
- static $curl_loops = 0;
- static $curl_max_loops = 20;
- if ($curl_loops++ >= $curl_max_loops)
- {
- $curl_loops = 0;
- return false;
- }
- curl_setopt($ch, curlopt_header, true);
- curl_setopt($ch, curlopt_returntransfer, true);
- $data = curl_exec($ch);
- $debbbb = $data;
- list($header, $data) = explode("nn", $data, 2);
- $http_code = curl_getinfo($ch, curlinfo_http_code);
- if ($http_code == 301 || $http_code == 302) {
- $matches = array();
- preg_match('/location:(.*?)n/', $header, $matches);
- $url = @parse_url(trim(array_pop($matches)));
- //print_r($url);
- if (!$url)
- {
- //couldn't process the url to redirect to
- $curl_loops = 0;
- return $data;
- }
- $last_url = parse_url(curl_getinfo($ch,
- curlinfo_effective_url));
- /* if (!$url['scheme'])
- $url['scheme'] = $last_url['scheme'];
- if (!$url['host'])
- $url['host'] = $last_url['host'];
- if (!$url['path'])
- $url['path'] = $last_url['path'];*/
- $new_url = $url['scheme'] . '://' . $url['host'] . $url['path']
- . ($url['query']?'?'.$url['query']:'');
- curl_setopt($ch, curlopt_url, $new_url);
- // debug('redirecting to', $new_url);
- return curl_redir_exec($ch);
- } else {
- $curl_loops=0;
- return $debbbb;
- }
- }
- ?>
新聞熱點
疑難解答