如何在PHP中将POST数据发送到URL (没有表单)?
我将使用它来发送一个变量来完成并提交一个表单。
发布于 2010-06-21 01:32:50
如果您希望从PHP代码本身(不使用html表单)将数据发布到URL,则可以使用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 );
这将把post变量发送到指定的url,页面返回的内容将是$response格式的。
发布于 2014-04-30 18:10:27
可以在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);
发布于 2010-06-21 01:29:15
你的问题不是特别清楚,但是如果你想在不使用表单的情况下将POST数据发送到url,你可以使用fsockopen或curl。
https://stackoverflow.com/questions/3080146
复制相似问题