Hi Hugh,
Been meaning to knock up a proper class for this for ages; here's one I
just made that should do the trick for you; supports GET & POST + params
+ any headers you like (attached w/ example). Will flesh it out a bit
and create an open source documented class in the near future.
Hope it's of some use.
Many Regards,
Nathan
ps: I've been doing masses of RDF + SPARQL w/ PHP so if you get stuck
with anything give me a shout for pointers :) happy to help.
Hugh Glaser wrote:
> Thanks mate.
>
> On 25/01/2010 20:01, "Mischa Tuffield" <mmt...@ecs.soton.ac.uk> wrote:
>
>> Hi Hugh,
>>
>> The code you posted seems sane to me. It is very similar to the php we use to
>> make sparql-queries. My only comment being that you probably want to have a
>> line which closes curl connection after you have used it.
>>
>> something like :
>>
>> <!--
>> curl_close($ch);
>> -->
>>
>> You could also set a User Agent, to identify your self to the website's you
>> are fetching RDF from, you would do it like so :
>>
>> <!--
>> curl_setopt($ch, CURLOPT_USERAGENT, "hugh's crawler 0.1");
>> -->
>>
>> I hope this helps,
>>
>> Mischa
>>
>> On 25 Jan 2010, at 18:10, Hugh Glaser wrote:
>>
>>> OK, here¹s some fun for you...
>>> (Excuse me if it has been discussed before, and just point me at it :-) )
>>>
>>>
>>> Having struggled through the php manual for cURL, I have come up with the
>>> following draft for getting an RDF document, given a URI.
>>>
>>> $ch = curl_init();
>>> curl_setopt($ch, CURLOPT_URL, $_REQUEST['uri']);
>>> curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
>>> curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
>>> curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept:
>>> application/rdf+xml, text/n3, text/rdf+n3, text/turtle,
>>> application/x-turtle,
>>> application/turtle, text/plain"));
>>> $data = curl_exec($ch);
>>> $info = curl_getinfo($ch);
>>>
>>> if ($data === FALSE || $info['http_code'] != 200) {
>>>
>>> What does anyone think?
>>> I¹m sure there are a bunch of improvements/corrections.
>>>
>>> As a (hopefully) separate issue, the MIME types will probably generate some
>>> discussion, but it is the PHP I am primarily asking about at the moment.
>>>
>>> Best
>>> Hugh
>>>
>> _________________________________
>> Mischa Tuffield
>> ECS - http://www.ecs.soton.ac.uk/
>> Homepage - http://users.ecs.soton.ac.uk/mmt04r/
>> Identity - http://id.ecs.soton.ac.uk/person/6914
>> WebID - http://mmt.me.uk/foaf.rdf#mischa
>>
>>
>>
>>
>>
>
>
>
>
*
* Created: Nath - 25 Jan 2010 22:26:33
* Modified: SVN: $Id$
* PHP Version: 5.1.6+
*
* @package @project.name@
* @author Nathan
* @version SVN: $Revision$
*/
class RestInterface
{
const DEFAULT_TIMEOUT = 20;
const HTTP_METHOD_GET = 'GET';
const HTTP_METHOD_POST = 'POST';
public static $SUPPORTED_HTTP_METHODS = array(
self::HTTP_METHOD_GET,
self::HTTP_METHOD_POST,
);
private $endpoint;
private $headers = array();
private $method;
private $params = array();
private $timeout;
public function __construct()
{
$this->reset();
}
public function setEndpoint( $endpoint )
{
$this->endpoint = $endpoint;
}
public function getEndpoint()
{
if( is_null( $this->endpoint ) ) {
throw new InvalidArgumentException( 'no endpoint specified' );
}
return $this->endpoint;
}
public function setMethod( $method )
{
$method = strtoupper(trim($method));
if( !in_array( $method , self::$SUPPORTED_HTTP_METHODS ) ) {
throw new InvalidArgumentException( $method );
}
$this->method = $method;
}
public function setTimeout( $timeout )
{
$this->timeout = (int)$timeout;
}
public function getTimeout()
{
if( is_null( $this->timeout ) ) {
return self::DEFAULT_TIMEOUT;
}
return $this->timeout;
}
public function addParam( $key, $value )
{
$this->params[ $key ] = $value;
}
public function addHeader( $key, $value )
{
$this->headers[] = $key . ': ' . $value;
}
public function reset( $resetEndpoint=true )
{
if( $resetEndpoint ) {
$this->endpoint = null;
}
$this->headers = array();
$this->params = array();
$this->method = self::HTTP_METHOD_GET;
}
public function sendRequest()
{
$ch = curl_init();
if (! is_resource($ch) ) {
return false;
}
curl_setopt( $ch , CURLOPT_SSL_VERIFYPEER , 0 );
curl_setopt( $ch , CURLOPT_FOLLOWLOCATION , 0 );
curl_setopt( $ch , CURLOPT_URL , $this->getEndpoint() );
switch( $this->method ) {
case self::HTTP_METHOD_GET:
curl_setopt( $ch , CURLOPT_HTTPGET , 1 );
if( count($this->params) > 0 ) {
curl_setopt( $ch , CURLOPT_URL ,
$this->getEndpoint() . '?' . http_build_query($this->params)
);
}
break;
case self::HTTP_METHOD_POST:
curl_setopt( $ch , CURLOPT_POST , 1 );
if( count($this->params) > 0 ) {
curl_setopt( $ch , CURLOPT_POSTFIELDS , http_build_query($this->params) );
}
break;
}
curl_setopt( $ch , CURLOPT_TIMEOUT , $this->getTimeout() );
curl_setopt( $ch , CURLOPT_RETURNTRANSFER , 1 );
curl_setopt( $ch , CURLOPT_HTTPHEADER , $this->headers );
curl_setopt( $ch , CURLOPT_VERBOSE , 0 );
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
}
$rest = new RestInterface();
$rest->setMethod( RestInterface::HTTP_METHOD_GET );
$rest->setEndpoint( 'http://www.openlinksw.com/dataspace/person/kide...@openlinksw.com' );
$rest->addHeader( 'Accept' , 'application/rdf+xml, text/n3, text/rdf+n3, text/turtle, application/x-turtle, application/turtle, text/plain' );
echo $rest->sendRequest();