【问题标题】:how to construct an https POST request with drupal_http_request?如何使用 drupal_http_request 构造 https POST 请求?
【发布时间】:2012-12-17 14:43:51
【问题描述】:

我想向 https 服务器发送 POST 请求。

$data = 'name=value&name1=value1';

$options = array(
  'method' => 'POST',
  'data' => $data,
  'timeout' => 15,
  'headers' => array('Content-Type' => 'application/x-www-form-urlencoded'),
);

$result = drupal_http_request('http://somewhere.com', $options);

我不知道在上面的 POST 示例代码中实现 https 选项。

谁能解释一下如何做到这一点?我对使用 Drupal 进行 PHP 编码非常陌生,我绝对可以使用该指南。

我发现只需在协议中设置它。所以我得到了这段代码。

$data = 'access_token=455754hhnaI&href=fb&template=You have people waiting to play with you, play now!';

$options = array(
  'method' => 'POST',
  'data' => $data,
  'timeout' => 15,
  'headers' => array('Content-Type' => 'application/x-www-form-urlencoded'),
);

$result = drupal_http_request('https://graph.facebook.com/1000721/notifications?', $options);

还是不行。如果我通过 Firefox 使用 https://graph.facebook.com/1000080521/notifications?access_token=45575FpHfhnaI&href=fb&template=You have people waiting to play with you, play now! 发帖,它可以工作。

我可能没有在 Drupal 中正确构建请求。

我做错了什么?如何让我的代码正常工作?

【问题讨论】:

    标签: https drupal-7 http-headers


    【解决方案1】:

    使用drupal_http_request() 与安全连接 (https://) 或不使用安全连接 (http://) 没有区别。

    PHP 编译时必须支持 OpenSSL;否则,drupal_http_request() 不支持安全连接。除此之外,唯一的问题可能是代理服务器不支持安全连接。

    作为旁注,您使用 https://graph.facebook.com/1000721/notifications? 作为请求的 URL。问号不应是 URL 的一部分。

    我还将使用drupal_http_build_query() 来构建用于请求的数据。

    $data = array(
      'access_token' => '455754hhnaI',
      'href' => 'fb',
      'template' => 'You have people waiting to play with you, play now!'
    );
    
    $options = array(
      'method' => 'POST',
      'data' => drupal_http_build_query($data),
      'timeout' => 15,
      'headers' => array('Content-Type' => 'application/x-www-form-urlencoded'),
    );
    
    $result = drupal_http_request('https://graph.facebook.com/1000721/notifications', $options);
    

    【讨论】:

      猜你喜欢
      • 2011-02-06
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 2014-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多