技术教程
当前位置:首页 > 技术教程

php模拟post提交数据

( 更新:10-02   加入收藏)
第一种:file_get_contents来模拟post

function file_get_contents_post($url, $post){
    $options = array(
             'http'=> array(
             'method'=>'POST',
             'content'=> http_build_query($post),
              ),
     );
    $result = file_get_contents($url,false, stream_context_create($options));
    return $result;
 }

$data = file_get_contents_post("http://xxx.xxx.xx/post.php", 
array('name'=>'caiknife','email'=>'[email protected]'));
var_dump($data);


第二种curl模拟post

function curl_post($url, $post){
    $options = array(
        CURLOPT_RETURNTRANSFER =>true,
        CURLOPT_HEADER =>false,
        CURLOPT_POST =>true,
        CURLOPT_POSTFIELDS => $post,
    );
    $ch = curl_init($url);
    curl_setopt_array($ch, $options);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

$data = curl_post("http://www.a.com/post/post.php", 
array('name'=>'caiknife','email'=>'caiknife#gmail.com'));
var_dump($data);

相关阅读