博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
php模拟POST请求提交数据
阅读量:4946 次
发布时间:2019-06-11

本文共 1415 字,大约阅读时间需要 4 分钟。

php模拟POST请求提交数据

1.基于fsockopen

function phppost00($jsonString){$URL='https://www.jy.com/phppostok.php';$post_data['clientname'] = $jsonString;$referrer="";$URL_Info=parse_url($URL);foreach($post_data as $key=>$value)$values[]="$key=".$value; $data_string=implode("&",$values);// Find out which port is needed - if not given use standard (=80)if(!isset($URL_Info["port"])) $URL_Info["port"]=80;// building POST-request:$request='';$request.="POST ".$URL_Info["path"]." HTTP/1.1\n";$request.="Host: ".$URL_Info["host"]."\n";//$request.="Referer: $referrer\n";$request.="Content-type: application/x-www-form-urlencoded\n";$request.="Content-length: ".strlen($data_string)."\n";$request.="Connection: close\n";$request.="\n";$request.=$data_string."\n";$fp = fsockopen($URL_Info["host"],$URL_Info["port"]);fputs($fp, $request);$result='';while(!feof($fp)) {$result .= fgets($fp, 128);}fclose($fp);}

 

2.基于curl_init

function phppost($jsonString){    $url='http://www.jy.com/phppostok.php';    $fields=$jsonString;    $ch=curl_init();    curl_setopt($ch,CURLOPT_POST, true);    curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);    curl_setopt($ch,CURLOPT_URL,$url);    curl_setopt($ch,CURLOPT_POST,count($fields));    curl_setopt($ch,CURLOPT_POSTFIELDS,$fields);    $response=curl_exec($ch);    curl_close($ch);    $result = json_decode($response,true);        return $result;}

 

 

转载于:https://www.cnblogs.com/keleyu/p/3364995.html

你可能感兴趣的文章