checkCIF API
checkCIF
API
checkCIF can be accessed from your own application by sending an HTTP POST request to
http://checkcif.iucr.org/cgi-bin/checkcif_curl.pl with the following fields:
| userid | containing the user ID provided when you register for this service |
| passcode | containing the passcode associated with the userid |
| outputtype |
containing one of the following: htmlstream (to obtain an HTML version of the report), pdfstream (to obtain a PDF), pdflink (to obtain a temporary link to the PDF on the IUCr site) |
| journalname | containing the name of the journal when the CIF is being checked for publication purposes |
| submit | containing the value 1 |
| file | containing the CIF contents |
A popular way to perform such HTTP requests is to use cURL (http://curl.haxx.se/), either from the command line or by using a scripting language that includes a cURL module.
For example, to fetch an HTML version of a checkCIF report using cURL on the command line simply type:
curl --form userid=yourID --form passcode=yourPassCode --form outputtype=htmlstream --form journalname="Name of Journal" --form submit=1 --form file=@/path/to/local.cif "http://checkcif.iucr.org/cgi-bin/checkcif_curl.pl" > /path/to/returned/checkcif_report.html
The examples below show how to access checkCIF using a variety of languages, both with and without cURL.
Example scripts
1. Example of using PHP with cURL
<?php
function checkCIF_via_cURL($localfile, $outputtype, $journalname)
{
$postData = array();
$postData['userid'] = "ID";
$postData['passcode'] = "PASSWD";
$postData['outputtype'] = $outputtype; // htmlstream | pdflink | pdfstream
$postData['journalname'] = $journalname; // e.g. Acta Crystallographica Section C
$postData['submit'] = "1";
$postData[ 'file' ] = "@{$localfile};filename=cif.cif;type=text/plain";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://checkcif.iucr.org/cgi-bin/checkcif_curl.pl' );
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData );
$response = curl_exec( $ch );
curl_close($ch);
if ($outputtype=="pdfstream") {
$spos=stripos($response, '<pdfstream>');
$epos=strripos($response, '</pdfstream>'); //php5+
if ($spos===false||$epos===false) return "Error";
$spos+=11;
$pdf=substr($response, $spos, $epos-$spos);
return $pdf;
}
return $response;
}
/* THIS SCRIPT IS PROVIDED AS IS WITH NO WARRANTY OF ANY KIND, INCLUDING THE
WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
*/
?>
2. Example of using PHP without cURL
<?php
function checkCIF_via_stream($localfile, $outputtype, $journalname)
{
// create a string containing the content of the http request:
$data = '';
$eol = "\r\n";
$mime_boundary=md5(time());
$size=filesize($localfile);
$fp = fopen($localfile, 'r');
$content = fread($fp, $size);
$data .= '--' . $mime_boundary . $eol;
$data .= 'Content-Disposition: form-data; name="userid"' . $eol . $eol;
$data .= 'ID' . $eol;
$data .= '--' . $mime_boundary . $eol;
$data .= 'Content-Disposition: form-data; name="passcode"' . $eol . $eol;
$data .= 'PASSWD' . $eol;
$data .= '--' . $mime_boundary . $eol;
$data .= 'Content-Disposition: form-data; name="outputtype"' . $eol . $eol;
$data .= $outputtype . $eol;
$data .= '--' . $mime_boundary . $eol;
$data .= 'Content-Disposition: form-data; name="journalname"' . $eol . $eol;
$data .= $journalname . $eol;
$data .= '--' . $mime_boundary . $eol;
$data .= 'Content-Disposition: form-data; name="submit"' . $eol . $eol;
$data .= '1' . $eol;
$data .= '--' . $mime_boundary . $eol;
$data .= 'Content-Disposition: form-data; name="file"; filename="'.$cifid . '.cif"' . $eol;
$data .= 'Content-Type: text/plain' . $eol . $eol;
$data .= $content . $eol;
$data .= "--" . $mime_boundary . "--" . $eol;
// create the http request:
$params = array('http' => array(
'method' => 'POST',
'header' => 'Content-Type: multipart/form-data; boundary=' . $mime_boundary . $eol,
'content' => $data
));
$ctx = stream_context_create($params);
// send the http request and read the response:
$rfp = fopen("http://checkcif.iucr.org/cgi-bin/checkcif_curl.pl", 'rb', false, $ctx);
$response = stream_get_contents($rfp);
fclose($fp);
fclose($rfp);
if ($response === false) {
return "Error";
}
if ($outputtype=="pdfstream") {
$spos=stripos($response, '<pdfstream>');
$epos=strripos($response, '</pdfstream>'); //php5+
if ($spos===false||$epos===false) return "Error";
$spos+=11;
$pdf=substr($response, $spos, $epos-$spos);
return $pdf;
}
return $response;
}
/* THIS SCRIPT IS PROVIDED AS IS WITH NO WARRANTY OF ANY KIND, INCLUDING THE
WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
*/
?>
1. Example of using Python with cURL
import pycurl
from cStringIO import StringIO
filename='/path/to/local.cif'
c = pycurl.Curl()
c.setopt(c.POST, 1)
c.setopt(c.HTTPPOST, [('submit', '1'), ('userid', 'ID'), ('passcode', 'PASSWD'), ('outputtype', 'pdfstream'), ('journalname', 'Acta Crystallographica Section C'), (('file', (c.FORM_FILE, filename)))])
bodyOutput = StringIO()
headersOutput = StringIO()
c.setopt(c.WRITEFUNCTION, bodyOutput.write)
c.setopt(c.URL, "http://checkcif.iucr.org/cgi-bin/checkcif_curl.pl" )
c.perform()
op = bodyOutput.getvalue()
spos=op.find("<pdfstream>")
epos=op.rfind("</pdfstream>")
if epos>spos & spos>-1: op=op[spos+11:epos]
print op
# THIS SCRIPT IS PROVIDED AS IS WITH NO WARRANTY OF ANY KIND, INCLUDING THE
# WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2. Example of using Python without cURL
import urllib2
import hashlib
import time
m = hashlib.md5()
m.update(str(time.time()))
mime_boundary = m.hexdigest()
eol = '\r\n'
filename='/path/to/local.cif'
cif=''
with open (filename, "r") as myfile:
cif=myfile.read()
content = []
content.append('--' + mime_boundary)
content.append('Content-Disposition: form-data; name="userid"')
content.append('')
content.append('ID')
content.append('--' + mime_boundary)
content.append('Content-Disposition: form-data; name="passcode"')
content.append('')
content.append('PASSWD')
content.append('--' + mime_boundary)
content.append('Content-Disposition: form-data; name="outputtype"')
content.append('')
content.append('pdfstream')
content.append('--' + mime_boundary)
content.append('Content-Disposition: form-data; name="journalname"')
content.append('')
content.append('Acta Crystallographica Section C')
content.append('--' + mime_boundary)
content.append('Content-Disposition: form-data; name="submit"')
content.append('')
content.append('1')
content.append('--' + mime_boundary)
content.append('Content-Disposition: form-data; name="file"; filename="cif.cif"')
content.append('Content-Type: text/plain')
content.append('')
content.append(cif)
content.append('--' + mime_boundary + '--')
content.append('')
body = eol.join(content)
content_type = 'multipart/form-data; boundary=%s' % mime_boundary
headers = {
'Content-Type': content_type,
'Content-Length': str(len(body)),
}
request = urllib2.Request('http://checkcif.iucr.org/cgi-bin/checkcif_curl.pl', data=body, headers=headers)
f = urllib2.urlopen(request)
op = f.read()
spos=op.find("<pdfstream>")
epos=op.rfind("</pdfstream>")
if epos>spos & spos>-1: op=op[spos+11:epos]
print op
# THIS SCRIPT IS PROVIDED AS IS WITH NO WARRANTY OF ANY KIND, INCLUDING THE
# WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1. Example of using Java
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
public class checkcif {
public static void main(String[] args)
throws IOException {
String in="";
String out="";
String otype="htmlstream";
String journal="";
for (int a=0;a<args.length;a++) {
if (args[a].equals("-i") && a+1<args.length && !args[a+1].startsWith("-")) in=args[a+1];
else if (args[a].equals("-o") && a+1<args.length && !args[a+1].startsWith("-")) out=args[a+1];
else if (args[a].equals("-t") && a+1<args.length && !args[a+1].startsWith("-")) otype=args[a+1];
else if (args[a].equals("-j") && a+1<args.length && !args[a+1].startsWith("-")) journal=args[a+1];
}
if (in.isEmpty()) {
System.out.println("ERROR: no input file");
System.out.println("Arguments:");
System.out.println("-i\t <input cif name>");
System.out.println("-o\t <output file name>\t (default: stdout)");
System.out.println("-t\t <output type>\t (htmlstream, pdfstream or pdflink; default: htmlstream)");
System.out.println("-j\t <journal>\t (if applicable)");
return;
}
File ifile=new File(in);
in=ifile.getAbsolutePath();
if (!out.isEmpty()) {
File ofile=new File(out);
out=ofile.getAbsolutePath();
}
new checkcif().getReport(in, out, otype, journal);
}
private void getReport(String filenamei, String filenameo, String type, String journal)
throws IOException {
File cif = new File(filenamei);
String mime_boundary = Long.toHexString(System.currentTimeMillis());
URLConnection connection = new URL("http://checkcif.iucr.org/cgi-bin/checkcif_curl.pl").openConnection();
connection.setDoOutput(true); // i.e. POST
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + mime_boundary);
PrintWriter writer = null;
try {
writer = new PrintWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));
writer.append("\r\n");
writer.append("--" + mime_boundary).append("\r\n");
writer.append("Content-Disposition: form-data; name=\"userid\"").append("\r\n\r\n");
writer.append("ID").append("\r\n");
writer.append("--" + mime_boundary).append("\r\n");
writer.append("Content-Disposition: form-data; name=\"passcode\"").append("\r\n\r\n");
writer.append("PASSWD").append("\r\n");
writer.append("--" + mime_boundary).append("\r\n");
writer.append("Content-Disposition: form-data; name=\"outputtype\"").append("\r\n\r\n");
writer.append(type).append("\r\n");
writer.append("--" + mime_boundary).append("\r\n");
writer.append("Content-Disposition: form-data; name=\"journalname\"").append("\r\n\r\n");
writer.append(journal).append("\r\n");
writer.append("--" + mime_boundary).append("\r\n");
writer.append("Content-Disposition: form-data; name=\"submit\"").append("\r\n\r\n");
writer.append("1").append("\r\n");
writer.append("--" + mime_boundary);
writer.append("Content-Disposition: form-data; name=\"file\"; filename=\"cif.cif\"").append("\r\n");
writer.append("Content-Type: text/plain; charset=UTF-8").append("\r\n");
writer.append("\r\n");
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(cif), "UTF-8"));
for (String line; (line = reader.readLine()) != null;) {
writer.println(line);
}
} finally {
if (reader != null) try { reader.close(); } catch (IOException logOrIgnore) {}
}
writer.append("\r\n");
writer.append("--" + mime_boundary + "--").append("\r\n");
} finally {
if (writer != null) writer.close();
}
if (((HttpURLConnection) connection).getResponseCode()!=200)
{
((HttpURLConnection) connection).disconnect();
System.out.println("Server error");
return;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream in= ((HttpURLConnection) connection).getInputStream();
byte[] buffer = new byte[8 * 1024];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
if (type.equals("pdfstream")) {
String srchs="<pdfstream>";
String srche="</pdfstream>";
byte[] op = out.toByteArray();
byte[] sa = srchs.getBytes();
byte[] sb = srche.getBytes();
int spos=KPMindexOf(op, 0, out.size()-1, sa);
int epos=-1;
int ep=spos;
while (ep>-1) {
ep=KPMindexOf(op, ep, out.size()-1, sb);
if (ep>0) {
epos=ep;
ep=ep+11;
}
}
if (!filenameo.isEmpty()) {
OutputStream pout = new FileOutputStream(filenameo);
if (spos>0&&epos>spos) {
spos+=11;
pout.write(op, spos, epos-spos);
}
else {
pout.write(op);
}
pout.close();
}
else {
if (spos>0&&epos>spos) {
spos+=11;
System.out.write(op, spos, epos-spos);
}
else {
System.out.write(op);
}
}
}
else {
if (!filenameo.isEmpty()) {
OutputStream pout = new FileOutputStream(filenameo);
pout.write(out.toByteArray());
pout.close();
}
else {
System.out.write(out.toByteArray());
}
}
in.close();
((HttpURLConnection) connection).disconnect();
}
private int KPMindexOf( byte[] data, int start, int stop, byte[] pattern) {
if ( data == null || pattern == null) return -1;
int[] failure = KPMcomputeFailure(pattern);
int j = 0;
for( int i = start; i < stop; i++) {
while (j > 0 && ( pattern[j] != '*' && pattern[j] != data[i])) {
j = failure[j - 1];
}
if (pattern[j] == '*' || pattern[j] == data[i]) {
j++;
}
if (j == pattern.length) {
return i - pattern.length + 1;
}
}
return -1;
}
private int[] KPMcomputeFailure(byte[] pattern) {
int[] failure = new int[pattern.length];
int j = 0;
for (int i = 1; i < pattern.length; i++) {
while (j>0 && pattern[j] != pattern[i]) {
j = failure[j - 1];
}
if (pattern[j] == pattern[i]) {
j++;
}
failure[i] = j;
}
return failure;
}
}
/* THIS SCRIPT IS PROVIDED AS IS WITH NO WARRANTY OF ANY KIND, INCLUDING THE
WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
*/
1. Example of using Perl with cURL
#!/usr/bin/perl
use warnings;
use strict;
use WWW::Curl::Easy;
use WWW::Curl::Form;
my $url = 'http://checkcif.iucr.org/cgi-bin/checkcif_curl.pl';
my $localcif = '/path/to/local.cif';
my $form = WWW::Curl::Form->new();
$form->formadd("userid", "ID");
$form->formadd("passcode", "PASSWD");
$form->formadd("outputtype", "pdfstream");
$form->formadd("journalname", "Acta Crystallographica Section C");
$form->formadd("submit", "1");
$form->formaddfile($localcif, 'file', 'multipart/form-data');
my $curl = WWW::Curl::Easy->new() or die $!;
$curl->setopt(CURLOPT_HTTPPOST, $form);
$curl->setopt(CURLOPT_URL, $url);
my $response;
open(my $fileb, ">", \$response);
$curl->setopt(CURLOPT_WRITEDATA,$fileb);
#$curl->setopt(CURLOPT_WRITEDATA,\$response);
my $retcode = $curl->perform;
if ($retcode == 0) {
#my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE);
$response =~ s/^.*<pdfstream>|<\/pdfstream>.*$//sm;
print("$response");
} else {
# Error code, type of error, error message
print("$retcode ".$curl->strerror($retcode)." ".$curl->errbuf."\n");
}
# THIS SCRIPT IS PROVIDED AS IS WITH NO WARRANTY OF ANY KIND, INCLUDING THE
# WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2. Example of using Perl without cURL
#!/usr/bin/perl
use LWP::UserAgent;
use warnings;
use strict;
my $ua = LWP::UserAgent->new;
$ua->timeout(10);
my $url = "http://checkcif.iucr.org/cgi-bin/checkcif_curl.pl";
my $localcif = "/path/to/local.cif";
my $response = $ua->post( $url,
Content_Type => 'form-data',
Content => [ "file" => ["$localcif"], "submit" => "1" , "userid" => "ID", "passcode" => "PASSWD", "outputtype" => "pdfstream", "journalname" => "Acta Crystallographica Section C" ]
);
my $pdf = $response->content;
$pdf =~ s/^.*<pdfstream>|<\/pdfstream>.*$//sm;
print("$pdf");
# THIS SCRIPT IS PROVIDED AS IS WITH NO WARRANTY OF ANY KIND, INCLUDING THE
# WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1. Example of using VBScript with cURL
' Save as e.g. checkcif.vbs and run using
' e.g. Cscript checkcif.vbs /i:"C:\path\to\local.cif" /o:"C:\path\to\output.pdf" /j:"Acta Crystallographica Section C"
' from the Windows Command Prompt
Dim objShell, objCmdExec1, objCmdExec2, comm, op, spos, epos, url
comm="curl --form file=@"+ WScript.Arguments.Named.Item("i") + " --form outputtype=pdflink --form journalname="""+ WScript.Arguments.Named.Item("j") + """ --form userid=ID --form passcode=PASSWD --form submit=1 ""http://checkcif.iucr.org/cgi-bin/checkcif_curl.pl"""
Set objShell = CreateObject("WScript.Shell")
Set objCmdExec1 = objShell.exec(comm)
op = objCmdExec1.StdOut.ReadAll
spos=InStr(op,"<pdflink>")
epos=InStr(op,"</pdflink>")
spos=spos+9
url=Mid(op, spos, epos-spos)
comm= "curl -o """+WScript.Arguments.Named.Item("o")+""" """+ url +""""
Set objCmdExec2 = objShell.exec(comm)
' THIS SCRIPT IS PROVIDED AS IS WITH NO WARRANTY OF ANY KIND, INCLUDING THE
' WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2. Example of using VBScript without cURL
' Save as e.g. checkcif.vbs and run using
' e.g. Cscript checkcif.vbs /i:"C:\path\to\local.cif" /o:"C:\path\to\output.pdf" /j:"Acta Crystallographica Section C"
' from the Windows Command Prompt
Function httpGetB(sUrl)
Dim o
Set o = CreateObject("MSXML2.XMLHTTP")
o.open "GET", sUrl, False
o.send
httpGetB = o.responseBody
End Function
Function httpPost(sUrl, sData, sBoundary)
Dim o
Set o = CreateObject("MSXML2.XMLHTTP")
o.Open "POST", sUrl, False
o.setRequestHeader "Content-Type", "multipart/form-data; boundary=" + sBoundary
o.send sData
httpPost = o.responseText
End Function
Dim objFSO, objFile, sCif, sData, sBoundary, sOp, sJnl, iSpos, iEpos, Spdf, oStream
Randomize
sBoundary = Hex(Int(1000000*Rnd+1)) + Hex(Timer)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile( WScript.Arguments.Named.Item("i") , 1)
sCif = objFile.ReadAll
sData = "--" + sBoundary + vbCrLf + _
"Content-Disposition: form-data; name=""userid""" + vbCrLf + vbCrLf + _
"ID" + vbCrLf + _
"--" + sBoundary + vbCrLf + _
"Content-Disposition: form-data; name=""passcode""" + vbCrLf + vbCrLf + _
"PASSWD" + vbCrLf + _
"--" + sBoundary + vbCrLf + _
"Content-Disposition: form-data; name=""outputtype""" + vbCrLf + vbCrLf + _
"pdflink" + vbCrLf + _
"--" + sBoundary + vbCrLf + _
"Content-Disposition: form-data; name=""journalname""" + vbCrLf + vbCrLf + _
WScript.Arguments.Named.Item("j") + vbCrLf + _
"--" + sBoundary + vbCrLf + _
"Content-Disposition: form-data; name=""submit""" + vbCrLf + vbCrLf + _
"1" + vbCrLf + _
"--" + sBoundary + vbCrLf + _
"Content-Disposition: form-data; name=""file""; filename=""cif.cif""" + vbCrLf + _
"Content-Type: text/plain" + vbCrLf + vbCrLf + _
sCif + vbCrLf + _
"--" + sBoundary + "--"
sOp = httpPost("http://checkcif.iucr.org/cgi-bin/checkcif_curl.pl", sData, sBoundary)
iSpos=InStr(sOp,"<pdflink>")
iEpos=InStr(sOp,"</pdflink>")
iSpos=iSpos+9
sOp=Mid(sOp, iSpos, iEpos-iSpos)
Spdf=httpGetB(sOp)
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write Spdf
oStream.SaveToFile WScript.Arguments.Named.Item("o"), 2
oStream.Close
' THIS SCRIPT IS PROVIDED AS IS WITH NO WARRANTY OF ANY KIND, INCLUDING THE
' WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1. Example of using Linux shell script with cURL
#!/bin/sh
CIF=$1 # /path/to/local.cif
PDF=$2 # /path/to/returned/checkcif_report.pdf
JNL=$3 # journalname
# run cURL to obtain a link to the PDF on the IUCr website
CURLOP=`curl --form file=@$CIF --form outputtype=pdflink --form journalname="$JNL" --form userid=ID --form passcode=PASSWD --form submit=1 "http://checkcif.iucr.org/cgi-bin/checkcif_curl.pl"`
# remove the wrapper around the PDF link
L=${CURLOP/#*<pdflink>/}
LINK=${L/%<\/pdflink>*/}
# retieve the PDF using the link
curl -o $PDF "$LINK"
# THIS SCRIPT IS PROVIDED AS IS WITH NO WARRANTY OF ANY KIND, INCLUDING THE
# WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE

menu![[American Chemical Society]](http://checkcif.iucr.org/logos/logo_acs60d.gif)
![[Elsevier]](http://checkcif.iucr.org/logos/Elsevierlogo85.gif)
![[Wiley]](http://checkcif.iucr.org/logos/wiley_logo.gif)
![[Royal Society of Chemistry]](http://checkcif.iucr.org/logos/rsc_logo.png)
![[Chemical Society of Japan]](http://checkcif.iucr.org/logos/csj.gif)



