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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2440252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:28:48  来源:igfitidea点击:

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

phpcurl

提问by ufk

I would like to know how to send a post request in curl and get the response page.

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

回答by Pascal MARTIN

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.


有关可与 curl 一起使用的选项列表,您可以查看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_execto 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
  • CURLOPT_POST: 因为你想发送一个 POST 请求,而不是一个 GET
  • CURLOPT_RETURNTRANSFER: 取决于您是要curl_exec返回请求的结果,还是只是输出它。
  • CURLOPT_POSTFIELDS: 将要发布的数据——可以直接写成字符串,比如查询字符串,或者使用数组


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


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

回答by areeb

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

回答by user262976

try the one in the comments: http://php.net/manual/en/curl.examples-basic.php

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

(but add curl_setopt($ch, CURLOPT_POST, 1) to make it a post instead of get)

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

or this example: http://php.dzone.com/news/execute-http-post-using-php-cu

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

回答by Tatu Ulmanen

You need to set the request to post using CURLOPT_POSTand if you want to pass data with it, use CURLOPT_POSTFIELDS:

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

回答by Sinan

I think you need to add

我认为你需要添加

curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $postFields);

回答by Siddharth Shukla

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

回答by Varasney Atawadkar

Using JSON DATA with CURL

将 JSON 数据与 CURL 结合使用

client.php

客户端.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

curl_server.php

    <?php

    $data = file_get_contents('php://input');
    $Data= json_decode($data,true);

    echo 'username: '.$Data['username']." and password: ".$Data['password'];
?>

回答by Juan Denis

Check out my code in this post

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

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

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://codepad.org/YE6fyzCA