【问题标题】:get response via file_get_content with https for sendgrid.com (php)通过带有 https for sendgrid.com (php) 的 file_get_content 获取响应
【发布时间】:2014-02-12 10:24:55
【问题描述】:

由于某种原因,此代码不起作用:

$url = "https://sendgrid.com/api/newsletter/lists/email/add.json?api_user=myUserName&api_key=myPassword&list=MyList&data[]={\"email\":\"my@email.here\",\"name\":\"Matthijs de Zwart\"}";
$result = file_get_content($url);
$response = json_decode( $result );
var_dump($response);

如果我打开浏览器并转到此 URL

https://sendgrid.com/api/newsletter/lists/email/add.json?api_user=myUserName&api_key=myPassword&list=MyList&data[]={"email":"my@email.here","name":"Matthijs de Zwart"}

我确实得到了回应。

我已经使用了这些检查

echo 'openssl: ',  extension_loaded  ('openssl') ? 'yes':'no', "\n";
echo 'http wrapper: ', in_array('http', $w) ? 'yes':'no', "\n";
echo 'https wrapper: ', in_array('https', $w) ? 'yes':'no', "\n";
echo 'wrappers: ', var_dump($w);

导致以下输出:

openssl: yes
http wrapper: yes
https wrapper: yes
wrappers: array(22) {
  [0]=>
  string(13) "compress.zlib"
  [1]=>
  string(14) "compress.bzip2"
  [2]=>
  string(4) "dict"
  [3]=>
  string(3) "ftp"
  [4]=>
  string(4) "ftps"
  [5]=>
  string(6) "gopher"
  [6]=>
  string(4) "http"
  [7]=>
  string(5) "https"
  [8]=>
  string(4) "imap"
  [9]=>
  string(5) "imaps"
  [10]=>
  string(4) "pop3"
  [11]=>
  string(5) "pop3s"
  [12]=>
  string(4) "rtsp"
  [13]=>
  string(4) "smtp"
  [14]=>
  string(5) "smtps"
  [15]=>
  string(6) "telnet"
  [16]=>
  string(4) "tftp"
  [17]=>
  string(3) "php"
  [18]=>
  string(4) "file"
  [19]=>
  string(4) "glob"
  [20]=>
  string(4) "data"
  [21]=>
  string(3) "zip"
}

我一直在谷歌搜索以使其正常工作,但到目前为止,我只能找到说打开 openssl 的解决方案......确实如此。

【问题讨论】:

    标签: php json url file-get-contents


    【解决方案1】:

    尝试使用他们的 apiv3。它曾经很好。 但实际上我会寻找一个新的供应商。在他们将我们锁定在我们的帐户之外并且他们的 48 小时支持人员说“创建一个新帐户”后,我们离开了他们

    【讨论】:

      【解决方案2】:

      我使用以下代码解决了这个问题:

      $dataString = urlencode('{"email":"my@email.here","name":"Matthijs de Zwart"}');
      
      $homepage = file_get_contents("https://sendgrid.com/api/newsletter/lists/email/add.json?api_user=myUserName&api_key=myPassword&list=MyList&data[]=".$dataString);
      

      返回我所期望的。

      【讨论】:

        【解决方案3】:

        试试这个:

        <?php
        
        // helper function
        function curlGet($url)
        {
            // create curl resource
            $ch = curl_init();
        
            // set url
            curl_setopt($ch, CURLOPT_URL, $url);
        
            // return the transfer as a string
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        
            // ssl opts
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
        
            // $output contains the output string
            $output = curl_exec($ch);
        
            // close curl resource to free up system resources
            curl_close($ch);
        
            // finished
            return $output;    
        }
        
        // usage
        var_dump(curlGet("https://sendgrid.com/api/newsletter/lists/email/add.json?api_user=myUserName&api_key=myPassword&list=MyList&data[]={\"email\":\"my@email.here\",\"name\":\"Matthijs de Zwart\"}"));
        
        ?>
        

        【讨论】:

        • 您好,感谢您的帮助。在 php.ini 文件中取消注释 extension=php_curl.dll 并重新启动 apache 后,输出如下:==> string(0) ""
        猜你喜欢
        • 2015-10-19
        • 2023-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多