php curl: I need a simple post request and retrival of page example

前端 未结 8 1085
走了就别回头了
走了就别回头了 2020-11-27 17:18

I would like to know how to send a post request in curl and get the response page.

8条回答
  •  野性不改
    2020-11-27 17:58

    You need to set the request to post using CURLOPT_POST and if you want to pass data with it, use CURLOPT_POSTFIELDS:

    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    
    $data = array(
        'username' => 'foo',
        'password' => 'bar'
    );
    
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    
    $contents = curl_exec($ch);
    
    curl_close($ch);
    

提交回复
热议问题