Leahn Novash
Leahn Novash

Reputation: 2941

PHP Curl sending an empty array

Ok, I've checked the other questions similar to this one, and none seem to point out to a solution to my problem. I have the following code:

<?PHP
$tamanho = $_REQUEST['tamanho'];

$post_data['item_quant'] = $_REQUEST['item_quant'];
$post_data['email_cobranca'] = $_REQUEST['email_cobranca'];
$post_data['tipo'] = $_REQUEST['tipo'];
$post_data['moeda'] = $_REQUEST['moeda'];
$post_data['item_id'] = $_REQUEST['item_id'].$tamanho;
$post_data['item_descr'] = $_REQUEST['item_descr'].'%20Tamanho%20'.$tamanho;
$post_data['item_valor'] = $_REQUEST['item_valor'];
$post_data['peso'] = $_REQUEST['peso'];
$post_data['encoding'] = $_REQUEST['encoding'];
$post_data['submit_01'] = $REQUEST['submit_01'];

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "[erased for security]");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
$result = curl_exec($curl);
curl_close($curl);

header('Location:[erased for security]');
?>

It is a barebone code to send a post request to another website, after I modified the product ID and description a little, to make them more unique (so I don't have to create a different product for each color and size of the clothes). However, this arrives at the other page as an empty $_POST array. Location headed is the same location the post request is being sent to.

No errors are being informed at the $result variable.

The output of curl_getinfo($curl) shows the array being sent perfectly.

Environment is PHP 5, and library being used is php_curl.dll.

Something is happening between page A sending the array and page B receiving the array.

Upvotes: 0

Views: 1725

Answers (3)

asad
asad

Reputation: 1

I think you should first make $post_data to query string and then send it. This will solve your problem.

Upvotes: 0

Wrikken
Wrikken

Reputation: 70460

However, this arrives at the other page as an empty $_POST array. Location headed is the same location the post request is being sent to.

Wait a minute? Are you trying to send someone to a page you just posted to? You cannot do that, the redirect of the user would result in the user / browser/ user-agent doing a GET request no matter what you are posting earlier. What do you see if you echo $result; instead of redirect? Does that page have the correct data? Then curl just plain works. If you want to get the user there with a post, you'll have to echo some sort of form, and perhaps perform some javascript magic to let it immediately submit to your target URL.

Upvotes: 1

MANCHUCK
MANCHUCK

Reputation: 2472

I had this issue once with CURL I did not dive in too deep to figure out why it happed (probably some issue with the CURL LIB used). I found a workaround by using http_build_query on my array

Upvotes: 1

Related Questions