首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >php sendgrid -将PHP与cURL结合使用示例-不起作用

php sendgrid -将PHP与cURL结合使用示例-不起作用
EN

Stack Overflow用户
提问于 2015-04-24 04:00:03
回答 2查看 3.3K关注 0票数 1

我正在尝试使用sendgrid网站上发布的相同代码来查询sendgrid API,该代码使用PHP的cURL函数。

我使用的代码是:

代码语言:javascript
运行
复制
<?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,但没有找到任何解决方案。请帮帮忙。

谢谢!

EN

回答 2

Stack Overflow用户

发布于 2016-03-22 18:58:31

我已经成功地让上面的代码像这样工作了。(我还应该注意到,由于消息内容,它最终出现在我的垃圾邮件中!)

代码语言:javascript
运行
复制
$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);
票数 2
EN

Stack Overflow用户

发布于 2015-04-24 04:13:31

您运行的PHP版本是什么?

来自 的

CURL_SSLVERSION_TLSv1_2 (整数)

自PHP 5.5.19和5.6.3起可用

您的服务器可能正在运行比此更低的版本,并且常量是未定义的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29833174

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档