一種選擇是使用的,而不是捲曲PHP的流功能
http://www.php.net/manual/en/ref.stream.php
你的代碼看起來像
$url = "http://some-restful-client/";
$params = array('http' => array(
'method' => "GET",
));
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp)
{
throw new Error("Problem with ".$url);
}
$response = @stream_get_contents($fp);
if ($response === false)
{
throw new Error("Problem reading data from ".$url);
}
echo $response //this is the contents of your request;
你需要使用PHP 4> = 4.3.0或PHP 5 tho
更新:
我把這個包裝成了一個快速上課。要使用它,請執行以下操作:
$hr = new HTTPRequest("http://someurl.com", "GET");
try
{
$data = $hr->send();
echo $data;
}
catch (Exception $e)
{
echo $e->getMessage();
}
以下是該類的代碼。您還可以將數據傳遞給構造函數的第3個和第4個參數,以相應地設置發佈數據和標題。注意發佈數據應該是一個字符串而不是數組,標題應該是一個數組,其中的鍵是標題的名稱
希望這有助於!
<?php
/**
* HTTPRequest Class
*
* Performs an HTTP request
* @author Adam Phillips
*/
class HTTPRequest
{
public $url;
public $method;
public $postData;
public $headers;
public $data = "";
public function __construct($url=null, $method=null, $postdata=null, $headers=null)
{
$this->url = $url;
$this->method = $method;
$this->postData = $postdata;
$this->headers = $headers;
}
public function send()
{
$params = array('http' => array(
'method' => $this->method,
'content' => $this->data
));
$headers = "";
if ($this->headers)
{
foreach ($this->headers as $hkey=>$hval)
{
$headers .= $hkey.": ".$hval."\n";
}
}
$params['http']['header'] = $headers;
$ctx = stream_context_create($params);
$fp = @fopen($this->url, 'rb', false, $ctx);
if (!$fp)
{
throw new Exception("Problem with ".$this->url);
}
$response = @stream_get_contents($fp);
if ($response === false)
{
throw new Exception("Problem reading data from ".$this->url);
}
return $response;
}
}
?>
很多道歉 - 我很快就把這堂課放在一起,並且有一些問題需要跟着開始。我已經更新了上面的代碼,現在應該很好,但是如果你有任何問題,請發表評論,我會盡我所能來幫助 乾杯,亞當 – Addsy 2010-01-11 12:46:19
@Addsy,我感謝你的幫助,但我是側重於這裏的捲曲。這只是一些API文檔,很多平臺都有cURL實現。無論如何,我爲此付出了努力。 – 2010-01-12 09:57:14
夠公平的。那麼在這種情況下,這聽起來你有一個問題與你的設置捲曲,因爲我只真的在Linux上使用PHP我沒有任何有用的建議,以實現它。祝你好運 - 希望你能把它整理好 – Addsy 2010-01-12 10:29:00