PHP模拟POST的步骤
Jun 13, 2016 pm 01:21 PM
PHP模拟POST的方法
PHP主动POST数据到JSP
采用哪种方式好?
最好有例子,谢谢
------解决方案--------------------
后台模拟 哪种都差不多 最常用的curl模块
------解决方案--------------------
- PHP code
function post_url($url, $post = "", $host = "www.ydtuiguang.com", $referrer = 'http://www.ydtuiguang.com/', $proxy = -1){ if(function_exists("curl_init")){ $ch = @curl_init(); @curl_setopt($ch, CURLOPT_URL, $url); if(!empty($proxy["address"])) @curl_setopt($ch, CURLOPT_PROXY, strpos($proxy["address"], "http") === 0 ? $proxy["address"] : "http://".$proxy["address"]); if(!empty($proxy["account"]) && !empty($proxy["password"])) @curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxy["account"].":".$proxy["password"]); @curl_setopt($ch, CURLOPT_REFERER, $referrer); @curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.0)"); @curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE_FILE_PATH); @curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIE_FILE_PATH); @curl_setopt($ch, CURLOPT_HEADER, 0); @curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); @curl_setopt($ch, CURLOPT_TIMEOUT, 1000); if (!empty($post)) { @curl_setopt($ch, CURLOPT_POST, 1); @curl_setopt($ch, CURLOPT_POSTFIELDS, $post); } $result = @curl_exec($ch); @curl_close($ch); }elseif(function_exists("fsockopen")){ $httpheader = "POST ".$url." HTTP/1.1\r\n"; $httpheader .= "Accept: */*\r\n"; $httpheader .= "Accept-Language: zh-cn\r\n"; $httpheader .= "Referer: ".$referrer."\r\n"; $httpheader .= "Content-Type: application/x-www-form-urlencoded\r\n"; $httpheader .= "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)\r\n"; $httpheader .= "Host: ".$host."\r\n"; $httpheader .= "Content-Length: ".strlen($post)."\r\n"; $httpheader .= "Connection: Keep-Alive\r\n"; $httpheader .= "\r\n"; $httpheader .= $post; $fd = fsockopen($host, 80); fwrite($fd, $httpheader); $result = ""; while(!feof($fd)){ $result .= fread($fd, 8192); } fclose($fd); }elseif(function_existes('file_get_contents')){ $httpheader = "POST ".$url." HTTP/1.1\r\n"; $httpheader .= "Accept: */*\r\n"; $httpheader .= "Accept-Language: zh-cn\r\n"; $httpheader .= "Referer: ".$referrer."\r\n"; $httpheader .= "Content-Type: application/x-www-form-urlencoded\r\n"; $httpheader .= "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)\r\n"; $httpheader .= "Host: ".$host."\r\n"; $httpheader .= "Content-Length: ".strlen($post)."\r\n"; $httpheader .= "Connection: Keep-Alive\r\n"; $opts = array( 'http'=>array( 'method'=>"POST", 'header'=>$httpheader, 'content'=>$post ) ); $context = stream_context_create($opts); $result = file_get_contents($url, 'r', $context); } return $result; } <br><font color="#e78608">------解决方案--------------------</font><br>

Article chaud

Outils chauds Tags

Article chaud

Tags d'article chaud

Bloc-notes++7.3.1
Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise
Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1
Puissant environnement de développement intégré PHP

Dreamweaver CS6
Outils de développement Web visuel

SublimeText3 version Mac
Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Sujets chauds

Comment réaliser la conversion mutuelle entre les requêtes CURL et python en python

Tutoriel sur la mise à jour de la version curl sous Linux !

Sortie de PHP8.1 : introduction de curl pour le traitement simultané de plusieurs requêtes

Du début à la fin : Comment utiliser l'extension php cURL pour effectuer des requêtes HTTP

Comment gérer la redirection 301 des pages web en PHP Curl ?

Comment définir des cookies dans php curl

Solution à l'erreur fatale PHP : appel à une fonction non définie curl_setopt()
