php 没有网络表单的PHP POST数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19454717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP POST data without web form
提问by Alan A
Is there a way to send POST data without using a web form? I am working with a 3rd party payment processor and I have an option to manually submit payment but the data is required to be POST formatted.
有没有办法在不使用 Web 表单的情况下发送 POST 数据?我正在与第 3 方付款处理器合作,我可以选择手动提交付款,但数据需要采用 POST 格式。
I plan to run my script as CRON job and so as it is automated there is no user input via a web form submission.
我计划将我的脚本作为 CRON 作业运行,因此它是自动化的,因此没有用户通过 Web 表单提交进行输入。
Thank in advance.
预先感谢。
回答by VancleiP
try CURL
试试卷曲
http://php.net/manual/en/book.curl.php
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);
回答by Alix Axel
You can use the cURL extension, or even file_get_contents()
with a custom context.
您可以使用 cURL 扩展,甚至可以使用file_get_contents()
自定义上下文。