Answers:
<?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 { ... }
?>
CURLOPT_POSTFIELDS
数组,则Content-Type
标头将设置为multipart/form-data
而不是application/x-www-form-urlencoded
。php.net/manual/en/function.curl-setopt.php
true
代替1
for CURLOPT_POST
。
// set post fields
$post = [
'username' => 'user1',
'password' => 'passuser1',
'gender' => 1,
];
$ch = curl_init('http://www.example.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
// execute!
$response = curl_exec($ch);
// close the connection, release resources used
curl_close($ch);
// do anything you want with your response
var_dump($response);
<?php
// mutatis mutandis
namespace MyApp\Http;
class CurlPost
{
private $url;
private $options;
/**
* @param string $url Request URL
* @param array $options cURL options
*/
public function __construct($url, array $options = [])
{
$this->url = $url;
$this->options = $options;
}
/**
* Get the response
* @return string
* @throws \RuntimeException On cURL error
*/
public function __invoke(array $post)
{
$ch = curl_init($this->url);
foreach ($this->options as $key => $val) {
curl_setopt($ch, $key, $val);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
$error = curl_error($ch);
$errno = curl_errno($ch);
if (is_resource($ch)) {
curl_close($ch);
}
if (0 !== $errno) {
throw new \RuntimeException($error, $errno);
}
return $response;
}
}
// create curl object
$curl = new \MyApp\Http\CurlPost('http://www.example.com');
try {
// execute the request
echo $curl([
'username' => 'user1',
'password' => 'passuser1',
'gender' => 1,
]);
} catch (\RuntimeException $ex) {
// catch errors
die(sprintf('Http error %s with code %d', $ex->getMessage(), $ex->getCode()));
}
这里的边注:最好创建一个AdapterInterface
例如使用getResponse()
method 调用的接口,并让上面的类实现它。然后,您始终可以将这种实现与您喜欢的另一个适配器交换,而对您的应用程序没有任何副作用。
通常,在Windows操作系统下,PHP中的cURL存在问题。尝试连接到受https保护的端点时,您会收到一条错误消息,告诉您certificate verify failed
。
大多数人在这里所做的是告诉cURL库简单地忽略证书错误并继续(curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
)。由于这将使您的代码正常工作,因此会引入巨大的安全漏洞,并使恶意用户能够对您的应用执行各种攻击,例如“中间人攻击”等。
永远不要那样做。相反,您只需要修改您php.ini
的CA Certificate
文件并告诉PHP您的文件在哪里,即可使其正确验证证书:
; modify the absolute path to the cacert.pem file
curl.cainfo=c:\php\cacert.pem
cacert.pem
可以从Internet下载最新版本,也可以从喜欢的浏览器中提取最新版本。更改任何php.ini
相关设置时,请记住重新启动Web服务器。
将其放在一个名为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>
因此,您对www.google.com进行了PHP POST,并向其发送了一些数据。
如果对服务器进行了编程以读取post变量,则可以基于此决定执行其他操作。
$postvars .= $key . $value;
应该$postvars .= $key . $value ."&";
还是不应该?
$fields
数组,它将输出查询字符串。
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
http_build_query
:curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
可以很容易地达到它:
<?php
$post = [
'username' => 'user1',
'password' => 'passuser1',
'gender' => 1,
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.domain.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
$response = curl_exec($ch);
var_export($response);
卷曲发布+错误处理+设置标题[感谢@ 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',
]);
curlPost('google.com', [
'username' => 'admin',
'password' => '12345',
]);
function curlPost($url, $data) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if ($error !== '') {
throw new \Exception($error);
}
return $response;
}
curl_close
在一个finally
块内。
如果表单使用重定向,身份验证,Cookie,SSL(https)或除需要POST变量的完全打开的脚本以外的其他任何内容,您将非常迅速地开始努力。看一下Snoopy,它确实满足您的想法,同时消除了设置大量开销的需求。
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
如果要将信息传递到自己的网站,一个更简单的答案是使用SESSION变量。以以下内容开始php页面:
session_start();
如果某些时候您想在PHP中生成信息并传递到会话的下一页,而不是使用POST变量,则将其分配给SESSION变量。例:
$_SESSION['message']='www.'.$_GET['school'].'.edu was not found. Please try again.'
然后在下一页上,您只需引用此SESSION变量。注意:使用后,请确保将其销毁,因此使用后它不会持续存在:
if (isset($_SESSION['message'])) {echo $_SESSION['message']; unset($_SESSION['message']);}
这是一些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, "");
...
?>
如果您尝试使用Cookie在网站上登录。
这段代码:
if ($server_output == "OK") { ... } else { ... }
如果您尝试登录,则可能无法工作,因为许多站点返回状态200,但是发布不成功。
检查登录是否成功的简单方法是检查是否再次设置cookie。如果在输出中具有Set-Cookies字符串,则意味着发布不成功,并且开始新的会话。
同样,发布可以成功,但状态可以重定向为200。
为确保发布成功,请尝试以下操作:
在帖子后关注位置,因此它将转到帖子重定向到的页面:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
然后检查请求中是否存在新的cookie:
if (!preg_match('/^Set-Cookie:\s*([^;]*)/mi', $server_output))
{echo 'post successful'; }
else { echo 'not successful'; }
$curlHandler = curl_init();
curl_setopt_array($curlHandler, [
CURLOPT_URL => 'https://postman-echo.com/post',
CURLOPT_RETURNTRANSFER => true,
/**
* Specify POST method
*/
CURLOPT_POST => true,
/**
* Specify array of form fields
*/
CURLOPT_POSTFIELDS => [
'foo' => 'bar',
'baz' => 'biz',
],
]);
$response = curl_exec($curlHandler);
curl_close($curlHandler);
echo($response);
http_build_query()
来处理参数;只需将数组传递给CURLOPT_POSTFIELDS
就足够了。