【问题标题】:Alternative for HTTPRequest in phpphp中HTTPRequest的替代方案
【发布时间】:2014-06-06 09:41:24
【问题描述】:

我在我的 php 脚本中使用了 HttpRequest 类,但是当我将此脚本上传到我的托管服务提供商的服务器时,我在执行它时遇到了一个致命错误:

致命错误:在第 87 行的 ... 中找不到类 'HttpRequest'

我认为原因是因为我的托管服务提供商的 php.ini 配置不包含支持 HttpRequest 的扩展。当我联系他们时,他们说我们无法在共享主机上安装以下扩展。 所以我想要这样的 httpRequest 替代方案:

   $url= http://ip:8080/folder/SuspendSubscriber?subscriberId=5
    $data_string="";
    $request = new HTTPRequest($url, HTTP_METH_POST);
    $request->setRawPostData($data_string);
    $request->send();    
    $response = $request->getResponseBody();
    $response= json_decode($response, true);
    return $response;

或者我如何在 curl 中使用此请求,因为它不适用于空数据字符串?

【问题讨论】:

  • 发送一个空的 POST 请求有什么意义?为什么不直接使用 GET?
  • @Mike 确实在某些情况下这是有意义的。例如,如果服务器端逻辑的行为根据请求的类型而有所不同。例如 WEBDAV 请求就是这种情况。
  • 这正是@arkascha 所说的。服务器端逻辑。
  • @arkascha 有趣。我想知道如果服务器收到一个空字符串,它是如何知道使用了 POST 的。
  • @Mike Fine 如果您这么认为的话,但这完全是错误的。你为什么不按我的建议看一眼?它会清楚地向您展示:GET 请求第一个标头的示例:GET /posts/23212407/edit HTTP/1.1。在这里POST 请求:POST /posts/23212695/comments HTTP/1.1 IT 不是宗教,您应该始终不相信自己的信仰...

标签: php httprequest


【解决方案1】:

你可以像这样在 php 中使用 CURL:

$ch = curl_init( $url );
$data_string  = " ";
curl_setopt( $ch, CURLOPT_POSTFIELDS, $data_string );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));   
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );   
$result = curl_exec($ch);
curl_close($ch);    
return $result;

并且空数据字符串在发布请求中没有意义,但我已经用空数据字符串检查了它,它运行良好。

【讨论】:

  • 正如上面在 cmets 中所解释的:一个空的 POST 请求确实有意义。
  • @arkascha 不明白你与 mike 的对话可能没有足够的知识。
【解决方案2】:

你可以使用像zend这样的框架来做到这一点。框架通常有多个适配器(curl、socket、proxy)。

这是 ZF2 的示例:

    $request = new \Zend\Http\Request();
    $request->setUri('[url]');
    $request->setMethod(\Zend\Http\Request::METHOD_POST);
    $request->getPost()->set('key', $value);

    $client = new \Zend\Http\Client();
    $client->setEncType('application/x-www-form-urlencoded');

    $response = false;
    try {
        /* @var $response \Zend\Http\Response */
        $response = $client->dispatch($request);
    } catch (Exception $e) {
        //handle error
    }

    if ($response && $response->isSuccess()) {
        $result = $response->getBody();            
    } else {
        $error = $response->getBody();
    }

您不必使用整个框架,只需包含(或自动加载)您需要的类。

【讨论】:

  • 现阶段不能使用框架,为时已晚。为努力而竖起大拇指。只需要用其他东西替换httprequest..
  • 正如我所说的you don't have to use the entire framework just include (or autoload) the classes that you need.
【解决方案3】:

在php中使用cURL

<?php
// A very simple PHP example that sends a HTTP POST to a remote site
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"http://example.com/feed.rss");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,"postvar1=value1&postvar2=value2");

// in real life you should use something like:
// curl_setopt($ch, CURLOPT_POSTFIELDS, 
//          http_build_query(array('postvar1' => 'value1')));

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

$server_output = curl_exec ($ch);

curl_close ($ch);

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

?>

更多信息请看PHP Difference between Curl and HttpRequest

【讨论】:

  • 感谢@sandip,但是当我在服务器上使用它时,它不起作用,因为响应在变量中没有显示任何内容。那是空的
  • @X-Man 是的,可能在你的服务器上cURl 没有启用,你必须启用它看看如何启用它:stackoverflow.com/questions/1347146/….it depends on which server you are using(XAMPP,WAMP,LAMP etc.)
  • @X-Man 如果您使用的是 WAMP,这非常简单,只需在task-bar 右侧的click on the icon of Wamp,点击extension,然后查找curl 点击它和restart the WAMP 然后运行脚本,它会工作..
  • 配合它在本地主机上的工作,但不是在托管服务器上,因为我已经共享了 linux 托管
  • @X-Man 要求您的托管服务提供商启用cURL,检查它是否已经启用,简单的echo phpinfo() 在输出中查找cURL
【解决方案4】:

提出的 curl 方法略有变化,我对返回的 json 进行解码,如 this snippet

【讨论】:

    猜你喜欢
    • 2011-10-03
    • 2011-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-22
    相关资源
    最近更新 更多