2011年10月19日 星期三

php 傳送POST到別的URL並取得回應內容 使用fsockopen

資料來源:http://blog.hsin.tw/2009/php-post-method-fsockopen/


//接收POST參數的URL
//POST參數,在這個陣列裡,索引是name,值是value,沒有限定組數
$postdata = array('post_name'=>'post_value','acc'=>'hsin','nick'=>'joe');
//函式回覆的值就是取得的內容
$result = sendpost($url,$postdata);
function sendpost($url, $data){
//先解析url 取得的資訊可以看看http://www.php.net/parse_url
$url = parse_url($url);
$url_port = $url['port']==''?(($url['scheme']=='https')?443:80):$url['port'];
if(!$url) return "couldn't parse url";
//對要傳送的POST參數作處理
$encoded = "";
while(list($k,$v)=each($data)){
  $encoded .= ($encoded?'&':'');
  $encoded .= rawurlencode($k)."=".rawurlencode($v);
}
//開啟一個socket
$fp = fsockopen($url['host'],$url_port);
if(!$fp) return "Failed to open socket to ".$url['host'];
//header的資訊
fputs($fp,'POST '.$url['path'].($url['query']?'?'.$url['query']:'')." HTTP/1.0rn");
fputs($fp,"Host: ".$url['host']."n");
fputs($fp,"Content-type: application/x-www-form-urlencodedn");
fputs($fp,"Content-length: ".strlen($encoded)."n");
fputs($fp,"Connection: closenn");
fputs($fp,$encoded."n");
//取得回應的內容
$line = fgets($fp,1024);
if(!eregi("^HTTP/1.. 200", $line)) return;
$results = "";
$inheader = 1;
while(!feof($fp)){
  $line = fgets($fp,2048);
  if($inheader&&($line == "n" || $line == "rn")){
    $inheader = 0;
  }elseif(!$inheader){
    $results .= $line;
  }
}
fclose($fp);
return $results;
}


相關參考:
http://wezfurlong.org/blog/2006/nov/http-post-from-php-without-curl/

PHP CURL 發送 GET 和 POST 語法

資料來源:http://fq-story.blogspot.com/2009/05/php-curl-get-post.html

PHP CURL

1、http的get傳送

$ch = curl_init("/") ;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ;
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ;
$output = curl_exec($ch) ;
$fh = fopen("out.html", 'w') ;
fwrite($fh, $output) ;
fclose($fh) ;

2、http的post傳送

EX1

$URL="www.mysite.com/test.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://$URL");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "Data1=blah&Data2=blah");curl_exec ($ch);
curl_close ($ch);


EX2

//extract data from the post
extract($_POST) ;
//set POST variables
$url = '/get-post.php' ;
$fields = array(
'lname'=>urlencode($last_name) ,
'fname'=>urlencode($first_name) ,
'title'=>urlencode($title) ,
'company'=>urlencode($institution) ,
'age'=>urlencode($age) ,
'email'=>urlencode($email) ,
'phone'=>urlencode($phone)
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&' ; }
rtrim($fields_string ,'&') ;
//open connection
$ch = curl_init() ;
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL,$url) ;
curl_setopt($ch, CURLOPT_POST,count($fields)) ;
curl_setopt($ch, CURLOPT_POSTFIELDS,$fields_string) ;
//execute post
$result = curl_exec($ch) ;
//close connection
curl_close($ch) ;

相關參考:
http://wezfurlong.org/blog/2006/nov/http-post-from-php-without-curl/
http://blog.roodo.com/esabear/archives/16358749.html

2011年9月27日 星期二

常用 Regular Expression 範例

資料來源:http://blog.longwin.com.tw/2009/09/paper-regular-expression-regex-2009/

常用的Regex幾乎都有,超讚的範例網站
http://www.virtuosimedia.com/dev/php/37-tested-php-perl-and-javascript-regular-expressions

2011年9月22日 星期四

Server Side Includes for HTML in IIS7

參考來源:http://tech.mikeal.com/blog1.php/server-side-includes-for-html-in-iis7

用HTML設定FB專頁時,在FB瀏覽此專頁IIS出現 HTTP 錯誤 405.0 - Method Not Allowed.
直接用瀏覽器開啟頁面位址的話又可正常瀏覽.
Google後.發現如下圖的設定就可以正常運作