Доступ к переменным CURLOPT_POSTFIELD в удаленном скрипте? - PullRequest
0 голосов
/ 09 августа 2011

Как мне извлечь содержимое переменной POSTFEILD в удаленном скрипте (target_url.php), чтобы использовать его в том же удаленном скрипте?

Я делаю что-то вроде этого ниже. Не возвращает ошибок.

//Have tried multiple ways to setup the POSTFEILD argument, such as:

$data = array('var'=>'varcontents');
$post_arg = http_build_query($data) . "\n";

//and

$post_arg = 'var ='  . urlencode($varcontents);


//create cURL connection
$ch = curl_init('http://www.remotedomain.com/target_url.php');

//set options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

//set data to be posted
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_arg);

//perform the request
$result = curl_exec($ch);

//show information regarding the request
print_r(curl_getinfo($ch));
echo curl_errno($ch) . '-' . curl_error($ch);

Ответы [ 3 ]

2 голосов
/ 21 мая 2013

Только что столкнулся с этой же проблемой, и вот решение, которое я нашел, подумал, что поделюсь им с вами:

Используйте

$varcontent = file_get_contents("php://input");

в вашем target_url.php, что должно сделатьтрюк

0 голосов
/ 09 августа 2011

Ваша настройка скручивания кажется не совсем правильной. Попробуйте это:

$post_data = http_build_query(array('var'=>'varcontents'));
$url = 'http://www.remotedomain.com/target_url.php';

$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$buffer = curl_exec($ch);
curl_close($ch);

И в вашем target_url.php, als уже сказал:

$varcontent = $_POST['var'];
0 голосов
/ 09 августа 2011

с использованием

<?php
$post_arg = http_build_query( array('var'=>'varcontents') );
//create cURL connection
$ch = curl_init('http://localhost/target_url.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_arg);
$result = curl_exec($ch);
var_dump($result);

как «клиент» и

<?php
var_dump($_POST);

как target_url.php, вывод

<code>string(186) "<pre class='xdebug-var-dump' dir='ltr'>
<b>array</b>
  'var' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'varcontents'</font> <i>(length=11)</i>
»

(напоминает мне отключить эту опцию xdebug ;-)), поэтому в target_url.php параметры доступны через $ _POST.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...