gpt4 book ai didi

php - CURL + 传递 $_POST 变量/数组

转载 作者:行者123 更新时间:2023-12-01 03:51:00 24 4
gpt4 key购买 nike

我有以下情况:

if(isset($_POST['thisvalue'])) {

$username = $_POST['username'];
$passwd = $_POST['passwd'];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.domain.com/scripts/curl/index_general.php' );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_POST, true );
curl_setopt($ch, CURLOPT_REFERER, 'http://www.domain.com.au' );
curl_setopt($ch, CURLOPT_POSTFIELDS, "body goes here" );
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
$result = curl_exec($ch);
curl_close($ch);
echo $result;

我可以通过“http://www.domain.com/scripts/curl/index_general.php”返回一个值。

我需要知道完成该行的最佳方式:curl_setopt($ch, CURLOPT_POSTFIELDS, "正文放在这里");

我应该如何发送用户名/密码。当我需要发送更多数据时,我会有其他时间,所以我假设(并且从我的阅读来看,数组是最好的)...

目前我的数据是从 JQUERY/HTML 传递的...然后我应该如何将此数据转换为数组并将其 CURL 到目标域?

谢谢

最佳答案

在我看来,将用户名和密码传递为 HTTP HEADER AUTHENTICATION WITH CURL 的最佳方式(安全)方法。

if(isset($_POST['thisvalue'])) {

$username = $_POST['username'];
$passwd = $_POST['passwd'];

$data = array('serialno' => '12345');

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.domain.com/scripts/curl/index_general.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_POST, true );
curl_setopt($ch, CURLOPT_USERPWD,'username:password'); // set your username and password
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_REFERER,'http://www.domain.com.au');
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
$result = curl_exec($ch);
curl_close($ch);
echo $result;
}

在index_general.php中

您可以访问 http 用户名和密码

$_SERVER['PHP_AUTH_USER'] and $_SERVER['PHP_AUTH_PW']

关于php - CURL + 传递 $_POST 变量/数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8572535/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com