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

前端 未结 8 1036
走了就别回头了
走了就别回头了 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:54
      <?php
      ob_start();
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL,'https://example.com/student_list.php');
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $response = curl_exec($ch);
      echo $response;
      ?>
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2020-11-27 17:59

    I think you need to add

    curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $postFields);
    
    0 讨论(0)
  • 2020-11-27 18:03

    What about something like this :

    $ch = curl_init();
    $curlConfig = array(
        CURLOPT_URL            => "http://www.example.com/yourscript.php",
        CURLOPT_POST           => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POSTFIELDS     => array(
            'field1' => 'some date',
            'field2' => 'some other data',
        )
    );
    curl_setopt_array($ch, $curlConfig);
    $result = curl_exec($ch);
    curl_close($ch);
    
    // result sent by the remote server is in $result
    


    For a list of options that can be used with curl, you can take a look at the page of curl_setopt.

    Here, you'll have to use, at least :

    • CURLOPT_POST : as you want to send a POST request, and not a GET
    • CURLOPT_RETURNTRANSFER : depending on whether you want curl_exec to return the result of the request, or to just output it.
    • CURLOPT_POSTFIELDS : The data that will be posted -- can be written directly as a string, like a querystring, or using an array


    And don't hesitate to read the curl section of the PHP manual ;-)

    0 讨论(0)
  • 2020-11-27 18:09

    Using JSON DATA with CURL

    client.php

        <?php 
    
    $url="http://192.168.44.10/project/server/curl_server.php";
    $data=['username'=>'abc','password'=>'123'];
    $data = json_encode($data);
    
    $ch = curl_init();
    $curlConfig = array(
        CURLOPT_URL            => $url,
        CURLOPT_HTTPHEADER     => array('Content-Type: application/json'),
        CURLOPT_POST           => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POSTFIELDS     => $data
    );
    curl_setopt_array($ch, $curlConfig);
    $result = curl_exec($ch);
    curl_close($ch);
    echo $result;  //here you get result like: username: abc and password: 123
    ?>
    

    curl_server.php

        <?php
    
        $data = file_get_contents('php://input');
        $Data= json_decode($data,true);
    
        echo 'username: '.$Data['username']." and password: ".$Data['password'];
    ?>
    
    0 讨论(0)
  • 2020-11-27 18:14
    $url = "http://www.example.com/";
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    
    $data = array(
        'username' => 'foo',
        'password' => 'bar'
    );
    
    
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    $contents = curl_exec($ch);
    curl_close($ch);
    
    0 讨论(0)
提交回复
热议问题