zoukankan      html  css  js  c++  java
  • php模拟POST请求提交数据

    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
    ";
    $request.="Host: ".$URL_Info["host"]."
    ";
    //$request.="Referer: $referrer
    ";
    $request.="Content-type: application/x-www-form-urlencoded
    ";
    $request.="Content-length: ".strlen($data_string)."
    ";
    $request.="Connection: close
    ";
    $request.="
    ";
    $request.=$data_string."
    ";
    $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;
    }
  • 相关阅读:
    tomcat内存溢出问题解决
    redis知识点汇总
    activiti全部知识点
    Python_Note_Preview_03_URL
    S&P_09_协方差(协方差矩阵)与相关系数
    Linear_algebra_06_ 内积空间
    Linear_algebra_05_线性方程组的解理论
    Linear_algebra_04_向量空间
    Linear_algebra_03_矩阵
    Linear_algebra_02_行列式
  • 原文地址:https://www.cnblogs.com/keleyu/p/3364995.html
Copyright © 2011-2022 走看看