【问题标题】:php curl: I need a simple post request and retrival of page examplephp curl:我需要一个简单的发布请求和页面检索示例
【发布时间】:2011-01-27 06:28:13
【问题描述】:

我想知道如何在 curl 中发送 post 请求并获取响应页面。

【问题讨论】:

    标签: php curl


    【解决方案1】:

    这样的事情怎么样:

    $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
    


    curl可以使用的选项列表,可以看curl_setopt的页面。

    在这里,您至少必须使用:

    • CURLOPT_POST : 因为你想发送一个 POST 请求,而不是一个 GET
    • CURLOPT_RETURNTRANSFER :取决于您是希望 curl_exec 返回请求的结果,还是只输出它。
    • CURLOPT_POSTFIELDS : 将要发布的数据——可以直接写成字符串,比如查询字符串,或者使用数组


    不要犹豫,阅读 PHP 手册的curl section ;-)

    【讨论】:

      【解决方案2】:
      $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);
      

      【讨论】:

        【解决方案3】:

        您需要使用CURLOPT_POST 将请求设置为发布,如果您想使用它传递数据,请使用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);
        

        【讨论】:

        • CURLOPT_POST 不是传递的数据计数吗?你让它听起来像一个布尔值
        【解决方案4】:

        试试 cmets 中的那个:http://php.net/manual/en/curl.examples-basic.php

        (但添加 curl_setopt($ch, CURLOPT_POST, 1) 使其成为帖子而不是获取)

        或者这个例子:http://php.dzone.com/news/execute-http-post-using-php-cu

        【讨论】:

          【解决方案5】:
            <?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;
            ?>
          

          【讨论】:

            【解决方案6】:

            我认为你需要添加

            curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $postFields);
            

            【讨论】:

              【解决方案7】:

              将 JSON DATA 与 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'];
              ?>
              

              【讨论】:

                【解决方案8】:

                在这篇文章中查看我的代码

                https://stackoverflow.com/a/56027033/6733212

                <?php
                if (!function_exists('curl_version')) {
                    exit("Enable cURL in PHP");
                }
                
                $url = "https://www.google.com/";
                
                $curl = curl_init();
                
                curl_setopt_array($curl, array(
                    CURLOPT_URL => $url,
                    CURLOPT_RETURNTRANSFER => true,
                    CURLOPT_ENCODING => "",
                    CURLOPT_MAXREDIRS => 10,
                    CURLOPT_TIMEOUT => 30,
                    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                    CURLOPT_CUSTOMREQUEST => "GET",
                    CURLOPT_POSTFIELDS => "",
                    CURLOPT_HTTPHEADER => array(
                        "Accept: */*",
                        "Cache-Control: no-cache",
                        "Connection: keep-alive",
                        "Host: " . url($url),
                        "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36",
                        "accept-encoding: gzip, deflate",
                        "cache-control: no-cache",
                    ),
                ));
                
                function url($url)
                {
                    $result = parse_url($url);
                    return $result['host'];
                }
                $response = curl_exec($curl);
                $err = curl_error($curl);
                
                curl_close($curl);
                
                if ($err) {
                    echo "cURL Error #:" . $err;
                } else {
                    echo "<textarea>" . $response . "</textarea>";
                
                }
                

                http://codepad.org/YE6fyzCA

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2015-12-06
                  • 2022-11-29
                  • 1970-01-01
                  • 2011-05-21
                  • 1970-01-01
                  • 2011-09-03
                  • 2023-03-25
                  • 2015-06-24
                  相关资源
                  最近更新 更多