且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

POST 数据到 PHP 中的 URL

更新时间:2023-02-26 13:05:47

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

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:

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

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

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