POST 数据到 PHP 中的 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3080146/
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 08:38:51  来源:igfitidea点击:

POST data to a URL in PHP

phppost

提问by Belgin Fish

How can I send POST data to a URL in PHP (without a form)?

如何将 POST 数据发送到 PHP 中的 URL(没有表单)?

I'm going to use it for sending a variable to complete and submit a form.

我将使用它来发送一个变量来完成和提交表单。

回答by Peter Anselmo

If you're looking to post data to a URL from PHP code itself (without using an html form) it can be done with curl. It will look like this:

如果您希望将数据从 PHP 代码本身发布到 URL(不使用 html 表单),则可以使用 curl 来完成。它看起来像这样:

$url = 'http://www.someurl.com';
$myvars = 'myvar1=' . $myvar1 . '&myvar2=' . $myvar2;

$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $myvars);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec( $ch );

This will send the post variables to the specified url, and what the page returns will be in $response.

这会将 post 变量发送到指定的 url,页面返回的内容将在 $response 中。

回答by Burak ?ztürk

cURL-less you can use in php5

您可以在 php5 中使用的无卷曲

$url = 'URL';
$data = array('field1' => 'value', 'field2' => 'value');
$options = array(
        'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data),
    )
);

$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);

回答by jfoucher

Your question is not particularly clear, but in case you want to send POST data to a url without using a form, you can use either fsockopen or curl.

您的问题不是特别清楚,但如果您想在不使用表单的情况下将 POST 数据发送到 url,您可以使用 fsockopen 或 curl。

Here's a pretty good walkthrough of both

是两者一个很好的演练