2011年10月20日 星期四

JavaScript Note

Javascript的參考資料:
http://irw.ncut.edu.tw/peterju/jscript.html

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