php curl:我需要一个简单的发布请求并检索页面示例

2021/01/30 17:21 · php ·  · 0评论

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

那么这样的事情呢:

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

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

尝试在评论中的一个: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

您需要将请求设置为使用来发布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);

我认为您需要添加

curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $postFields);
  <?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;
  ?>

在CURL中使用JSON DATA

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

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

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

本文地址:http://php.askforanswer.com/php-curlwoxuyaoyigejiandandefabuqingqiubingjiansuoyemianshili.html
文章标签: ,  
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!

文件下载

老薛主机终身7折优惠码boke112

上一篇:
下一篇:

评论已关闭!