禁用卷曲呼应东西展现出来,抛出“访问冲突”

问题描述:

我有类似的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);,代码执行就好了。我已授予ext/php_curl.dll我的IIS帐户的读取权限。

任何线索,或任何其他方式,以确保卷曲不回应回应?

一种选择是使用的,而不是卷曲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; 
    } 
} 
?> 
+0

很多道歉 - 我很快就把这堂课放在一起,并且有一些问题需要跟着开始。我已经更新了上面的代码,现在应该很好,但是如果你有任何问题,请发表评论,我会尽我所能来帮助 干杯,亚当 – Addsy 2010-01-11 12:46:19

+0

@Addsy,我感谢你的帮助,但我是侧重于这里的卷曲。这只是一些API文档,很多平台都有cURL实现。无论如何,我为此付出了努力。 – 2010-01-12 09:57:14

+0

够公平的。那么在这种情况下,这听起来你有一个问题与你的设置卷曲,因为我只真的在Linux上使用PHP我没有任何有用的建议,以实现它。祝你好运 - 希望你能把它整理好 – Addsy 2010-01-12 10:29:00