PHP 构造 模拟 http 请求 https_request 支持GET和POST

 

 

function https_request($url, $data = null)
{
    $curl = curl_init();//初始化
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_TIMEOUT, 30);//允许 cURL 函数执行的最长秒数。
    /*if (!empty($port)) {
        curl_setopt($curl, CURLOPT_PORT, $port);//可选的用来指定连接端口,默认80端口可不写
    }*/
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json;charset=UTF-8'));//设置请求目标url头部信息
    if (!empty($data)) {
        //$data不为空,发送post请求
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data); //$data:数组
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);//执行命令
    $error = curl_error($curl);//错误信息
    if ($error || $output == FALSE) {
        //报错信息
        return 'ERROR ' . curl_error($curl);
    }
    curl_close($curl);
    return $output;
}

 

 

下面是在php中输出html后用js提交表单

echo "<html>";
echo "<head>";
echo "<title>Loading...</title>";
echo "<meta http-equiv=\"content-Type\" content=\"text/html; charset=UTF-8\" />";
echo "</head>";
echo "<body>";
echo "<form action=\"{$post_url}\" method=\"post\" id=\"frm3\">";
echo "<input type=\"hidden\" name=\"pMerCode\" value=\"{$pMerCode}\">";
echo "<input type=\"hidden\" name=\"p3DesXmlPara\" value=\"{$p3DesXmlPara}\">";
echo "<input type=\"hidden\" name=\"pSign\" value=\"{$pSign}\">";
echo "</form>";
echo "<script language=\"javascript\">";
echo "document.getElementById(\"frm3\").submit();";      
echo "</script>";
echo "</body>";
echo "</html>";

 

posted @ 2016-06-27 14:35  WilliamShaw  阅读(1915)  评论(0编辑  收藏  举报