Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collecting and using data returned from a http Curl (php) Post

Tags:

php

curl

I need assistance with using the data returned after a http post. Below is my current post code that posts the data to a specific website

<?php
//extract data from the post
extract($_POST);

//set POST variables
$url = 'https://test.com';
$fields = array(
                        'surname' => urlencode($surname),
                        'first_name' => urlencode($first_name),
                        'dob' => urlencode($dob),
                        'email' => urlencode($email),
                        'home_phone' => urlencode($home_phone),
                        'mobile_phone' => urlencode($mobile_phone),
                        'work_phone' => urlencode($work_phone),
                        'postcode' => urlencode($postcode),
                        'leadid' => urlencode(123),
                        'affid' => urlencode(123),
                        'subid' => urlencode(123),
                        'bank_acno' => urlencode($bank_acno),
                        'bank_sort' => urlencode($bank_sort),
                        'amount_required' => urlencode($amount_required),

                );

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

?>

I get either one of 2 response's - {result:true} or {result:false}

I need to know is how to do the following :

When I get a {result:true} I want to redirect to url "http://thisurl.com" When I get a {result:false} I want to return a response "Denied"

Please note the response is not json , it is just returned in that format.

like image 277
MatHatrik Avatar asked Dec 06 '25 09:12

MatHatrik


1 Answers

You need to check what $result variable contains and then make the redirect or show denied message according to situation:

<?php
//extract data from the post
extract($_POST);

//set POST variables
$url = 'https://test.com';
$fields = array(
                        'surname' => urlencode($surname),
                        'first_name' => urlencode($first_name),
                        'dob' => urlencode($dob),
                        'email' => urlencode($email),
                        'home_phone' => urlencode($home_phone),
                        'mobile_phone' => urlencode($mobile_phone),
                        'work_phone' => urlencode($work_phone),
                        'postcode' => urlencode($postcode),
                        'leadid' => urlencode(123),
                        'affid' => urlencode(123),
                        'subid' => urlencode(123),
                        'bank_acno' => urlencode($bank_acno),
                        'bank_sort' => urlencode($bank_sort),
                        'amount_required' => urlencode($amount_required),

                );

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

$result_true   = 'true';
$check_true = strpos($result, $result_true);

if ($check_true !== false) {
     header('Location: http://url.com');
     die;
}

$result_false   = 'false';
$check_false = strpos($result, $result_false);

if ($check_false !== false) {
     echo "Denied";
}

And in your code you've used:

curl_setopt($ch,CURLOPT_POST, count($fields));

But in php.net manual:

CURLOPT_POST: TRUE to do a regular HTTP POST. This POST is the normal application/x-www-form-urlencoded kind, most commonly used by HTML forms.

So counting fields is not the proper way to do this it could be TRUE or FALSE not the count. Of course if $fields array is empty result will be 0 which is equal to FALSE or if it's 1 then it's 1 (TRUE) but when the result of the count is greater than 1 that's makes no sense.

like image 167
mirza Avatar answered Dec 09 '25 11:12

mirza