M
M
Max2015-09-24 17:15:53
PHP
Max, 2015-09-24 17:15:53

Redirect in php subject to incoming variable from API server?

Hello.
One hosting has its own api - I screwed it up and everything seems to be ok but (!)
After entering data into the form and pressing submit, everything seems to be ok. Responses from the API server come normally (either error or success).
Here is the html form:

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>form</title>
  <link rel="stylesheet" href="css/style.css" media="screen" type="text/css" />
</head>

<body>

  <div id="login">
  <div class="flip">
    <div class="form-signup">
      <h1>Регистрация email</h1>
      <fieldset>
      <p class="login-msg"></p>
        <form name="mail" method="post" action="mail.php" >
        <center>  
    <input id="email" type="text" name="email" required placeholder="Введите email" pattern="^[a-zA-Z0-9]+$" title="Только цифры и латинские буквы в верхнем и нижнем регистре"}/>
    
  <select id="domains">
    <option>@system-neo.com</option>
    <option>@clicin.de</option>
     </select><br>
   <input type="hidden" id="mailbox" name="mailbox" />
   <script type="text/javascript">
email.addEventListener('input', joinValues, false);
domains.addEventListener('input', joinValues, false);

function joinValues(){
    mailbox.value = email.value + domains.value;
}
</script>
          <input type="password" name="password" placeholder="Ваш сложный пароль..." pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}"  title="Не менее восьми символов, содержащих хотя бы одну цифру и символы из верхнего и нижнего регистра"  required />
          <input type="submit" name="vsubmit" value="Далее" /></center>
      </form>
   
    </div>


  </div>
</div>

</body>
</html>

<?php
$fields = array(
    'auth_login' => "q",
    'auth_token' => "c",
   
    'class' => "hosting_mailbox",
    'method' => "create",
   
    'account' => "c", 
    'mailbox' => $_POST['mailbox'],
    'password' => $_POST['password'],
);

$fields_string = ""; 
foreach($fields as $key => $value) {
     $fields_string .= $key.'='.urlencode($value).'&';
}

rtrim($fields_string, '&');
 
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://asd/api.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch);
$response = curl_exec($ch);
curl_close($ch); 
$json_string = 'https://asd/api.php';
$jsondata = file_get_contents($json_string);
$response = json_decode($response,true);
echo "<pre>";
print_r($response);

?>

What it looks like:
617c9eef4aa8.pngbede0f290ad3.png
I.e. redirects me from 192.168.0.117/new to 192.168.0.117/new/mail.php
Now I want to saddle so that when I click "submit" the value is checked print_r($response);now it is displayed on the screen in the browser (see IMG), and I need to check it using php and on condition of success , for example, redirect to /new/skype.html, and with [b]error[/b] clear the fields in the form and display popup on the form page with an error from print_r($response);
I tried to do this:
if ($response == "хз как опредялять success в $response") {$redirect_url = "/new/skype.html";}
else {$redirect_url = "/new";}

 
header('HTTP/1.1 200 OK');
header('Location: http://'.$_SERVER['HTTP_HOST'].$redirect_url);
exit();

I thought about parsing the page... but in my opinion it's f*cking bad(
I don't know how to get out of the situation. Maybe someone will tell you something :)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
danforth, 2015-09-24
@WarStyle

Check like this:

if ($response['status'] === 'error') {
// если API вернул статус 'error'
$redirect_url = "/new/skype.html";
} else {
// в противном случае
$redirect_url = "/new";
}

For the future, always try to anticipate all events. I often use the identical equals.

M
Maxim Grechushnikov, 2015-09-24
@maxyc_webber

in response you have a stupid array. its values ​​by key and check.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question