知名建案指名合作,專業外牆清洗
讓大樓亮晶晶,清潔服務隨傳隨到
拆除工程專業壓克力、金屬字、帆布、水晶字、不鏽鋼廣告招牌設計
材質選擇多,歡迎來電洽詢

首頁  •  j2h 論壇 • 程式設計討論     • 

[php] 模擬表格資料 post 方法送出

房東:小強
發表時間:2012-06-24


如何用Zend函數模擬出表格資料送出到下一頁呢?


以下是可實作的程式:


 









 


require_once 'Zend/Http/Client.php';


 


//Zend_Http_Client 宣告


$client = new Zend_Http_Client();


 


//設定要送出資料到哪個網址


$client->setUri('http://localhost/test/zend_post/test1.php');


 


//設定http headers參數


$client->setHeaders(array(


    'Host' => 'localhost',


    'Accept-encoding' => 'gzip,deflate',


    'X-Powered-By' => 'Zend'));


 


//設定http連線的參數,此部分可省略


$client->setConfig(array(


    'maxredirects' => 0,


    'timeout'      => 30));


 


//這是指定資料採用 post的方式作參數傳輸的編排,http協定中GET和POST參數傳遞實質上是不同的


$client->setParameterPost(array(


    'name'  => 'miggo',


    'nickname'   => 'miggo',


    'selection' => array(1, 32, 44)


));


 


//指定用post方法將資料送出


$client->setMethod(Zend_Http_Client::POST);


 


//送出http要求


$response = $client->request();


 


//可觀看送出結果到test1.php的回應


print_r($response);


?>



 


當然要撰寫一支 test1.php 頁面去接收post後的資料囉,這裡就不多說了!



http://miggo.pixnet.net/blog/post/28067420



  • 贊助網站       

    廣利不動產-板橋在地生根最實在--新板特區指名度最高、值得您信賴的好房仲
    完整房訊,房屋、店面熱門精選物件,廣利不動產 優質仲介,房屋租賃、買賣資訊透明,交易真安心!
    廣利不動產-新板特區指名度最高、值得您信賴的好房仲
    您的托付,廣利用心為您服務

  • 1 樓住戶:jack
    發表時間:2012-06-24

    GET行為比較簡單,POST比較複雜一些 这里提供两种方法供选择:第一:手写代碼。這裡提供兩種方法供選擇:第一:手寫代碼。 第二:利用HttpClient php类库第一种方法:第二:利用HttpClient php類庫第一種方法:
    复制代碼代碼如下: 複製代碼代碼如下:
    <? <? PHP PHP
    $flag = 0; $flag = 0;
    //要post的数据 //要post的數據
    $argv = array( $argv = array(
    'var1'=>'abc', 'var1'=>'abc',
    'var2'=>'你好吗'); 'var2'=>'你好嗎');
    //构造要post的字符串 //構造要post的字符串
    foreach ($argv as $key=>$value) { foreach ($argv as $key=>$value) {
    if ($flag!=0) { if ($flag!=0) {
    $params .= "&"; $params .= "&";
    $flag = 1; $flag = 1;
    } }
    $params.= $key."="; $params.= urlencode($value); $params.= $key."="; $params.= urlencode($value);
    $flag = 1; $flag = 1;
    } }
    $length = strlen($params); $length = strlen($params);
    //創建socket连接 //創建socket連接
    $fp = fsockopen("127.0.0.1",80,$errno,$errstr,10) or exit($errstr."--->".$errno); $fp = fsockopen("127.0.0.1",80,$errno,$errstr,10) or exit($errstr."--->".$errno);
    //构造post请求的头 //構造post請求的頭
    $header = "POST /mobile/try.php HTTP/1.1"; $header = "POST /mobile/try.php HTTP/1.1";
    $header .= "Host:127.0.0.1"; $header .= "Host:127.0.0.1";
    $header .= "Referer:/mobile/sendpost.php"; $header .= "Referer:/mobile/sendpost.php";
    $header .= "Content-Type: application/x-www-form-urlencoded"; $header .= "Content-Type: application/x-www-form-urlencoded";
    $header .= "Content-Length: ".$length.""; $header .= "Content-Length: ".$length."";
    $header .= "Connection: Close"; $header .= "Connection: Close";
    //添加post的字符串 //添加post的字符串
    $header .= $params.""; $header .= $params."";
    //发送post的数据 //發送post的數據
    fputs($fp,$header); fputs($fp,$header);
    $inheader = 1; $inheader = 1;
    while (!feof($fp)) { while (!feof($fp)) {
    $line = fgets($fp,1024); //去除请求包的头只显示页面的返回数据 $line = fgets($fp,1024); //去除請求包的頭只顯示頁面的返回數據
    if ($inheader && ($line == "n" || $line == "")) { if ($inheader && ($line == "n" || $line == "")) {
    $inheader = 0; $inheader = 0;
    } }
    if ($inheader == 0) { if ($inheader == 0) {
    echo $line; echo $line;
    } }
    } }
    fclose($fp); fclose($fp);
    ?> ?>

    第二种方法是:使用httpclient类第二種方法是:使用httpclient類
    复制代碼代碼如下: 複製代碼代碼如下:
    $pageContents = HttpClient::quickPost('http://example.com/someForm', array( $pageContents = HttpClient::quickPost('http://example.com/someForm', array(
    'name' => 'Some Name', 'name' => 'Some Name',
    'email' => '[email protected]' 'email' => '[email protected]'
    )); ));

    使用httpclient类库,可以去官方下载最新的类库,官方地址为:http://scripts.incutio.com/httpclient/index.php使用httpclient類庫,可以去官方下載最新的類庫,官方地址為:http://scripts.incutio.com/httpclient/index.php
    附加一些点php httpclient的其他几个用法静态方法获取网页:附加一些點php httpclient的其他幾個用法靜態方法獲取網頁:
    复制代碼代碼如下: 複製代碼代碼如下:
    $pageContents = HttpClient::quickGet('http://bankcha.com') $pageContents = HttpClient::quickGet('http://bankcha.com')

    Get方法获取 Get方法獲取
    复制代碼代碼如下: 複製代碼代碼如下:
    $client = new HttpClient('bankcha.com'); $client = new HttpClient('bankcha.com');
    if (!$client->get('/')) { if (!$client->get('/')) {
    die('An error occurred: '.$client->getError()); die('An error occurred: '.$client->getError());
    } }
    $pageContents = $client->getContent(); $pageContents = $client->getContent();
    带调试的Get方法获取PHP代碼帶調試的Get方法獲取PHP代碼
    $client = new HttpClient('bankcha.com'); $client = new HttpClient('bankcha.com');
    $client->setDebug(true); $client->setDebug(true);
    if (!$client->get('/')) { if (!$client->get('/')) {
    die('An error occurred: '.$client->getError()); die('An error occurred: '.$client->getError());
    } }
    $pageContents = $client->getContent(); $pageContents = $client->getContent();
    带自动转向的Get方法PHP代碼$client = new HttpClient('www.bankcha.com');帶自動轉向的Get方法PHP代碼$client = new HttpClient('www.bankcha.com');
    $client->setDebug(true); $client->setDebug(true);
    if (!$client->get('/')) { if (!$client->get('/')) {
    die('An error occurred: '.$client->getError()); die('An error occurred: '.$client->getError());
    } }
    $pageContents = $client->getContent(); $pageContents = $client->getContent();
    检查页面是否存在PHP代碼檢查頁面是否存在PHP代碼
    $client = new HttpClient('bankcha.com'); $client = new HttpClient('bankcha.com');
    $client->setDebug(true); $client->setDebug(true);
    if (!$client->get('/thispagedoesnotexist')) { if (!$client->get('/thispagedoesnotexist')) {
    die('An error occurred: '.$client->getError()); die('An error occurred: '.$client->getError());
    } }
    if ($client->getStatus() == '404') { if ($client->getStatus() == '404') {
    echo 'Page does not exist!'; echo 'Page does not exist!';
    } }
    $pageContents = $client->getContent(); $pageContents = $client->getContent();
    伪造客户端PHP代碼偽造客戶端PHP代碼
    $client = new HttpClient('bankcha.com'); $client = new HttpClient('bankcha.com');
    $client->setDebug(true); $client->setDebug(true);
    $client->setUserAgent('Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.3a) Gecko/20021207'); $client->setUserAgent('Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.3a) Gecko/20021207');
    if (!$client->get('/')) { if (!$client->get('/')) {
    die('An error occurred: '.$client->getError()); die('An error occurred: '.$client->getError());
    } }
    $pageContents = $client->getContent(); $pageContents = $client->getContent();
    登录验证并请求一个网页PHP代碼登錄驗證並請求一個網頁PHP代碼
    $client = new HttpClient('bankcha.com'); $client = new HttpClient('bankcha.com');
    $client->post('/login.php', array( $client->post('/login.php', array(
    'username' => 'Simon', 'username' => 'Simon',
    'password' => 'ducks' 'password' => 'ducks'
    )); ));
    if (!$client->get('/private.php')) { if (!$client->get('/private.php')) {
    die('An error occurred: '.$client->getError()); die('An error occurred: '.$client->getError());
    } }
    $pageContents = $client->getContent(); $pageContents = $client->getContent();
    HTTP授权PHP代碼 HTTP授權PHP代碼
    $client = new HttpClient('bankcha.com'); $client = new HttpClient('bankcha.com');
    $client->setAuthorization('Username', 'Password'); $client->setAuthorization('Username', 'Password');
    if (!$client->get('/')) { if (!$client->get('/')) {
    die('An error occurred: '.$client->getError()); die('An error occurred: '.$client->getError());
    } }
    $pageContents = $client->getContent(); $pageContents = $client->getContent();
    输出头信息PHP代碼輸出頭信息PHP代碼
    $client = new HttpClient('bankcha.com'); $client = new HttpClient('bankcha.com');
    if (!$client->get('/')) { if (!$client->get('/')) {
    die('An error occurred: '.$client->getError()); die('An error occurred: '.$client->getError());
    } }
    print_r($client->getHeaders()); print_r($client->getHeaders());
    设置一个域内重定向最多次数PHP代碼設置一個域內重定向最多次數PHP代碼
    $client = new HttpClient('www.bankcha.com'); $client = new HttpClient('www.bankcha.com');
    $client->setDebug(true); $client->setDebug(true);
    $client->setMaxRedirects(3); $client->setMaxRedirects(3);
    $client->get('/'); $client->get('/');

    php fsockopen 伪造post和get方法 php fsockopen 偽造post和get方法
    fsockopen 伪造post和get方法哦,如果你正在找伪造post和get方法的php处理代碼这款不错哦。 fsockopen 偽造post和get方法哦,如果你正在找偽造post和get方法的php處理代碼這款不錯哦。
    复制代碼代碼如下: 複製代碼代碼如下:
    //fsocket模拟post提交 //fsocket模擬post提交
    $purl = "http://localhost/netphp/test2.php?uu=rrrrrrrrrrrr"; $purl = "http://localhost/netphp/test2.php?uu=rrrrrrrrrrrr";
    print_r(parse_url($url)); print_r(parse_url($url));
    sock_post($purl,"uu=55555555555555555"); sock_post($purl,"uu=55555555555555555");
    //fsocket模拟get提交 //fsocket模擬get提交
    function sock_get($url, $query) function sock_get($url, $query)
    { {
    $info = parse_url($url); $info = parse_url($url);
    $fp = fsockopen($info["host"], 80, $errno, $errstr, 3); $fp = fsockopen($info["host"], 80, $errno, $errstr, 3);
    $head = "GET ".$info['path']."?".$info["query"]." HTTP/1.0rn"; $head = "GET ".$info['path']."?".$info["query"]." HTTP/1.0rn";
    $head .= "Host: ".$info['host']."rn"; $head .= "Host: ".$info['host']."rn";
    $head .= "rn"; $head .= "rn";
    $write = fputs($fp, $head); $write = fputs($fp, $head);
    while (!feof($fp)) while (!feof($fp))
    { {
    $line = fread($fp,4096); $line = fread($fp,4096);
    echo $line; echo $line;
    } }
    } }
    sock_post($purl,"uu=rrrrrrrrrrrrrrrr"); sock_post($purl,"uu=rrrrrrrrrrrrrrrr");
    function sock_post($url, $query) function sock_post($url, $query)
    { {
    $info = parse_url($url); $info = parse_url($url);
    $fp = fsockopen($info["host"], 80, $errno, $errstr, 3); $fp = fsockopen($info["host"], 80, $errno, $errstr, 3);
    $head = "POST ".$info['path']."?".$info["query"]." HTTP/1.0rn"; $head = "POST ".$info['path']."?".$info["query"]." HTTP/1.0rn";
    $head .= "Host: ".$info['host']."rn"; $head .= "Host: ".$info['host']."rn";
    $head .= "Referer: http://".$info['host'].$info['path']."rn"; $head .= "Referer: http://".$info['host'].$info['path']."rn";
    $head .= "Content-type: application/x-www-form-urlencodedrn"; $head .= "Content-type: application/x-www-form-urlencodedrn";
    $head .= "Content-Length: ".strlen(trim($query))."rn"; $head .= "Content-Length: ".strlen(trim($query))."rn";
    $head .= "rn"; $head .= "rn";
    $head .= trim($query); $head .= trim($query);
    $write = fputs($fp, $head); $write = fputs($fp, $head);
    while (!feof($fp)) while (!feof($fp))
    { {
    $line = fread($fp,4096); $line = fread($fp,4096);
    echo $line; echo $line;
    } }
    } }
    ?> ?>


    2 樓住戶:阿凱
    發表時間:2012-07-07

    //1.通過curl函數//

    /* * test.php
    * 這個文件把參數模擬提交到test2.php */

    $post_data = array(
    'before=你好',
    'wb_lp=CHJA', $post_data = implode('&',$post_data);
    $url='http://localhost/test2.php';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_URL,$url);
    //傳遞一個作爲HTTP “POST”操作的所有數據的字符串。
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    ob_start();
    curl_exec($ch);
    $result = ob_get_contents() ;
    ob_end_clean();

    echo $result;

    /**
    * test2.php
    * 接受從test.php 模擬提交過來的參數 並輸出
    */

    echo '
    ';
    print_r($_GET);

    ECHO '
    ----------
    ';
    print_r($_POST); ?>

    //2.通過fsockopen

    $URL='http://localhost/test2.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.1\n";
    $request.="Host: ".$URL_Info["host"]."\n";
    $request.="Referer: $referrer\n";
    $request.="Content-type: application/x-www-form-urlencoded\n";
    $request.="Content-length: ".strlen($data_string)."\n";
    $request.="Connection: close\n";
    $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);

    echo $result;
    ?>



    3 樓住戶:小寶
    發表時間:2012-07-07

    以下是PHP使用curl函數模擬POST登陸並獲取後台頁面內容的實例代碼:

    $cookie_jar =tempnam("","cookie"); $cookie_jar =tempnam("","cookie");

    //存储cookie的文件,第一个参数可以设置为c:\windows\temp、c:\windows\system32之类文件夹 //存儲cookie的文件,第一個參數可以設置為c:\windows\temp、c:\windows\system32之類文件夾

    $request = 'user=abcd9.com&pw=abcd9.com'; $request = 'user=abcd9.com&pw=abcd9.com';

    //提交的参数,如帐号、密码等,在实际应用时填写可参考抓包数据中的参数及对应值 //提交的參數,如帳號、密碼等,在實際應用時填寫可參考抓包數據中的參數及對應值

    $ch = curl_init(); $ch = curl_init();

    curl_setopt($ch,CURLOPT_URL,'http://www.abcd9.com/admin/index.php?action=login'); curl_setopt($ch,CURLOPT_URL,'http://www.abcd9.com/admin/index.php?action=login');

    //提交的目标地址 //提交的目標地址

    curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POST, 1);

    //设置提交方式为POST //設置提交方式為POST

    curl_setopt($ch, CURLOPT_POSTFIELDS, $request); curl_setopt($ch, CURLOPT_POSTFIELDS, $request);

    //设置POST提交内容 //設置POST提交內容

    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);

    //把返回来的cookie信息保存在$cookie_jar文件中 //把返回來的cookie信息保存在$cookie_jar文件中

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    //设定返回的数据是否自动显示 //設定返回的數據是否自動顯示

    curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_HEADER, false);

    //设定是否显示头信息 //設定是否顯示頭信息

    curl_setopt($ch, CURLOPT_NOBODY, false); curl_setopt($ch, CURLOPT_NOBODY, false);

    //设定是否输出页面内容 //設定是否輸出頁面內容

    curl_exec($ch); curl_exec($ch);

    curl_close($ch); curl_close($ch);

    /*以上是登录代码*/ /*以上是登錄代碼*/

    /*以下是抓取代码*/ /*以下是抓取代碼*/

    $ch2 = curl_init(); $ch2 = curl_init();

    curl_setopt($ch2, CURLOPT_URL, 'http://www.abcd9.com/admin/'); curl_setopt($ch2, CURLOPT_URL, 'http://www.abcd9.com/admin/');

    //抓取的后台页面地址 //抓取的後台頁面地址

    curl_setopt($ch2, CURLOPT_HEADER, false); curl_setopt($ch2, CURLOPT_HEADER, false);

    curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch2, CURLOPT_COOKIEFILE, $cookie_jar); curl_setopt($ch2, CURLOPT_COOKIEFILE, $cookie_jar);

    $content = curl_exec($ch2); $content = curl_exec($ch2);

    curl_close($ch2); curl_close($ch2);

    unlink($cookie_jar); unlink($cookie_jar);

    //释放cookie临时文件 //釋放cookie臨時文件

    echo $content; echo $content;

    //显示抓取到的内容 //顯示抓取到的內容



    http://www.abcd9.com/?post=131

    4 樓住戶:jack
    發表時間:2012-07-14

    其實不用jQuery,你只要先用PHP模擬POST做登入,再用PHP模擬POST送出文章即可,你有幾個,就要用這弄幾次(建議用While+Switch+CURL)
    http://blog.istory.tw/2009/05/php-curl-get-post.html

    以下是範例,至於要做啥變化,自己弄:
    /*SET*/
    $blog=4;//blog total num
    $blog[1]=Array('name'='pchome','id'='xxxx','pw'='xxxx');//name = BLOG NAME
    $blog[2]=Array('name'='yam','id'='xxxx','pw'='xxxx');//id = Your BLOG ID
    $blog[3]=Array('name'='yahoo','id'='xxxx','pw'='xxxx');//pw = Your BLOG PW
    $blog[4]=Array('name'='wretch','id'='xxxx','pw'='xxxx');
    /*PROGRAM*/
    $x=$_POST;
    While($x||$n<$blog){
    $n++;
    switch($blog[$n]['name']){
    case 'pchome':
    /*LOGIN*/
    /*POST*/
    break;
    case 'yahoo':
    /*LOGIN*/
    /*POST*/
    break;
    case 'wretch':
    /*LOGIN*/
    /*POST*/
    break;
    case 'yam':
    /*LOGIN*/
    /*POST*/
    break;
    }
    }
    ?>



     共 4 人回應  選擇頁數 【第1 頁】 

    姓名:
    佈告內容:
    其他選項: