PHP发送POST请求

{{ time }}

范例如下

<?php
/**
 * PHP发送Json对象数据
 *
 * @param $url 请求url
 * @param $jsonStr 发送的json字符串
 * @return array
 */
function http_post_json($url, $jsonStr)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/text/plain; charset=utf-8',
            'Content-Length: ' . strlen($jsonStr),
            'Accept-Encoding: gzip, deflate'
        )
    );
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
 
    return array($httpCode, $response);
}
 
$url = "http://www.163.com:5509/API";
$data1=["theta"=>0.6, "beta"=>0.92, "gamma"=>3, "age"=>23, "numearner"=>5, "numchild"=>3, "income"=>123, "muH"=>0.05, "sigma_occu"=>0.08, "sigma_sector"=>0.08, "Medexp"=>0.6, "mu_Y"=>0.04, "stock"=>1, "bond"=>123, "deposit"=>123, "househome"=>123, "mortgagehome"=>23, "houseINV"=>123, "mortgageINV"=>23, "Htransfer"=>123, "Hsupport"=>123, "ReverseM"=>1, "Cmean"=>50, "Cbar"=>12, "edufund"=>1, "edufundQ"=>123, "edufundyr"=>20];
$jsonStr = json_encode($data1);
list($returnCode, $returnContent) = http_post_json($url, $jsonStr);
echo $returnCode;

echo $returnContent;