{"id":469,"date":"2024-01-23T16:35:56","date_gmt":"2024-01-23T08:35:56","guid":{"rendered":"https:\/\/blog.quantoyo.com\/?p=469"},"modified":"2024-01-24T10:51:39","modified_gmt":"2024-01-24T02:51:39","slug":"php-proxy-%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/blog.quantoyo.com\/?p=469","title":{"rendered":"PHP proxy \u4f7f\u7528 fsockopen"},"content":{"rendered":"\n<p>\u4f7f\u7528socket \u4f86\u5be6\u4f5c proxy \u529f\u80fd<\/p>\n\n\n\n<!--more-->\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background has-small-font-size\"><code>&lt;?php\n$targetHost = \"192.168.123.123;        \/\/\u8981\u8f49\u9001\u7684\u76ee\u7684HOST(\u53e6\u4e00\u53f0\u4f3a\u670d\u5668)\n$targetPort = 80;\n$targetPath = \"\/target.php\";            \/\/\u8981\u8f49\u767c\u7684\u76ee\u7684\u8def\u5f91\n\n\/\/ \u53d6\u5f97\u539f\u59cb\u8acb\u6c42\u7684\u65b9\u6cd5\n$method = $_SERVER&#91;'REQUEST_METHOD'];\n\n\/\/ \u53d6\u5f97\u539f\u59cb\u8acb\u6c42\u7684\u6240\u6709\u53c3\u6578\n$params = http_build_query($_REQUEST);\n\n\/\/ \u5efa\u7acb\u539f\u59cb\u8acb\u6c42\u982d\n$requestHeaders = \"$method $targetPath HTTP\/1.1\\r\\n\";\n$requestHeaders .= \"Host: $targetHost\\r\\n\";\n$requestHeaders .= \"Content-Length: \" . strlen($params) . \"\\r\\n\";\n$requestHeaders .= \"Content-Type: application\/x-www-form-urlencoded\\r\\n\";\n$requestHeaders .= \"Connection: close\\r\\n\\r\\n\";\n\n\/\/ \u958b\u555f\u8207\u76ee\u6a19\u4f3a\u670d\u5668\u7684\u9023\u63a5\n$socket = fsockopen($targetHost, $targetPort, $errno, $errstr, 30);\n\nif (!$socket) {\n    header('HTTP\/1.1 500 Internal Server Error');\n    echo \"Error: $errstr ($errno)\";\n} else {\n    fwrite($socket, $requestHeaders . $params);     \/\/ \u50b3\u9001\u539f\u59cb\u8acb\u6c42\n    $isFlush = false;\n    $byteArray = unpack('C*', 'abce');  \/\/ \u521d\u59cb\u53164\u500bbyte\u7684array\n    while (!feof($socket)) {\n        $byte = fread($socket, 1);      \/\/ \u5f9e\u76ee\u6a19\u4f3a\u670d\u5668\u8b80\u53d6\u56de\u61c9\u4e26\u5373\u6642\u50b3\u9001\u7d66\u5ba2\u6236\u7aef\n        if(!$isFlush){\n            array_shift($byteArray);    \/\/ \u8fed\u4ee3\u63a7\u7ba1\u7528\u7684array\uff0c\u70ba\u4e86\u904e\u6ffe\\r\\n\\r\\n\n            array_push($byteArray, tobyte($byte));\n            if(byteArray2String($byteArray) == \"\\r\\n\\r\\n\"){     \/\/\u904e\u6ffe\u6389header\uff0c\u5f8c\u7e8c\u7684\u624d\u50b3\u9001\n                $isFlush = true;\n            }\n        }else{\n            echo $byte;     \/\/\u9019\u908a\u624d\u662f\u5167\u5bb9\n            flush();\n        }\n    }\n    fclose($socket);        \/\/ \u95dc\u9589\u9023\u7dda\n}\n\nfunction byteArray2String($byteArray) {\n    $chars = array_map(\"chr\", $byteArray);\n    return join($chars);\n}\n\nfunction tobyte($str){\n    $tmpAry = unpack('C*', $str);\n    return $tmpAry&#91;1];\n}\n\n?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u6ce8\u610f\uff1a<\/h2>\n\n\n\n<p>1. php \u9810\u8a2d\u6c92\u6709byte \u578b\u614b\uff0c\u6240\u4ee5\u8981\u653e\u5165byte array \u7684\u5b57\u4e32\u5fc5\u9808\u8f49\u578b<\/p>\n\n\n\n<p>2. fread \u8b80\u53d6\u51fa\u4f86\u7684\u5176\u5be6\u662f\u5b57\u4e32<\/p>\n\n\n\n<p>3. unpack\u9019\u500bfunction \u6703return \u4e00\u500bindex \u5f9e1 \u958b\u59cb\u7684 array<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Caution<br>If you do not name an element, numeric indices starting from 1 are used. Be aware that if you have more than one unnamed element, some data is overwritten because the numbering restarts from 1 for each element.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u53c3\u8003\u8cc7\u6599\uff1a<\/p>\n\n\n\n<p><a href=\"https:\/\/www.php.net\/manual\/en\/function.unpack.php\">https:\/\/www.php.net\/manual\/en\/function.unpack.php<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528socket \u4f86\u5be6\u4f5c proxy \u529f\u80fd<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[82,83,77,79,81,76,18,80,78,75],"class_list":["post-469","post","type-post","status-publish","format-standard","hentry","category-technology","tag-byte","tag-byte-array","tag-fsockopen","tag-header","tag-http","tag-php","tag-proxy","tag-request","tag-socket","tag-unpack"],"_links":{"self":[{"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/posts\/469","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=469"}],"version-history":[{"count":4,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/posts\/469\/revisions"}],"predecessor-version":[{"id":476,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/posts\/469\/revisions\/476"}],"wp:attachment":[{"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=469"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=469"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=469"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}