ホームページ バックエンド開発 PHPチュートリアル PHP 模倣 ASP xmlhttprequest リクエスト データ コード_PHP チュートリアル

PHP 模倣 ASP xmlhttprequest リクエスト データ コード_PHP チュートリアル

Jul 20, 2016 am 11:09 AM
asp method php url コード データ クラス名 聞く

クラス名: httprequest($url="",$method="get",$usesocket=0)
//$url は要求されたアドレスです。$usesocket のデフォルトは 0 で、fsockopen を使用します。 Method、1 に設定した場合は、socket_create メソッドを使用します

Method:
open($ip="",$port=-1) //同じサーバーへの接続を開きます。デフォルトでは、これらを設定する必要はありません。 2 つのパラメーター (同僚が Linux で使用する場合)、リクエストはホスト名によって解決された IP ではないため、これら 2 つのパラメーターは実際のサーバー IP に接続するために追加されます)
settimeout($timeout=0) // のタイムアウトを設定しますデータの取得。有効にするには、send メソッドが呼び出される前に設定する必要があります。単位は秒です。デフォルト値 0 は無制限です
setrequestheader($key,$value="") // リクエスト ヘッダーを設定します。これは設定する必要があります。 send メソッドが呼び出される前に有効になります
removerequestheader($key,$value="") / / 指定されたキー値のリクエスト ヘッダーを削除します。有効にするためには send メソッドが呼び出される前に呼び出す必要があります
send($ data="") //データ $data をサーバーに送信
getresponsebody() //サーバーから返されたテキストを取得
getallresponseheaders( ) //サーバー応答のすべてのヘッダー情報を取得
getresponseheader($key) //Getサーバー応答の特定のヘッダー情報 (server、set_cookie など)

属性:
$url //リクエストされる URL
$method //リクエスト メソッド (post/get)
$port //リクエストされたポート
$hostname //リクエストされたホスト名
$uri //urlのファイル部分
$protocol //リクエストプロトコル(http)(この属性の上記5つを含む属性はURLを通じてプログラムによって自動的に解析されます)
$excption / /例外情報
$_headers=array() //リクエストヘッダ array("key"=>"value")
$_senddata //サーバーに送信されたデータ
$status //返されたステータスコード
$statustext //ステータス情報
$httpprotocolversion //サーバーの http プロトコルのバージョン

注:
post メソッドでリクエストする場合、ホストヘッダーはプログラムによって自動的に設定されます。content-length と content-type は自動的に設定されます。
サポート gzip 圧縮ページ

inc.http.php チュートリアル ファイル

class httprequest{
public $url,$method,$port,$hostname,$uri,$protocol,$excption,$_headers=array(),$_senddata,$status,$statustext,$httpprotocolversion;
private $fp =0,$_buffer="",$responsebody,$responseheader,$timeout=0,$usesocket;
//构造関数数
function __construct($url="",$method="get",$usesocket=0) {
$this->url = $url;
$this->method = strtoupper($method);
$this->usesocket = $usesocket;
$this->setrequestheader("accept"," */*");
$this->setrequestheader("accept- language","zh-cn");
$this->setrequestheader("accept-encoding","gzip, deflate");
$ this->setrequestheader("user-agent","httprequest class 1.0");  //可调用setrequestheader来修正
}

//接服务器
public function open($ip="",$port=-1){
if(!$this->_geturlinfo()) return false;
$this->setrequestheader("host",$this->hostname);
$this->setrequestheader("connection","close");
$ip = ($ip=="" ? $this ->ホスト名 : $ip);
$port = ($port==-1 ? $this->ポート : $port);
if($this->usesocket==1){
if(! $this->fp=$socket=socket_create(af_inet,sock_stream,0)) {
$this->excption="ソケットを作成できません";return false;
}else{
if(!socket_connect($this ->fp,$ip, $port) ){
$this->excption="サーバー " に接続できません。 $this->ホスト名 。 「ポート上」。 $this->port;return false;
}
}
}else{
if(!$this->fp=fsockopen($ip, $port,$errno,$errstr,10)) {
$this ->excption="サーバーに接続できません" 。 $this->ホスト名 。 「ポート上」。 $this->port;return false;
}
}
return true;
}

public function send($data=""){
if(!$this->fp){$this-> excption="はリソース ID ではありません";return false;}
if($this->method=="get" && $data!=""){
$s_str="?";
if(strpos( $this->uri,"?")>0) $s_str = "&";
$this->uri.= $s_str 。 $data;
$data="";
}
$senddata=$this->メソッド 。 「」。 $this->uri 。 " http/1.1rn";
if($this->method=="post"){
$this->setrequestheader("content-length",strlen($data));
$this-> setrequestheader("content-type", "application/x-www-form-urlencoded");
}
foreach($this->_headers as $keys => $value){
$senddata .= "$keys : $valuern";
}
$senddata .= "rn";
if($this->method=="post") $senddata .= $data;
$this->_senddata = $senddata;
if($this->usesocket==1){
socket_write($this->fp,$this->_senddata);
$buffer="";
$timestart = time();
do{
if($this->timeout>0){
if(time()-$timestart>$this->timeout){break;}
}
$this->_buffer.=$buffer;
$buffer =socket_read($this->fp,4096);
}while($buffer!="");
socket_close($this->fp); 
}else{
fputs($this->fp, $senddata);
$this->gt;_buffer="";
$timestart = time();
while(!feof($this->fp) )
{
if($this->timeout>0){
if(time()-$timestart>$this->timeout){break;}
}
$this->gt;_buffer.=fgets( $this->fp,4096);
}
   fclose($this->fp);   
}
$this->_splitcontent();
$this->_getheaderinfo();
}

public function getresponsebody(){
if($this->getresponseheader("content-encoding")== "gzip" && $this->getresponseheader("transfer-encoding")=="chunked"){
return gzdecode_1(transfer_encoding_chunked_decode($this->responsebody));
}else if($this->getresponseheader ("content-encoding")=="gzip"){
return gzdecode_1($this->responsebody);
}else{
return $this->responsebody;
}
}

public function getallresponseheaders() {
return $this->responseheader;
}

public function getresponseheader($key){
$key = str_replace("-","-",$key);
$headerstr = $this->responseheader 。 "rn";
$count = preg_match_all("/n$key:(.+?)r/is",$headerstr,$result,preg_set_order);
if($count>0){
$returnstr="" ;
foreach($result as $key1=>$value){
if(strtoupper($key)=="set-cookie"){
$value[1] = substr($value[1],0, strpos($value[1],";"));
}
$returnstr .= ltrim($value[1]) 。 "; ";
}
$returnstr = substr($returnstr,0,strlen($returnstr)-2);
return $returnstr;
}else{return "";}
}

public function settimeout($timeout =0){
$this->timeout = $timeout; 
}

public function setrequestheader($key,$value=""){
$this->_headers[$key]=$value;
}

public function Removerequestheader($key){
if(count( $this->_headers)==0){return;}
$_temp=array();
foreach($this->_headers as $keys => $value){
if($keys!=$ key){
$_temp[$keys]=$value;
}
}
$this->_headers = $_temp;
}

//拆分url
private function _geturlinfo(){
$url = $ this->url;
$count = preg_match("/^http://([^:/]+?)(:(d+))?/(.+?)$/is",$url,$ result);
if($count>0){
$this->uri="/" . $result[4];
}else{
$count = preg_match("/^http://([^:/]+?)(:(d+))?(/)?$/is",$url ,$result);
if($count>0){
$this->uri="/";
}
}
if($count>0){
$this->protocol="http" ;
$this->hostname=$result[1];
if(isset($result[2]) && $result[2]!="") {$this->port=intval($result[ 3]);}else{$this->port=80;}
return true;
}else{$this->excption="url 形式エラー";return false;}
}

private function _splitcontent( ){
$this->responseheader="";
$this->responsebody="";
$p1 = strpos($this->_buffer,"rnrn");
if($p1>0) {
$this->responseheader = substr($this->gt;_buffer,0,$p1);
if($p1+4_buffer)){
$this->responsebody = substr($this->_buffer,$p1+4);
}
}
}

プライベート関数 _getheaderinfo(){
$headerstr = $this->responseheader;
$count = preg_match("/^ http/(.+?)s(d+)s(.+?)rn/is",$headerstr,$result);
if($count>0){
$this->httpprotocolversion = $result[1 ];
$this->status = intval($result[2]);
$this->statustext = $result[3];
}
}
}


//以下两関数数参考网络
function gzdecode_1 ($data) {
$data = ($data);
if (!function_exists ( 'gzdecode' )) {
$flags = ord ( substr ( $data, 3 , 1 ) );
$headerlen = 10;
$extralen = 0;
$filenamelen = 0;
if ($flags & 4) {
$extralen = unpack ( 'v', substr ( $data, 10, 2 ) );
$extralen = $extralen [1];
$headerlen += 2 + $extralen;
}
if ($flags & 8) // ファイル名
$headerlen = strpos ( $data, chr ( 0 ), $headerlen ) + 1;
if ($flags & 16) // コメント
$headerlen = strpos ( $data, chr ( 0 ), $headerlen ) + 1;
if ($flags & 2) // 末尾に crcファイルの
$headerlen += 2;
$unpacked = @gzinflate ( substr ( $data, $headerlen ) );
if ($unpacked === false)
$unpacked = $data;
return $unpacked;
} else{
return gzdecode($data);
}
}

function transfer_encoding_chunked_decode($in) {
$out = "";
while ( $in !="") {
$lf_pos = strpos ( $in, "12" );
if ($lf_pos === false) {
$out .= $in;
break;
}
$chunk_hex = トリム ( substr ( $in, 0, $lf_pos ) );
$sc_pos = strpos ( $chunk_hex, ';' );
if ($sc_pos !== false)
$chunk_hex = substr ( $chunk_hex, 0, $sc_pos );
if ($chunk_hex =="") {
$out .= substr ( $in, 0, $lf_pos );
$in = substr ( $in, $lf_pos + 1 );
続行;
}
$chunk_len = hexdec ( $chunk_hex );
if ($chunk_len) {
$out .= substr ( $in, $lf_pos + 1, $chunk_len );
$in = substr ( $in, $lf_pos + 2 + $chunk_len );
} else {
$in = "";
}
}
return $out;
}
function utf8togb($str){
return iconv("utf-8","gbk",$str);
}

function gbtoutf8($str){
return iconv( "gbk","utf-8",$str);
}
?>

response.asp教程文件

<%
response.cookies("a") = "anlige"
response.cookies ("a").expires = dateadd("yyyy",1,now())
response.cookies("b")("c") = "wsdasdadsa"
re​​sponse.cookies("b")("d ") = "ddd"
response.cookies("b").expires = dateadd("yyyy",1,now())
response.write "querystring : " & request.querystring & "
"
request.querystring の各 v について
response.write v & "=" & request.querystring(v) & "
"
next
response.write "
form : " & request.form & "
"
request.form の各 v について
response.write v & "=" & request.form(v) & "
"
次の
response .write "
url : " & request.servervariables("url") & "
"
response.write "referer : " & request.servervariables("http_referer") & "< ;br />gt;"
response.write "host : " & request.servervariables("http_host") & "
gt;"
response.write "user-agent : " & request.servervariables("http_user_agent" ) & "
"
response.write "cookie" & request.servervariables("http_cookie")
%>

index.php文件

転送データを取得
転送データを送信
ソース情報をサーバーに送信
ユーザーエージェントをサーバーに送信
サーバーから返されたステータスを取得します
サーバーの応答ヘッダーを取得します
画像を保存



include("inc_http.php");
$responseurl = " http: //dev.mo.cn/aiencode/http/response.asp";

$act = isset($_get["action"]) ? $_get["action"] : "";
if($ act = = "get"){ //データを取得

$myhttp = new httprequest("$responseurl?a=text");
$myhttp->open();
$myhttp->send(" name=anlige&city =" . urlencode("杭州"));
echo($myhttp->getresponsebody());

}else if($act == "post"){ //データを投稿

$ myhttp = new httprequest ("$responseurl?a=text","post");
$myhttp->open();
$myhttp->send("name=anlige&city=" . urlencode("杭州") );
echo ($myhttp->getresponsebody());

}else if($act == "header_referer"){ //ソース情報をサーバーに送信します

$myhttp = new httprequest("$responseurl? a=text" ,"投稿");
$myhttp->open();
$myhttp->setrequestheader("リファラー","http://www.baidu.com");
$myhttp->send(" name=anlige&city=" . urlencode("Hangzhou"));
echo($myhttp->getresponsebody());

}else if($act == "header_useragent"){ //サーバーに ユーザーを送信します-エージェント

$myhttp = new httprequest("$responseurl?a=text","post");
$myhttp->open();
$myhttp->setrequestheader("referer","http :// www.baidu.com");
$myhttp->setrequestheader("user-agent","mozilla/4.0 (互換性; msie 7.0; windows nt 6.0; trident/4.0)");
$myhttp->send ("name=anlige&city=" . urlencode("杭州"));
echo($myhttp->getresponsebody());

}else if($act == "status"){ // によって返されるステータスを取得しますサーバー

$myhttp = new httprequest("$responseurl?a=text","post");
$myhttp->open();
$myhttp->send("name=anlige&city=" . urlencode ("杭州"));
echo($myhttp->ステータス . " " . $myhttp->ステータステキスト ."

");
echo($myhttp -> ;getresponsebody());

}else if($act == "get_headers"){ //サーバーの応答ヘッダーを取得します

$myhttp = new httprequest("$responseurl?a=text","get") ;
$myhttp->open();
$myhttp->send("name=anlige&city=" . urlencode("杭州"));
echo($myhttp->getallresponseheaders()."

");
echo($myhttp->getresponseheader("server")."

");

}else if($act == "get_image"){

$myhttp = new httprequest("http://www.baidu.com/img/baidu_logo.gif");
$myhttp->open();
$myhttp-> ; send();
$fp = @fopen("demo.gif","w");
fwrite($fp,$myhttp->getresponsebody());
fclose($fp);
echo(" < ;img src="demo.gif" />");
}

?>


www.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/444789.html技術記事クラス名: httprequest($url=,$method=get,$usesocket=0) //$url は要求されたアドレスです。デフォルトの要求メソッドは get です。$usesocket のデフォルトは 0、1 に設定されている場合は fsockopen メソッドを使用します。 、そう使ってください...
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

PHP:Web開発の重要な言語 PHP:Web開発の重要な言語 Apr 13, 2025 am 12:08 AM

PHPは、サーバー側で広く使用されているスクリプト言語で、特にWeb開発に適しています。 1.PHPは、HTMLを埋め込み、HTTP要求と応答を処理し、さまざまなデータベースをサポートできます。 2.PHPは、ダイナミックWebコンテンツ、プロセスフォームデータ、アクセスデータベースなどを生成するために使用され、強力なコミュニティサポートとオープンソースリソースを備えています。 3。PHPは解釈された言語であり、実行プロセスには語彙分析、文法分析、編集、実行が含まれます。 4.PHPは、ユーザー登録システムなどの高度なアプリケーションについてMySQLと組み合わせることができます。 5。PHPをデバッグするときは、error_reporting()やvar_dump()などの関数を使用できます。 6. PHPコードを最適化して、キャッシュメカニズムを使用し、データベースクエリを最適化し、組み込み関数を使用します。 7

PHP対Python:違いを理解します PHP対Python:違いを理解します Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHPは、シンプルな構文と高い実行効率を備えたWeb開発に適しています。 2。Pythonは、簡潔な構文とリッチライブラリを備えたデータサイエンスと機械学習に適しています。

PHPとPython:2つの一般的なプログラミング言語を比較します PHPとPython:2つの一般的なプログラミング言語を比較します Apr 14, 2025 am 12:13 AM

PHPとPythonにはそれぞれ独自の利点があり、プロジェクトの要件に従って選択します。 1.PHPは、特にWebサイトの迅速な開発とメンテナンスに適しています。 2。Pythonは、データサイエンス、機械学習、人工知能に適しており、簡潔な構文を備えており、初心者に適しています。

アクション中のPHP:実際の例とアプリケーション アクション中のPHP:実際の例とアプリケーション Apr 14, 2025 am 12:19 AM

PHPは、電子商取引、コンテンツ管理システム、API開発で広く使用されています。 1)eコマース:ショッピングカート機能と支払い処理に使用。 2)コンテンツ管理システム:動的コンテンツの生成とユーザー管理に使用されます。 3)API開発:RESTFUL API開発とAPIセキュリティに使用されます。パフォーマンスの最適化とベストプラクティスを通じて、PHPアプリケーションの効率と保守性が向上します。

PHPの永続的な関連性:それはまだ生きていますか? PHPの永続的な関連性:それはまだ生きていますか? Apr 14, 2025 am 12:12 AM

PHPは依然として動的であり、現代のプログラミングの分野で重要な位置を占めています。 1)PHPのシンプルさと強力なコミュニティサポートにより、Web開発で広く使用されています。 2)その柔軟性と安定性により、Webフォーム、データベース操作、ファイル処理の処理において顕著になります。 3)PHPは、初心者や経験豊富な開発者に適した、常に進化し、最適化しています。

PHPおよびPython:さまざまなパラダイムが説明されています PHPおよびPython:さまざまなパラダイムが説明されています Apr 18, 2025 am 12:26 AM

PHPは主に手順プログラミングですが、オブジェクト指向プログラミング(OOP)もサポートしています。 Pythonは、OOP、機能、手続き上のプログラミングなど、さまざまなパラダイムをサポートしています。 PHPはWeb開発に適しており、Pythonはデータ分析や機械学習などのさまざまなアプリケーションに適しています。

PHP対その他の言語:比較 PHP対その他の言語:比較 Apr 13, 2025 am 12:19 AM

PHPは、特に迅速な開発や動的なコンテンツの処理に適していますが、データサイエンスとエンタープライズレベルのアプリケーションには良くありません。 Pythonと比較して、PHPはWeb開発においてより多くの利点がありますが、データサイエンスの分野ではPythonほど良くありません。 Javaと比較して、PHPはエンタープライズレベルのアプリケーションでより悪化しますが、Web開発により柔軟性があります。 JavaScriptと比較して、PHPはバックエンド開発により簡潔ですが、フロントエンド開発のJavaScriptほど良くありません。

PHPおよびPython:コードの例と比較 PHPおよびPython:コードの例と比較 Apr 15, 2025 am 12:07 AM

PHPとPythonには独自の利点と短所があり、選択はプロジェクトのニーズと個人的な好みに依存します。 1.PHPは、大規模なWebアプリケーションの迅速な開発とメンテナンスに適しています。 2。Pythonは、データサイエンスと機械学習の分野を支配しています。

See all articles