php中類比POST提交資料的方法

來源:互聯網
上載者:User

1.通過curl函數

 代碼如下 複製代碼

$post_data = array();
$post_data['clientname'] = "test08";
$post_data['clientpasswd'] = "test08";
$post_data['submit'] = "submit";
$url='http://xxx.xxx.xxx.xx/xx/xxx/top.php';
$o="";
foreach ($post_data as $k=>$v)
{
    $o.= "$k=".urlencode($v)."&";
}
$post_data=substr($o,0,-1);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL,$url);
//為了支援cookie
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$result = curl_exec($ch);

2.通過fsockopen

 代碼如下 複製代碼
$URL=‘http://xxx.xxx.xxx.xx/xx/xxx/top.php';
$post_data['clientname'] = "test08";
$post_data['clientpasswd'] = "test08";
$post_data['submit'] = "ログイン";
$referrer="";
// parsing the given URL
$URL_Info=parse_url($URL);
// Building referrer
if($referrer=="") // if not given use this script as referrer
$referrer=$_SERVER["SCRIPT_URI"];
 
// making string from $data
foreach($post_data as $key=>$value)
$values[]="$key=".urlencode($value);
 
$data_string=implode("&",$values);
// Find out which port is needed - if not given use standard (=80)
if(!isset($URL_Info["port"]))
$URL_Info["port"]=80;
// building POST-request:
$request.="POST ".$URL_Info["path"]." HTTP/1.1n";
$request.="Host: ".$URL_Info["host"]."n";
$request.="Referer: $referrern";
$request.="Content-type: application/x-www-form-urlencodedn";
$request.="Content-length: ".strlen($data_string)."n";
$request.="Connection: closen";
$request.="n";
$request.=$data_string."n";
$fp = fsockopen($URL_Info["host"],$URL_Info["port"]);
fputs($fp, $request);
while(!feof($fp)) {
    $result .= fgets($fp, 128);
}
fclose($fp);

其它的第三方外掛程式

Snoopy 類(2)
sourceforge.net/projects/snoopy/

http://www.redalt.com/xref/trunk/nav.htm?wp-includes/class-snoopy.php.htm

HTTP類(1,2)

http://www.phpclasses.org/browse/download/1/file/5/name/http.php

PEAR HTTP_Request

http://pear.php.net/package/HTTP_Request

Popularity: 74%

curl參考

PHP中的CURL函數庫(Client URL Library Function)

curl_close — 關閉一個curl會話
curl_copy_handle — 拷貝一個curl串連資源的所有內容和參數
curl_errno — 返回一個包含當前會話錯誤資訊的數字編號
curl_error — 返回一個包含當前會話錯誤資訊的字串
curl_exec — 執行一個curl會話
curl_getinfo — 擷取一個curl串連資源控制代碼的資訊
curl_init — 初始化一個curl會話
curl_multi_add_handle — 向curl批處理會話中添加單獨的curl控制代碼資源
curl_multi_close — 關閉一個批處理控制代碼資源
curl_multi_exec — 解析一個curl批處理控制代碼
curl_multi_getcontent — 返回擷取的輸出的文字資料流
curl_multi_info_read — 擷取當前解析的curl的相關傳輸資訊
curl_multi_init — 初始化一個curl批處理控制代碼資源
curl_multi_remove_handle — 移除curl批處理控制代碼資源中的某個控制代碼資源
curl_multi_select — Get all the sockets associated with the cURL extension, which can then be "selected"
curl_setopt_array — 以數組的形式為一個curl設定會話參數
curl_setopt — 為一個curl設定會話參數
curl_version — 擷取curl相關的版本資訊

curl_init()函數的作用初始化一個curl會話,curl_init()函數唯一的一個參數是可選的,表示一個url地址。
curl_exec()函數的作用是執行一個curl會話,唯一的參數是curl_init()函數返回的控制代碼。
curl_close()函數的作用是關閉一個curl會話,唯一的參數是curl_init()函數返回的控制代碼。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.