CURL +传递$ _POST变量/数组 [英] CURL + Passing $_POST variables/array
问题描述
我有以下情况:
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"中返回一个值.
I can the "http://www.domain.com/scripts/curl/index_general.php" to return a value.
我需要知道完成这条线的最佳方法: curl_setopt($ ch,CURLOPT_POSTFIELDS,尸体在这里");
I need to know the best way to complete the line: curl_setopt($ch, CURLOPT_POSTFIELDS, "body goes here" );
我应该如何发送用户名/密码. 我还有其他时间需要发送更多数据,因此我认为(从我的阅读中,数组是最好的)...
How should I send the username/passwd. I will have other time when I need to send more data so I assume (and from my reading that an array is best)...
当前,我的数据是从JQUERY/HTML传递过来的...那么我该如何将这些数据转换为数组并将CURL转换为目标域呢?
Currently my data is passed from JQUERY/HTML... how should I then convert this data into an array and the CURL it to the destination domain?
thx
推荐答案
在我看来,将用户名和密码作为带有CURL的HTTP HEADER AUTHENTICATION传递的最佳方法(安全).
In my opinion the best way (secure) method for passing username and password as 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中
In index_general.php
您可以通过以下方式访问http用户名和密码
you can access the http username and password as
$_SERVER['PHP_AUTH_USER'] and $_SERVER['PHP_AUTH_PW']
这篇关于CURL +传递$ _POST变量/数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!