2012-12-17 221 views
3

我想发送POST请求到https服务器。如何使用drupal_http_request构建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中正确构建请求。

我在做什么错?我如何让我的代码工作?

回答

8

使用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); 
+0

非常感谢!真的很感谢你的详细解答! – BLV