PHP中HTTPRequest的替代方法

问题描述:

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

I am using the HttpRequest class in my php script, but when I uploaded this script to my hosting provider's server, I get a fatal error when executing it:

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

Fatal error: Class 'HttpRequest' not found in ... on line 87

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

I believe the reason is because my hosting provider's php.ini configuration doesnt include the extension that supports HttpRequest. When i contacted them they said that we cannot install the following extentions on shared hosting. So i want the alternative for httpRequest which i make like this:

   $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中使用此请求,因为它不适用于空数据串?

Or How can i use this request in curl as it is not working for empty datastring?

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

You can use CURL in php like this:

$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;

和空的数据字符串在发布请求中没有意义,但是我已经用空的数据字符串检查了它,并且运行得很好.

and empty data string doesn't make sense in post request, but i've checked it with empty data string and it works quiet well.