php curl oauth, i do not manage to get data out

This space is made available to users of Open Patent Services (OPS) web-service and now also to users of EPO’s bulk data subscription products such as 14. EPO worldwide bibliographic database (DOCDB), 14.11 EPO worldwide legal status database (INPADOC), 14.12 EP full text data, 14.1 EP bibliographic data (EBD)and more.

Users can ask each other questions, exchange experiences and solutions, post ideas. The moderator will use this space to announce changes or other relevant information.
Post Reply

gerben
Posts: 46
Joined: Tue Feb 27, 2007 10:29 am
Location: Nijmegen, The Netherlands

php curl oauth, i do not manage to get data out

Post by gerben » Mon Apr 20, 2015 11:33 am

Dear Forum,

I try to get data out of the OPS system using PHP cURL functions http://php.net/manual/en/ref.curl.php. This to allow usage of oauth2 authentication.

I managed to create a token and store its data locally using the following functions:

Code: Select all

function read_token ($tokenname) {
// read token file and return token variables array
// if token not present or outdated create a new token and return new token variables array
	$tokenfile="./tokens/$tokenname.dat";
	$error='';
	if (file_exists($tokenfile)) {
		$token=unserialize(file_get_contents($tokenfile));
// convert token issued time from windows (milliseconds) format to unix (seconds) format
		$tokentime=substr($token['issued_at'],0,-3);
		$tokenduration=$tokentime + $token['expires_in'] - 120;
		if ($tokenduration < time()) {
			$error.="token '$tokenname' expired.<br>\n";
		} else {
			$token['error']=$error;
		}
	} else {
		$error.="tokenfile '$tokenname' not found.<br>\n";
	}
	if ($error) {$token=create_token($tokenname);}
	return($token);
}

function create_token ($tokenname) {
// set variables
	$tokenfile="./tokens/$tokenname.dat";
	$error='';
	switch ($tokenname) {
		case 'OPSincidental':
			$ops_key='--mykey--';
			$ops_secret='--mysecret--';
		break;
		default:
			$ops_key='';
			$ops_secret='';
		break;

	}
	$tokenUrl='https://ops.epo.org/3.1/auth/accesstoken';
	$tokenHeaders=array(
		'Authorization: Basic '.base64_encode($ops_key.':'.$ops_secret),
		'Content-Type: application/x-www-form-urlencoded'
	);
	$tokenPostFields='grant_type=client_credentials';
	
	$curlOpts=array(
		CURLOPT_URL => $tokenUrl,
		CURLOPT_HTTPHEADER => $tokenHeaders,
		CURLOPT_POSTFIELDS => $tokenPostFields,
		CURLOPT_POST => 1,
		CURLOPT_RETURNTRANSFER => 1
	);

// obtain token
	$token_request= curl_init();
		curl_setopt_array($token_request, $curlOpts);
		if (!$ops_token_response=curl_exec($token_request)) {$error.=curl_error($token_request)."<br>\n";}
	curl_close($token_request);

// process token
	$ops_token_split=explode(',', trim($ops_token_response,'{}'));
	foreach ($ops_token_split as $tokenval) {
		$tokenpair=explode(' : ', trim($tokenval));
		$token[trim($tokenpair[0],'"')]=trim($tokenpair[1],'"');
	}

// write token data to file
	file_put_contents($tokenfile, serialize($token));

// add error information to token array and return result
	$token['error']=$error;
	return($token);
}
My next step is to try to use this token to get data using the following code:

Code: Select all

<HTML>
<HEAD></HEAD>
<BODY>
<?php
// obtain token
	include_once('./functions/token.php');
	$token=read_token('OPSincidental');
	if (!$token['error']) {
		echo "Token:<br>\n<PRE>"; print_r($token); echo "</PRE>";

// prepare for sending data request
		$error='';
		$requestUrl='http://ops.epo.org/3.1/rest-services/published-data/publication/epodoc/EP1000000';
		$requestHeaders=array(
			'Authorization: Bearer '.$token['access_token'],
			'Host: ops.epo.org',
			'X-Target-URI: http://ops.epo.org',
			'Accept: application/xml',
			'Connection: Keep-Alive'
		);
	
		$curlOpts=array(
			CURLOPT_URL => $requestUrl,
			CURLOPT_HTTPHEADER => $requestHeaders,
//			CURLOPT_SSL_VERIFYPEER => FALSE,
//			CURLOPT_SSL_VERIFYHOST => FALSE,
			CURLOPT_RETURNTRANSFER => 1,
			CURLOPT_HEADER => 1
		);

// send request and collect data
		$ops_request= curl_init();
			curl_setopt_array($ops_request, $curlOpts);
			if (!$ops_response=curl_exec($ops_request)) { $error.=curl_error($ops_request)."<br>\n";}
			echo "curl options:<br>\n";
			echo "<PRE>";print_r($requestHeaders);echo "</PRE>";
		curl_close($ops_request);


		if ($error) {echo "Error:<br>\n$error";} else {echo "Result:<br>\n".htmlspecialchars($ops_response);}
	} else {
		echo $token['error'];
	}
?>
</BODY>
</HTML>
The code correctly reads the token as stored.
However the result I receive is just a response header, no response data.
The header response is as follows

Code: Select all

HTTP/1.1 303 See Other
Content-Type: text/plain
Date: Mon, 20 Apr 2015 09:49:32 GMT
Location: http://ops.epo.org/3.1/rest-services/published-data/publication/epodoc/EP1000000/biblio
Server: Apache
X-API: ops-v3.1
X-EPO-Client-IP: 82.161.239.65
X-EPO-Forwarded: [82.161.239.65]
X-IndividualQuotaPerHour-Used: 335
X-RegisteredQuotaPerWeek-Used: 0
X-Throttling-Control: idle (images=green:200, inpadoc=green:60, other=green:1000, retrieval=green:200, search=green:30)
Content-Length: 0
Connection: keep-alive
Please note that the response reports a content-type of text/plain and a content-length of 0.

I tried the following variations, all with the same result.
1)
use https;\\ with additional options:
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_SSL_VERIFYHOST => FALSE

2)
simple request headers as suggested in manual (options in sample code above is taken from developers area on http://developers.epo.org):
$requestHeaders=array(
'Authorization: Bearer '.$token['access_token'],
'Accept: application/xml'
);

Does anyone have an idea what goes wrong? There is no error, a valid header is returned, just no content.

p.s. pasting the example url in a browser, does return the expected result



Best regards,

Gerben Gieling


claessen
Posts: 20
Joined: Mon Apr 12, 2004 7:50 am
Location: Germany
Contact:

Re: php curl oauth, i do not manage to get data out

Post by claessen » Tue Oct 06, 2015 7:46 am

Did you manage to solve the problem? I have the exact same problem.

Rolf Claessen


gerben
Posts: 46
Joined: Tue Feb 27, 2007 10:29 am
Location: Nijmegen, The Netherlands

Re: php curl oauth, i do not manage to get data out

Post by gerben » Tue Oct 06, 2015 8:21 am

Sorry Rolf,

I have not yet found a solution and have not received any response to this message except yours.

The issue still blocks me from using the registered entrance into the OPS, so we simply continue to use the unregistered access to the OPS.
I would like to see this solved because:
1) I'm curious after the volume of data we actually use in the OPS
2) It probably is a very simple small mistake we make, and I hate not being able to find it.

Gerben


EPO / OPS Support
Posts: 1298
Joined: Thu Feb 22, 2007 5:32 pm

Re: php curl oauth, i do not manage to get data out

Post by EPO / OPS Support » Tue Oct 06, 2015 9:34 am

Hi

We were hoping that some user will be able to help since we don't really have a chance to check this kind of things here but I asked one of our IT colleges to have another look and this is what he recommends:
the only thing the user probably missed is a redirect from so called “no constituent url” to “biblio url“, i.e.
from http://ops.epo.org/3.1/rest-services/pu ... /EP1000000 to http://ops.epo.org/3.1/rest-services/pu ... 000/biblio
The redirect in HTTP works on the client and when the client is getting HTTP 303 it should use the url from a Location HTTP header

Alternatively the user can use direct call to biblio service http://ops.epo.org/3.1/rest-services/pu ... 000/biblio
or set value of CURLOPT_FOLLOWLOCATION option to 1
Let us know if this was helpful

Regards,
OPS support


gerben
Posts: 46
Joined: Tue Feb 27, 2007 10:29 am
Location: Nijmegen, The Netherlands

Re: php curl oauth, i do not manage to get data out

Post by gerben » Tue Oct 06, 2015 10:19 am

This makes sense, as I wrote earlier today "It probably is a very simple small mistake we make".
Thanks for pointing this out, I will try this tip later this week.


Gerben


gerben
Posts: 46
Joined: Tue Feb 27, 2007 10:29 am
Location: Nijmegen, The Netherlands

Re: php curl oauth, i do not manage to get data out

Post by gerben » Wed Oct 07, 2015 6:20 am

Adding /biblio at the end makes it work.

Thanks for the tip.


Gerben


Post Reply