美文网首页我爱编程
php.curl方式发送post报文

php.curl方式发送post报文

作者: 程序猿阿乐 | 来源:发表于2018-08-09 16:28 被阅读0次
/**
 * curl方式发送post报文
 * $remoteServer 请求地址
 * $postData post报文内容
 * $userAgent用户属性
 * return 返回报文
 */
private function _curlRequest($remoteServer, $postData)
{
    Log::write('发送post报文:址:'.$remoteServer , Log::INFO);
    Log::write('发送post报文:内容:'.json_encode($postData) , Log::INFO);

    $data_string = json_encode($postData);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, $remoteServer);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($data_string))
    );
    $result = urldecode(curl_exec($ch));
    curl_close($ch);

    return $data = empty($result) ? array() : json_decode($result, true);
}

相关文章

网友评论

    本文标题:php.curl方式发送post报文

    本文链接:https://www.haomeiwen.com/subject/nnrcbftx.html