我想向 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 编码非常陌生,我绝对可以使用该指南。
我发现只需在协议(protocol)中设置它。所以我得到了这段代码。
$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=有人等着和你一起玩,现在就玩吧!
它有效。
我可能没有在 Drupal 中正确构建请求。
我做错了什么?如何让我的代码正常工作?
最佳答案
使用 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);
关于https - 如何使用 drupal_http_request 构造 https POST 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13916287/