3

I'm using Sendgrid API to send an email message over HTTP via PHP. This is my code:

<?php
$url = 'https://api.sendgrid.com/';
$user = 'USER';
$pass = 'PASSWORD';
$params = array(
    'api_user'  => $user,
    'api_key'   => $pass,
    'to'        => 'TARGET',
    'subject'   => 'Kami Menanti Anda',
    'from'      => 'noreply@kompetisiindonesia.com',
  );
  $params['html'] = 'html message';
  $params['text'] = $params['html'];
$request =  $url.'api/mail.send.json';
// Generate curl request
$session = curl_init($request);
// Tell curl to use HTTP POST
curl_setopt ($session, CURLOPT_POST, true);
// Tell curl that this is the body of the POST
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
// Tell curl not to return headers, but do return the response
curl_setopt($session, CURLOPT_HEADER, false);
// Tell PHP not to use SSLv3 (instead opting for TLS)
curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// obtain response
$response = curl_exec($session);
curl_close($session);
// print everything out
print_r($response);

But I get this error message:

Notice: Use of undefined constant CURL_SSLVERSION_TLSv1_2 - assumed 'CURL_SSLVERSION_TLSv1_2' in /opt/lampp/htdocs/oprek/sendgrid/sendviahttp.php on line 28

Does anybody know what's happening?

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
yussan
  • 2,277
  • 1
  • 20
  • 24
  • You don't have to upgrade PHP to set **CURLOPT_SSLVERSION** to **CURL_SSLVERSION_TLSv1_2** as @Matt Bernier stated. Instead of **CURL_SSLVERSION_TLSv1_2** use **6**. For more info [read this](http://stackoverflow.com/a/32926813/1057527). TLS 1.2 worked for me with PHP 5.2.17 and cURL 7.24.0 – machineaddict Mar 10 '16 at 14:55

4 Answers4

3

It looks like you might have an outdated build of CURL installed on your machine.

CURL_SSLVERSION_TLSv1_2 (integer) Available since PHP 5.5.19 and 5.6.3

http://php.net/manual/en/curl.constants.php

Matt Bernier
  • 94
  • 1
  • 3
2

Depending on your situation, you can try using the integer that represents the constant. In our case (CentOS6, with IUS PHP 5.3.23), the constant was not there, but the following worked just fine..

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://tlstest.paypal.com/");
curl_setopt($ch, CURLOPT_SSLVERSION, 6);
var_dump(curl_exec($ch));
1
if ( ! defined('CURL_SSLVERSION_TLSv1_2')) {
        define('CURL_SSLVERSION_TLSv1_2', 6);
    }

Add above code before

curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
Justin J
  • 808
  • 8
  • 14
0

Also, check the namespace your code is running in. If you have a TYPO3-command-controller, it`s code will run in \TYPO3 - namespace. Constants defined in the regular normal namespace have to be prepended with a backslash then, to be evaluated correctly.

Oliver
  • 105
  • 13