hunt4car
hunt4car

Reputation: 93

$_POST via cURL not working

Im trying my hand at using curl to post some data, I am not reciving my post content and can not find the source of the problem

curl.php

<?php 

$data = array("user_email" => "22" , "pass" => "22" ); 
$string = http_build_query($data);


$ch = curl_init("http://localhost:8888/290_project/test.php"); //this is where post data goes too, also starts curl
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_exec($ch);
curl_close($ch) //ends curl
?>

test.php

<?php 
if(isset($_POST['user_email'], $_POST['pass'])) {

$name = $_POST['user_email'];
$pass = $_POST['pass'];

echo $name;
echo $pass;

} else {
echo "error";
} ?>

Every time I get my error response meaning the post data is not going through. I have tried everything I could think of to trouble shoot;I must be over looking something I am simply not yet familiar with?

Upvotes: 4

Views: 739

Answers (3)

Vinie
Vinie

Reputation: 2993

Following is working example. Please try this.

$postData = array(
        'user_name' => 'abcd',
        'password' => 'asdfghj',
        'redirect' => 'yes',
        'user_login' => '1'
    );
$url='http://localhost:8888/290_project/test.php';
$ch = curl_init();
//Set the URL to work with
curl_setopt($ch, CURLOPT_URL, $url);
// ENABLE HTTP POST
curl_setopt($ch, CURLOPT_POST, 1);
//Set the post parameters
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//execute the request
$store = curl_exec($ch);
print_r($store);
curl_close($ch)

Upvotes: 0

djkevino
djkevino

Reputation: 364

You have an error in your php code:

edit:

if(isset($_POST['user_email'], $_POST['pass'])) {

to:

if(isset($_POST['user_email']) && isset($_POST['pass'])) {

Upvotes: -1

Aruti
Aruti

Reputation: 73

Please set CURLOPT_URL to http://localhost:8888.....

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost:8888/290_project/test.php");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);

if(!curl_errno($ch)){ 
$info = curl_getinfo($ch); 
} else { 
echo  'Curl error: ' . curl_error($ch); 
} 
curl_close($ch) //ends curl
?>

With curl_getinfo() - Gets information about the last transfer. For more detail read below link:- http://php.net/manual/en/function.curl-getinfo.php

I have edited the answer, The reason for error is not curl.

http_build_query($data, '', '&');

Upvotes: 2

Related Questions