PHP + curl, HTTP POST sample code?

前端 未结 11 2343
北荒
北荒 2020-11-21 04:58

Can anyone show me how to do a php curl with an HTTP POST?

I want to send data like this:

username=user1, password=passuser1, gender=1
11条回答
  •  情歌与酒
    2020-11-21 05:38

    A live example of using php curl_exec to do an HTTP post:

    Put this in a file called foobar.php:

    $skipper, 'bestpony'=>'rainbowdash');
      $postvars = '';
      foreach($fields as $key=>$value) {
        $postvars .= $key . "=" . $value . "&";
      }
      $url = "http://www.google.com";
      curl_setopt($ch,CURLOPT_URL,$url);
      curl_setopt($ch,CURLOPT_POST, 1);                //0 for a get request
      curl_setopt($ch,CURLOPT_POSTFIELDS,$postvars);
      curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch,CURLOPT_CONNECTTIMEOUT ,3);
      curl_setopt($ch,CURLOPT_TIMEOUT, 20);
      $response = curl_exec($ch);
      print "curl response is:" . $response;
      curl_close ($ch);
    ?>
    

    Then run it with the command php foobar.php, it dumps this kind of output to screen:

    
    
    
    
    Title
    
    
    
    
      A mountain of content...
    
    
    

    So you did a PHP POST to www.google.com and sent it some data.

    Had the server been programmed to read in the post variables, it could decide to do something different based upon that.

提交回复
热议问题