有人能告诉我如何用HTTP POST做一个PHP cURL吗?
我想这样发送数据:
username=user1, password=passuser1, gender=1
到www.example.com
我希望cURL返回result=OK这样的响应。有什么例子吗?
有人能告诉我如何用HTTP POST做一个PHP cURL吗?
我想这样发送数据:
username=user1, password=passuser1, gender=1
到www.example.com
我希望cURL返回result=OK这样的响应。有什么例子吗?
当前回答
如果你试图用cookies登录网站。
这段代码:
if ($server_output == "OK") { ... } else { ... }
如果您尝试登录,它可能无法工作,因为许多网站返回状态200,但发布不成功。
检查登录帖子是否成功的简单方法是检查它是否再次设置cookie。如果在输出中有一个Set-Cookies字符串,这意味着帖子没有成功,它开始一个新的会话。
同样,帖子可以成功,但是状态可以重定向,而不是200。
为了确保帖子成功,试试下面的方法:
Follow location在post之后,所以它会转到post重定向到的页面:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
然后检查请求中是否存在新的cookie:
if (!preg_match('/^Set-Cookie:\s*([^;]*)/mi', $server_output))
{echo 'post successful'; }
else { echo 'not successful'; }
其他回答
一个使用php curl_exec来做一个HTTP post的实例:
把它放在一个名为foobar.php的文件中:
<?php
$ch = curl_init();
$skipper = "luxury assault recreational vehicle";
$fields = array( 'penguins'=>$skipper, 'bestpony'=>'rainbowdash');
$postvars = '';
foreach($fields as $key=>$value) {
$postvars .= $key . "=" . $value . "&";
}
$url = "http://www.google.com";
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST, 1); //0 for a get request
curl_setopt($ch,CURLOPT_POSTFIELDS,$postvars);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT ,3);
curl_setopt($ch,CURLOPT_TIMEOUT, 20);
$response = curl_exec($ch);
print "curl response is:" . $response;
curl_close ($ch);
?>
然后用php foobar.php命令运行它,它会将这样的输出转储到屏幕上:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Title</title>
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="0">
<body>
A mountain of content...
</body>
</html>
因此,您执行了一个PHP POST到www.google.com,并向其发送了一些数据。
如果服务器被编程为读入post变量,它可以基于此决定做一些不同的事情。
我很惊讶没有人建议file_get_contents:
$url = "http://www.example.com";
$parameters = array('username' => 'user1', 'password' => 'passuser1', 'gender' => '1');
$options = array('http' => array(
'header' => 'Content-Type: application/x-www-form-urlencoded\r\n',
'method' => 'POST',
'content' => http_build_query($parameters)
));
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
这很简单,很有效;我把它用在两端都能控制代码的环境中。
更好的方法是使用json_decode(并设置代码以返回JSON)
$result = json_decode(file_get_contents($url, false, $context), TRUE);
这种方法在幕后调用curl,但不需要经历那么多困难。
从Stack Overflow上其他地方的原始答案提炼出来的答案: PHP将变量发送到file_get_contents()
<?php
//
// A very simple PHP example that sends a HTTP POST to a remote site
//
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.example.com/tester.phtml");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"postvar1=value1&postvar2=value2&postvar3=value3");
// In real life you should use something like:
// curl_setopt($ch, CURLOPT_POSTFIELDS,
// http_build_query(array('postvar1' => 'value1')));
// Receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close($ch);
// Further processing ...
if ($server_output == "OK") { ... } else { ... }
?>
Curl Post +错误处理+设置头[感谢@mantas-d]:
function curlPost($url, $data=NULL, $headers = NULL) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if(!empty($data)){
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
if (!empty($headers)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
$response = curl_exec($ch);
if (curl_error($ch)) {
trigger_error('Curl Error:' . curl_error($ch));
}
curl_close($ch);
return $response;
}
curlPost('google.com', [
'username' => 'admin',
'password' => '12345',
]);
下面是PHP + curl的一些样板代码 http://www.webbotsspidersscreenscrapers.com/DSP_download.php
包含在这些库中将简化开发
<?php
# Initialization
include("LIB_http.php");
include("LIB_parse.php");
$product_array=array();
$product_count=0;
# Download the target (store) web page
$target = "http://www.tellmewhenitchanges.com/buyair";
$web_page = http_get($target, "");
...
?>