使用PHP发送数据的方法有哪些?

时间:2011-12-21 21:39:55

标签: php curl fopen

我需要将HTTP POST数据发送到网页。我的主机缺少一些扩展(我不确定哪些扩展)。我试过cURL和fopen,它们都不起作用。

有哪些其他方式发送数据?

编辑:顺便说一句,我也可以发送$ _GET数据。所以只要我可以打开一个网址(例如.file_get_contents),它就可以了。

2 个答案:

答案 0 :(得分:1)

查看非常强大的PHP stream functions

但是,如果禁用文件/流和cURL函数 - 则使用AJAX请求在前端创建它们。 jQuery is good at this只要数据不敏感。

我在前端使用jQuery JSONP请求构建了一个完整的blog system,因为我想将负载移动到用户而不是我的服务器。

答案 1 :(得分:0)

这可能有用。实际上并不需要上下文,但允许您设置自定义超时和用户代理。

/* Set up array with options for the context used by file_get_contents(). */
$opts = array(
  'http'=>array(
    'method'  => 'GET',
    'timeout' => 4,
    'header'  => "Accept-language: en\r\n" .
                 "User-Agent: Some UA\r\n"
  )
);

/* Create context. */
$context = stream_context_create($opts);

/* Make the request */
$response = @file_get_contents('http://example.com/?foo=bar', null, $context);

if($response === false) {
  /* Could not make request. */
}

您可以使用http_build_query()从数组构建查询字符串。

相关问题