PHP에서 자동으로 양식을 제출하는 방법(fsockopen 및 컬 기반)_php 기술

PHP中文网
풀어 주다: 2016-05-16 09:00:03
원래의
1748명이 탐색했습니다.

이 기사의 예에서는 PHP가 fsockopen 및 컬을 기반으로 양식을 자동으로 제출하는 방법을 설명합니다. 참고할 수 있도록 모든 사람과 공유하세요. 자세한 내용은 다음과 같습니다.

fsockopen과 컬 모두 PHP 자동 양식 제출을 수행할 수 있습니다

1. fsockopen 방법:

php 코드:

<?php
/*-----------------------------------------------------------
*功能:使用PHP socke 向指定页面提交数据
*作者:果冻  说明:post($url, $data)
*
*    $url   = &#39;http://www.xxx.com:8080/login.php&#39;;
*    $data[user] = &#39;hong&#39;;
*    $data[pass] = &#39;xowldo&#39;;
*    echo post($url, $data);
*-----------------------------------------------------------*/
function post($url, $data) {
  $url = parse_url($url);
   if (!$url) return "couldn&#39;t parse url";
   if (!isset($url[&#39;port&#39;])) { $url[&#39;port&#39;] = ""; }
   if (!isset($url[&#39;query&#39;])) { $url[&#39;query&#39;] = ""; }
  $encoded = "";
   while (list($k,$v) = each($data)) {
    $encoded .= ($encoded ? "&" : "");
    $encoded .= rawurlencode($k)."=".rawurlencode($v);
   }
  $fp = fsockopen($url[&#39;host&#39;], $url[&#39;port&#39;] ? $url[&#39;port&#39;] : 80);
   if (!$fp) return "Failed to open socket to $url[host]";
  fputs($fp, sprintf("POST %s%s%s HTTP/1.0n", $url[&#39;path&#39;], $url[&#39;query&#39;] ? "?" : "", $url[&#39;query&#39;]));
  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, "$encodedn");
  $line = fgets($fp,1024);
   if (!eregi("^HTTP/1.. 200", $line)) return;
  $results = ""; $inheader = 1;
   while(!feof($fp)) {
    $line = fgets($fp,1024);
     if ($inheader && ($line == "n" || $line == "rn")) {
    $inheader = 0;
   }
   elseif (!$inheader) {
    $results .= $line;
   }
   }
  fclose($fp);
   return $results;
}
/*
$url = &#39;http://video.xxx.com:80/game_vm.php&#39;;
$data[&#39;gid&#39;] = &#39;1&#39;;
echo post($url, $data);
*/
?>
로그인 후 복사

2. Curl 방법:

php 코드:

<?php
  $url = &#39;http://localhost/curl/result.php&#39;;
  $params = "param=123¶m2=333"; //What will be posted
  $user_agent = "Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)";
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_POST,1);
  curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
  curl_setopt($ch, CURLOPT_URL,$url);
  curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  $result=curl_exec ($ch); //execut
  curl_close ($ch);
  echo "Results: <br>".$result;
?>
로그인 후 복사

result.php (단순 테스트용)

<?php
print_r($_POST);
?>
로그인 후 복사

위는 php 자동 폼 제출 방식의 내용입니다(fsockopen 기반) 그리고 컬)_php 스킬, 더 많은 관련 내용은 PHP 중국어 홈페이지(m.sbmmt.com)를 주목해주세요!


관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!