我有类似以下PHP代码行(在IIS上):
$service_url = 'http://some-restful-client/';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$curl_response = @curl_exec($curl);
curl_close($curl);
执行此操作时,我收到以下异常
PHP在010AD1C0
遇到访问冲突
删除行curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
时,代码执行得很好。我已经为我的IIS帐户授予了ext/php_curl.dll
读取权限。
任何线索,或任何其他方式来确保卷曲不回应响应?
答案 0 :(得分:1)
一种替代方法是使用PHPs Stream函数而不是curl
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;
}
}
?>