PDA

View Full Version : حرفه ای: مشکل در سوکت



qti3e.qti3e
یک شنبه 08 فروردین 1395, 19:05 عصر
سلام همگی
راستش داشتم یه کلاس مینوشتم برای درخواست های GET و POST با استفاده از تابع fsockopen اما خب نمیدونم چرا کار نمیکنه کد رو میزارم اگه کسی میتونه یه کمک کنه!


<?php
/************************************************** ***************************
* In the name of God the Most Beneficent the Most Merciful *
*_________________________________________________ __________________________*
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
*_________________________________________________ __________________________*
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
*_________________________________________________ __________________________*
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*_________________________________________________ __________________________*
* Created by Qti3e *
* <http://Qti3e.Github.io> LO-VE <Qti3eQti3e@Gmail.com> *
************************************************** ***************************/

/**
* Class browser
*/
class browser{
private static $url;
private static $scheme;
private static $host;
private static $path;
private static $port;
private static $socket;

private static $cookies = array();
private static $default_ports = array(
'http' => 80,
'https'=> 80,
'ftp' => 21
);
private static $headers = array(
'Host' =>'',
'Connection' =>'Keep-alive',
'Accept' =>'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Upgrade-Insecure-Requests' =>1,
'User-Agent' =>'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36',
'Accept-Encoding' =>'gzip, deflate, sdch',
'Accept-Language' =>'en-GB,en;q=0.8,en-US;q=0.6,fa;q=0.4',
'Cookie' =>''
);
public static $showErrors = true;

private static function parseHeaders($header){
echo "browser::parseHeaders\n";
$keys = array_keys($header);
$count = count($header);
$re = '';
for($i = 0;$i < $count;$i++){
$key= $keys[$i];
$val= $header[$key];
if(is_array($val)){
$c = count($val);
$ks = array_keys($val);
$r = '';
for($j = 0;$j < $c;$j++){
$k = $ks[$j];
$v = $val[$k];
$r.= urlencode($k).'='.urlencode($v).';';
}
$val = substr($r,0,-1);
}else{
$val = urlencode($val);
}
$re .= urlencode($key).': '.$val."\r\n";
}
return $re;
}
private static function ParseRespond($url,$respond){
//TODO
echo "browser::ParseRespond\n";
return $respond;
}
private static function parseURL($url){
echo "browser::parseURL\n";
$parsed = parse_url($url);
self::$host = $parsed['host'];
self::$scheme = isset($parsed['scheme']) ? $parsed['scheme'] : 'http';
self::$path = isset($parsed['path']) ? $parsed['path'] : '/';
self::$port = isset($parsed['port']) ? $parsed['port'] : self::$default_ports[self::$scheme];
self::$url = self::$scheme.'://'.self::$host.':'.self::$port.'/'.self::$path;
}
private static function Request($url,$request){
echo "browser::Request\n";
self::$socket = @fsockopen(self::$host,self::$port,$errorN,$errorS tr);
if(self::$socket){
echo "\tYes\n";
fwrite(self::$socket,$request);
echo "$request\n";
$re = '';
echo "\tWhile Start\n";
while(!feof(self::$socket)){
$p = fgets(self::$socket,128);
$re .= $p;
echo "\t\t$p\n";
}
echo "\tWhile End\n";
return self::ParseRespond($url,$re);
}elseif(self::$showErrors){
echo "Error in creating socket\n error code:#$errorN\n error detail:$errorStr";
}
return false;
}
private static function parseData($data){
echo "browser::parseData\n";
$count = count($data);
$keys = array_keys($data);
$re = '';
for($i = 0;$i < $count;$i++){
$key = $keys[$i];
$val = $data[$key];
$re .= urlencode($key).'='.urlencode($val).'&';
}
return substr($re,0,-1);
}
private static function getCookies($url){
//TODO
return [];
}
public static function URL($url,$getData = []){
echo "browser::URL\n";
if(empty($getData)){
return urlencode($url);
}
return urlencode($url).'?'.self::parseData($getData);
}
public static function POST($url,$data = [],$getData = []){
echo "browser::POST\n";
self::parseURL($url);
$data = self::parseData($data);
$length = strlen($data);
$url = self::URL($url,$getData);
$request = 'POST '.$url." HTTP/1.0\r\n";
$header = self::$headers;
$header['Cookie'] = self::getCookies(self::$url);
$header['Host'] = self::$host;
$header['Content-Type'] = 'application/x-www-form-urlencoded';
$header['Content-Length'] = $length;
if(empty($header['Cookie'])){
unset($header['Cookie']);
}
$request .= self::parseHeaders($header);
return self::Request(self::$url,$request);
}
public static function GET($url,$data = []){
echo "browser::GET\n";
self::parseURL($url);
$url = self::URL(self::$path,$data);
$request = 'GET '.$url." HTTP/1.0\r\n";
$header = self::$headers;
$header['Cookie'] = self::getCookies(self::$url);
$header['Host'] = self::$host;
if(empty($header['Cookie'])){
unset($header['Cookie']);
}
$request .= self::parseHeaders($header);
return self::Request(self::$url,$request);
}
}
var_dump(browser::GET('http://127.0.0.1/lib/Socket/a.php',[
'Name'=>'Qti3e'
]));
اینم لاگ:



php browser.php
browser::GET
browser::parseURL
browser::URL
browser::parseData
browser::parseHeaders
browser::Request
Yes
GET %2Flib%2FSocket%2Fa.php?Name=Qti3e HTTP/1.0
Host: 127.0.0.1
Connection: Keep-alive
Accept: text%2Fhtml%2Capplication%2Fxhtml%2Bxml%2Capplicat ion%2Fxml%3Bq%3D0.9%2Cimage%2Fwebp%2C%2A%2F%2A%3Bq %3D0.8
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla%2F5.0+%28Windows+NT+10.0%3B+WOW64%29+Apple WebKit%2F537.36+%28KHTML%2C+like+Gecko%29+Chrome%2 F49.0.2623.87+Safari%2F537.36
Accept-Encoding: gzip%2C+deflate%2C+sdch
Accept-Language: en-GB%2Cen%3Bq%3D0.8%2Cen-US%3Bq%3D0.6%2Cfa%3Bq%3D0.4


While Start

دیگه جلوتر نمیره

qti3e.qti3e
دوشنبه 09 فروردین 1395, 11:46 صبح
دوستان کسی واقعا نمیخواد کمک کنه؟؟؟
آقا نیاز دارم کمک :گریه: