代码之家  ›  专栏  ›  技术社区  ›  Jan Jongboom

禁用cURL回显所有内容,抛出“访问冲突”

  •  0
  • Jan Jongboom  · 技术社区  · 15 年前

    $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帐户的读取权限。

    有什么线索,或者其他方法来确定卷曲没有回应?

    1 回复  |  直到 15 年前
        1
  •  1
  •   Addsy    15 年前

    一种替代方法是使用PHPs流函数而不是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个参数中,以便相应地设置post数据和头。注意post数据应该是一个字符串而不是一个数组,headers应该是一个数组,keys是header的名称

    希望这有帮助!!!

    <?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;
        }
    }
    ?>