向Web服务发出POST请求

时间:2010-12-23 18:21:34

标签: php http post

简单地说,我需要使用php脚本向Web服务发出POST请求。问题是服务器上的php版本是4.4.x并且curl被禁用。我有什么想法可以打电话并阅读回复吗?

3 个答案:

答案 0 :(得分:2)

根据stream_context_create页面上的示例,您可以使用fopenstream_context_create

$context = stream_context_create(array(
    'http' => array (
        'method' => 'GET'
    )
));
$fp = fopen ('http://www.example.com', 'r', $context);
$text = '';
while (!feof($fp)) {
    $text .= fread($fp, 8192);
}

fclose($fp);

另外,请参阅HTTP context optionsSocket context options以查看您可以设置的选项。

答案 1 :(得分:1)

你基本上可以使用socket(fsockopen)和这样的fput:

$port = 80;
$server = "domain.com";
$valuesInPost = 'param=value&ahah=ohoho';
$lengthOfThePost = strlen($valuesInPost);

if($fsock = fsockopen($server, $port, $errno, $errstr)){
    fputs($fsock, "POST /path/to/resource HTTP/1.1 \r\n");
    fputs($fsock,"Host: $server \r\n");
    fputs($fsock,"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13 \r\n");
    fputs($fsock,"Accept-Language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3 \r\n");
    fputs($fsock,"Keep-Alive: 115 \r\n");
    fputs($fsock,"Connection: keep-alive\r\n");
    fputs($fsock,"Referer: http://refererYou.want\r\n");
    fputs($fsock,"Content-Type: application/x-www-form-urlencoded\r\n");
    fputs($fsock,"Content-Length: $lengthOfThePost\r\n\r\n");
    fputs($fsock,"$valuesInPost\r\n\r\n");
    $pcontent = "";

    // results
    while (!feof($fsock))
        $pcontent .= fgets($fsock, 1024);

   // echoes response
   echo $pcontent;
}

由于类似的重写,可能会出现一些语法错误。

请注意,您可以使用所需的端口。

答案 2 :(得分:0)

您是否可以访问PEAR http_request?