PHP HTTP 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4565451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP HTTP-Request
提问by JNK
I have MAMP Pro installed running php 5.2.13. When I try to initialize a HTTP-Request
我安装了运行 php 5.2.13 的 MAMP Pro。当我尝试初始化 HTTP 请求时
$r = new HttpRequest('http://example.com/', HttpRequest::METH_GET);
it tells me:
它告诉我:
"Class 'HttpRequest' not found in ...".
“在……中找不到‘HttpRequest’类”。
What do I need to do to 'install(?)' it?
我需要做什么来“安装(?)”它?
回答by Vojta
You must enable http extension:
您必须启用 http 扩展:
http://www.php.net/manual/en/http.setup.php
http://www.php.net/manual/en/http.setup.php
Or you can try new HTTP_Request2:
或者您可以尝试新的 HTTP_Request2:
sudo pear install --alldeps HTTP_Request2-alpha
And then:
进而:
$req = new HTTP_Request2('your.url');
$req->setMethod('POST');
$req->setHeader("content-type", $mimeType);
$req->setBody('');
$response = $req->send();
回答by Philip
Contemporary Answer for MAMP 2.0 and HTTP_Request2:
MAMP 2.0 和 HTTP_Request2 的当代答案:
Go into your MAMP/bin/php/php5.3.6/bin/ and run
进入你的 MAMP/bin/php/php5.3.6/bin/ 并运行
./pear install --alldeps HTTP_Request2
./pear install --alldeps HTTP_Request2
Restart your server and test with the following code, from the PEAR repository:
重新启动服务器并使用 PEAR 存储库中的以下代码进行测试:
<?php
require_once 'HTTP/Request2.php';
$request = new HTTP_Request2('http://pear.php.net/', HTTP_Request2::METHOD_GET);
try {
$response = $request->send();
if (200 == $response->getStatus()) {
echo $response->getBody();
} else {
echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
$response->getReasonPhrase();
}
} catch (HTTP_Request2_Exception $e) {
echo 'Error: ' . $e->getMessage();
}
?>
Don't forget the require_once statement!
不要忘记 require_once 语句!
回答by Andrey Voev
You need to enable the extension ...
您需要启用扩展...
add the following to your php.ini
将以下内容添加到您的 php.ini
extension = php_http.dll
Apparently that was asked a lot:
显然这是问了很多: