Php help for post request and get post vars

Hello,

I have some problems with my php script.
I’d like to send a php post request to an external url (server 1 -> server 2 protocol -> server 1 respond)

But i can’t get the post vars on my server 2
Here is my code :

SEND POST :

public static function SendPostRequest_toMediaServer($url = "",$data = array()){
                if(!empty($url) AND !empty($data)){
                    $converted_data = http_build_query($data);
                    $options = array(
                        'http' =>
                            array(
                                'header'  => 'Content-type: application/x-www-form-urlencoded\r\n',
                                'method'  => 'POST',
                                'content' => $converted_data
                            )
                    );
                    $streamContext  = stream_context_create($options);
                    $result = file_get_contents($url, false, $streamContext);
                    exit($result
                    );
                    if(strpos($result, "true") !== false){
                        return true;
                    }else{
                        return false;
                    }
                }else{
                    return false;
                }
}

RECEIVE POST (debug to test script) :

 public function update_avatar(){
        var_dump($_POST);
        if(isset($_POST['exempleinput'])){
            exit("true");
        }else{
            exit("false");
        }
    }

you are exiting pre-maturely

The following updates works against my api tester,

$converted_data = http_build_query([
    'some' => 'thing',
    'is'=> 'here'
    ]);
$options = array(
    'http' =>
        array(
            'header'  => 'Content-type: application/x-www-form-urlencoded',
            'method'  => 'POST',
            'content' => $converted_data
            )
        );
$streamContext  = stream_context_create($options);
$result = file_get_contents($url, false, $streamContext);
print_r($result);

the

exit($result );

is just to get the var_dump($_POST) on update_avatar function, it’s just for my debug test.
If(isset($_POST[‘exempleinput’]) return me false in all case. So the 2nd server can’t get the $_POST vars.

Do you have anything that is activating this function?

Yup website is build in MVC, so GET request call this function

And here my function to call SendPostRequest_toMediaServer()

public function req_0x01(){
        if(AccountModel::CheckSecureAuth()){
            $account_info = AccountModel::GetAccountBasicInfo($_SESSION['user_logged']);
            if(isset($_POST['uploadprofilepicture'])){
                $post = $_POST;
                $post['account_public_key'] = $account_info->pcw_accounts_public_key;
                $post['account_private_key'] = $account_info->pcw_accounts_private_key;
                if(RequestModel::SendPostRequest_toMediaServer("http://server2url/upload/update_avatar",$post)){
                    AppModel::PopupNotif_setup("success","Profil picture updated","");
                    AppModel::redirect("/account/edit");
                }else{
                    AppModel::PopupNotif_setup("error","Protocol Error","An internal error as been detected, please contact an administrator");
                    AppModel::redirect("/account/edit");
                }
            }else{
                AppModel::PopupNotif_setup("error","Client error","No submition detected, please use the form");
                AppModel::redirect("/profile/".$account_info->profile_redirect);
            }
        }else{
            AppModel::PopupNotif_setup("error","Login Error","You must be logged");
            AppModel::redirect("/account/login&redirect=/account/profile_redirect");
        }
    }

OK, update :
I just forget i fu***** “s” in the request URL !

Thanks for your help :slight_smile:

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service