我正在尝试使用sendgrid网站上发布的相同代码来查询sendgrid API,该代码使用PHP的cURL函数。
我使用的代码是:
<?php
$url = 'https://api.sendgrid.com/';
$user = 'USERNAME';
$pass = 'PASSWORD';
$params = array(
'api_user' => $user,
'api_key' => $pass,
'to' => 'example3@sendgrid.com',
'subject' => 'testing from curl',
'html' => 'testing body',
'text' => 'testing body',
'from' => 'example@sendgrid.com',
);
$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);
?>
但是每次我运行它的时候,我都会得到这样的错误:
注意:使用未定义的常量CURL_SSLVERSION_TLSv1_2 -假定为'CURL_SSLVERSION_TLSv1_2‘
我从字面上搜索了google,但没有找到任何解决方案。请帮帮忙。
谢谢!
发布于 2016-03-22 18:58:31
我已经成功地让上面的代码像这样工作了。(我还应该注意到,由于消息内容,它最终出现在我的垃圾邮件中!)
$url = 'https://api.sendgrid.com/';
$user = 'USERNAME';
$pass = 'PASSWORD';
$params = array(
'api_user' => $user,
'api_key' => $pass,
'to' => 'example3@sendgrid.com',
'subject' => 'testing from curl',
'html' => 'testing body',
'text' => 'testing body',
'from' => 'example@sendgrid.com',
);
$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);
//Turn off SSL
curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);//New line
curl_setopt($session, CURLOPT_SSL_VERIFYHOST, false);//New line
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// obtain response
$response = curl_exec($session);
// print everything out
var_dump($response,curl_error($session),curl_getinfo($session));
curl_close($session);
发布于 2015-04-24 04:13:31
您运行的PHP版本是什么?
来自 的
CURL_SSLVERSION_TLSv1_2 (整数)
自PHP 5.5.19和5.6.3起可用
您的服务器可能正在运行比此更低的版本,并且常量是未定义的。
https://stackoverflow.com/questions/29833174
复制相似问题