tags:

views:

130

answers:

1

I've got something like the following lines of PHP code (on 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);

When executing this I get the following exception

PHP has encountered an Access Violation at 010AD1C0

When removing the line curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);, the code is executing just fine. I have granted ext/php_curl.dll read rights for my IIS account.

Any clue, or any other way around this to make sure curl isn't echo'ing the response?

+1  A: 

One alternative would be to use PHPs Stream functions instead of curl

http://www.php.net/manual/en/ref.stream.php

Your code would look something like

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

You need to be using PHP 4 >= 4.3.0 or PHP 5 tho

UPDATE:

I wrapped this into a quick class for you. To use it, do something like the following

$hr = new HTTPRequest("http://someurl.com", "GET");

try
{
    $data = $hr->send();

    echo $data;
}
catch (Exception $e)
{
    echo $e->getMessage();
}

Here is the code for the class. You can also pass data into the 3rd and 4th parameters of the constructor to set post data and headers accordingly. Note post data should be a string not an array, headers should be an array with the keys being the name of the header

Hope this helps!!!

<?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
Many apologies - I threw this class together a bit quickly and there were some issues with it to begin with. I have updated the code above and it should be good now but please post a comment if you get any problems and I'll do my best to helpCheers, Adam
Addsy
@Addsy, I appreciate your help in this, yet I am focussing on cURL here. It's just some API documentation thingy, and many platforms have a cURL implementation. Anyhow, I +1'ed for the effort.
Jan Jongboom
Fair enough. Well in that case it sounds you have an issue with curl on your setup and since I only really use PHP on Linux I don't have any helpful suggestions on getting it going. Good luck - hope you get it sorted
Addsy