게시물 요청을 시뮬레이션하기 위한 PHP의 세 가지 일반적인 용도

墨辰丷
풀어 주다: 2023-03-30 22:12:02
원래의
5341명이 탐색했습니다.

이 글은 주로 게시물 요청을 시뮬레이션하기 위한 PHP의 세 가지 일반적인 용도를 소개합니다. 관심 있는 친구들이 이 글을 참고하여 모든 사람에게 도움이 되기를 바랍니다.

이 글의 예는 시뮬레이션된 게시물 요청을 구현하기 위한 PHP 방법을 설명하며, 세부 사항은 다음과 같습니다:

class Request{
 public static function post($url, $post_data = '', $timeout = 5){//curl
  $ch = curl_init(); 
  curl_setopt ($ch, CURLOPT_URL, $url);
  curl_setopt ($ch, CURLOPT_POST, 1);
  if($post_data != ''){
   curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  }
  curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
  curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  curl_setopt($ch, CURLOPT_HEADER, false);
  $file_contents = curl_exec($ch);
  curl_close($ch);
  return $file_contents;
 } 
 public static function post2($url, $data=array()){//file_get_content
  $postdata = http_build_query(
   $data
  );   
  $opts = array('http' =>
      array(
       'method' => 'POST',
       'header' => 'Content-type: application/x-www-form-urlencoded',
       'content' => $postdata
      )
  );   
  $context = stream_context_create($opts);
  $result = file_get_contents($url, false, $context); 
  return $result;
 } 
 public static function post3($host,$path,$query,$others=''){//fsocket
  $post="POST $path HTTP/1.1\r\nHost: $host\r\n";
  $post.="Content-type: application/x-www-form-";
  $post.="urlencoded\r\n${others}";
  $post.="User-Agent: Mozilla 4.0\r\nContent-length: ";
  $post.=strlen($query)."\r\nConnection: close\r\n\r\n$query";
  $h=fsockopen($host,80);
  fwrite($h,$post);
  for($a=0,$r='';!$a;){
    $b=fread($h,8192);
    $r.=$b;
    $a=(($b=='')?1:0);
   }
  fclose($h);
  return $r;
 }
}
$url='http://******/con/Inter.php';
$data=Request::post($url,array('api'=>'tag_list'));
$data2=Request::post2($url,array('api'=>'tag_list'));
echo $data;
로그인 후 복사

요약: 위는 이 글의 전체 내용입니다. 모든 분들의 학습에 도움이 되기를 바랍니다. .

관련 권장 사항:

PHP는 잦은 IP 액세스를 차단하여 웹 사이트의 공격을 방지합니다.

php를 통해 온라인 사용자 수를 계산하는 방법

PHP+Mysql+jQuery로 구현된 쿼리 및 목록 상자 선택

위 내용은 게시물 요청을 시뮬레이션하기 위한 PHP의 세 가지 일반적인 용도의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

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