php curl 模拟post提交

版权声明:本文为博主原创文章,未经博主允许不得(商用)转载。 https://blog.csdn.net/tang1jun/article/details/81512394

模拟post提交,用于测试

/**
 * 模拟post进行url请求
 * @param string $url
 * @param string $param
 */
function request_post($url = '', $param = '') 
{
    if (empty($url) || empty($param)) {
        return false;
    }

    $postUrl = $url;
    $curlPost = $param;
    $ch = curl_init();//初始化curl
    curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
    curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
    curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
    curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
    $data = curl_exec($ch);//运行curl
    curl_close($ch);

    return $data;
}


function testAction(){
    $url = 'http://localhost/cainiaoWechat/frontend/web/index.php?r=store/wxapp/home/order/send-package-do';
    $post_data['appid']       = '10';
    $post_data['appkey']      = 'cmbohpffXVR03nIpkkQXaAA1Vf5nO4nQ';
    $post_data['member_name'] = 'zsjs123';
    $post_data['password']    = '123456';
    $post_data['email']    = '[email protected]';
    $o = "";
    foreach ( $post_data as $k => $v ) 
    { 
        $o.= "$k=" . urlencode( $v ). "&" ;
    }
    $post_data = substr($o,0,-1);

    $res = request_post($url, $post_data);           

    print_r($res);
}

testAction();

猜你喜欢

转载自blog.csdn.net/tang1jun/article/details/81512394