English 中文(简体)
有没有其他方法可以在php中完成这个HTTP请求?
原标题:is there another way to do this HTTP request in php?
function do_post_request($url, $data, $optional_headers = null) {

    $request = new HttpRequest($url, HttpRequest::METH_POST);
    $request->setBody($data);
        $response = $request->send();
        return $response->getBody();

}

这段代码似乎不起作用,而且似乎破坏了我的脚本。我不知道这是否是因为我没有php_http模块,但有没有我可以使用的等效模块?

例如卷曲?我试过curl,但我对它了解不多,使用curl时,我收到了一个从我试图连接的服务器返回的“错误请求”,状态为400。

什么都好

谢谢

汤姆

编辑:

function do_post_request($url, $data, $optional_headers = null) {


    $request = new HttpRequest($url, HttpRequest::METH_POST);
     $request->setBody($data);
  $response = $request->send();
  return $response->getBody();

}


echo "before";

$response = do_post_request($url, $data);
echo "After";

这样做会使“之前”出现在页面上。但没有“之后”。

在设法打开错误报告后,我得到了以下信息:

致命错误:在第25行的/home/sites/ollysmithwineapp.com/public_html/mellowpages/geocode.php中找不到类HttpRequest

所以我需要另一种方法来完成HTTP请求。

最佳回答

是否确定已正确安装和配置HTTP扩展?

Installation/Configuration

安装

This » PECL extension is not bundled with PHP.

Information for installing this PECL extension may be found in the manual chapter titled Installation of PECL extensions. Additional information such as new releases, downloads, source files, maintainer information, and a CHANGELOG, can be located here: » http://pecl.php.net/package/pecl_http.

也许cURl是一条路

问题回答

这个问题。您可以直接插入$data,其中CURLOPT_POSTFIELDS设置在查询字符串的位置。

<?php
//
// A very simple PHP example that sends a HTTP POST to a remote site
//

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"http://www.mysite.com/tester.phtml");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
            "postvar1=value1&postvar2=value2&postvar3=value3");

// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec ($ch);

curl_close ($ch);

// further processing ....
if ($server_output == "OK") { ... } else { ... }

?>

我还使用stream_context_create()找到了一个解决方案。它使您能够更好地控制POST中发送的内容。

这里有一篇博客文章解释了如何做到这一点。它可以让你轻松地指定确切的标题和正文。

http://wezfurlong.org/blog/2006/nov/http-post-from-php-without-curl/

没有HttpRequest::setBody()方法。您应该使用addPostFields函数,使用关联数组:

function do_post_request($url, $data, $optional_headers = null) {
    $request = new HttpRequest($url, HttpRequest::METH_POST);
    $request->setPostFields($data);
    $response = $request->send();
    return $response->getBody();
}

$responseBody = do_post_request( http://www.example.com ,array( examplefield => exampledata ));




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签