有没有一种方法可以不使用网络表单来发送 POST 数据?我正在使用第 3 方支付处理器,我可以选择手动提交支付,但数据需要采用 POST 格式。
我计划将我的脚本作为 CRON 作业运行,因为它是自动化的,所以没有用户通过 Web 表单提交输入。
提前致谢。
最佳答案
试试 CURL
http://php.net/manual/en/book.curl.php
//set POST variables
$url = 'http://domain.com/get-post.php';
$fields = array(
'lname' => urlencode($last_name),
'fname' => urlencode($first_name),
'title' => urlencode($title),
'company' => urlencode($institution),
'age' => urlencode($age),
'email' => urlencode($email),
'phone' => urlencode($phone)
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
关于没有 Web 表单的 PHP POST 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19454717/