cURL returning null value in JSON with PHP

0

I'm studying cURL because I want to make an application with Twitter using login form and password ... yesterday I did this question was very well answered, based on this I am studying cURL , so I have the following code:

<?php
if (isset($_POST['ttrSignin'])) {
  $ttrUsername = trim(filter_input(INPUT_POST, 'ttrUsername'));
  $ttrPassword = trim(filter_input(INPUT_POST, 'ttrPassword'));
  $ttrTweet = trim(filter_input(INPUT_POST, 'ttrTweet'));

  if (empty($ttrUsername)) {
    $error[] = 'Insira seu nome de usuário do Twitter.';
  } elseif (empty($ttrPassword)) {
    $error[] = 'Insira sua senha do Twitter.';
  } elseif (empty($ttrTweet)) {
    $error[] = 'Insira seu Tweet.';
  } else {
    # The Twitter API Address
    $url = 'https://api.twitter.com/1.1/statuses/update.json';

    # Versão alternativa do JSON
    # $url = 'https://api.twitter.com/1.1/statuses/update.json'
    # Configure e execute o processo de curl
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, 'status=' . $ttrTweet);
    curl_setopt($ch, CURLOPT_USERPWD, $ttrUsername . ':' . $ttrPassword);

    $buffer = curl_exec($ch);
    curl_close($ch);

    //var_dump($buffer);

  }

  # Verifique o sucesso ou o fracasso
  if (empty($buffer)) {
    $error[] = 'Não foi possível conectar-se ao Twitter.'; 
  } else {
    $success[] = 'Tweet postado via Twitter API.';
  }
}
?>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>Follow</title>

    <link href="assets/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <link href="assets/css/main.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script><scriptsrc="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
    <section class="section">
      <div class="container">
        <div class="row">
          <div class="row">
              <div class="col-md-4 col-md-offset-4">
                <div class="panel panel-default">
                  <div class="panel-heading">
                    <i class="fa fa-sign-in"></i> Entrar para ganhar seguidores
                  </div>

                  <div class="panel-body">
                    <form method="post">
                      <div class="form-group">
                        <input type="text" name="ttrUsername" placeholder="Usuário do Twitter" value="<?php if (isset($error)) {echo $ttrUsername;} ?>" class="form-control">
                      </div>
                      <div class="form-group">
                        <input type="password" name="ttrPassword" placeholder="Senha do Twitter" class="form-control">
                      </div>

                      <div class="form-group">
                        <input type="text" name="ttrTweet" placeholder="Digite seu Tweet" value="<?php if (isset($error)) {echo $ttrTweet;} ?>" class="form-control">
                      </div>

                      <button type="submit" name="ttrSignin" class="btn btn-primary btn-block">
                        <i class="fa fa-twitter"></i> Entrar agora
                      </button>
                    </form>
                  </div>

                  <div class="panel-footer">
                    <div class="text-center">
                      <small>
                        Ao entrar você estará concordando com os <a href="#">Termos de Uso</a>.
                      </small>
                    </div>
                  </div>
                </div>
                <?php if (isset($error)): ?>
                    <?php foreach ($error as $e): ?>
                      <div class="alert alert-danger">
                        <i class="fa fa-warning"></i> <?php echo $e; ?>
                      </div>
                    <?php endforeach ?>
                  <?php endif ?>

                  <?php if (isset($success)): ?>
                    <?php foreach ($success as $s): ?>
                      <div class="alert alert-success">
                        <i class="fa fa-check"></i> <?php echo $s; ?>
                      </div>
                    <?php endforeach ?>
                  <?php endif ?>

                  <?php if (isset($ttrTweet)): ?>
                    <?php echo $ttrTweet; ?>
                  <?php endif ?>
              </div>
          </div>
        </div>
      </div>
    </section>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script><scriptsrc="assets/js/bootstrap.min.js"></script>
  </body>
</html>

When run it shows the message:

  

Unable to connect to Twitter.

By giving var_dump($buffer); , it returns the following:

  

C: \ wamp64 \ www \ twitterlogin \ index.php: 31: boolean false

What's wrong?

    
asked by anonymous 27.06.2017 / 20:23

1 answer

3

There are several possible issues:

  • This endpoint ( https://api.twitter.com/1.1/statuses/update.json ) expects you to send access_token , obtained using OAuth .

    Solution: Enter access_token .

  • If you have not set CABundle and are using PHP 7.1, cURL will check the issuer of the certificate with the authorities you trust, automatically. One of the principles of SSL is trust, if you trust the one who issued the certificate it will be safe, I will not go into details here .

    Solution:

  • Set the public keys of the issuers you trust, or a generic .
  • Define where it is stored:

    curl_setopt($ch, CURLOPT_CAINFO, 'C:\local\do\ca-bundle.pem');
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    
  • CURLOPT_USERPWD is for Basic Access Authentication , if you do not use CURLOPT_HTTPAUTH . Normally these data are sent in those alert boxes asking for login / password, by the browser, the relative Twitter does not use this, since it uses OAuth.

  • 27.06.2017 / 21:00