php curl:我需要一个简单的帖子请求和页面示例检索

时间:2010-03-13 22:15:31

标签: php curl

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

8 个答案:

答案 0 :(得分:62)

这样的事情:

$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; - )

答案 1 :(得分:4)

$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);

答案 2 :(得分:2)

在评论中尝试一个:http://php.net/manual/en/curl.examples-basic.php

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

或此示例:http://php.dzone.com/news/execute-http-post-using-php-cu

答案 3 :(得分:2)

您需要设置使用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);

答案 4 :(得分:1)

我认为你需要添加

curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $postFields);

答案 5 :(得分:1)

  <?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 :(得分:0)

在CURL中使用JSON数据

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'];
?>

答案 7 :(得分:-1)

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

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