native api post using php

61 views
Skip to first unread message

Frank Andreas Sposito

unread,
Dec 19, 2017, 6:35:37 AM12/19/17
to Dataverse Users Community
Does anyone have experience making api calls using php? So far we have successfully processed get commands but post returns null every time. We're using the dataverse create json from the api guide as a start. Not sure what we're am getting wrong, hoping someone can identify the error or oversight. It's possible that our test server is not properly configured (for example, the ssl is not set up). Below is the code snippit. Thanks in advance for the help. Frank


         
$prm["data"]='{"name": "Scientific Research","alias": "science","dataverseContacts":[{"contactEmail": "p...@example.edu"},{"contactEmail": "stu...@example.edu"}],"affiliation": "Scientific Research University","description": "We do all the science.","dataverseType": "LABORATORY"}';
$prm
["data"]=json_encode(json_decode($prm["data"],true));          
$prm
["url"]="http://sandbox.vdc.ac/api/dataverses/";

//parameter array for request
$req
=["http"=>[
               
"method"=>"POST",
               
"ssl"=>["verify_peer"=>false,"verify_peer_name"=>false],
               
"header"=>[
                           
"Content-type:application/json",
                           
"X-Dataverse-key:{$prm["key"]}"
                         
],
               
"content"=>$prm["data"]
               
]
       
];

$prm
["context"]=stream_context_create($req); //creates context object for request

$rsp
=file_get_contents($prm["url"],false,$prm["context"]); //sends request

return json_encode(["req"=>$req,"rsp"=>$rsp]); //return request + response (should be the new dataverse id but so far is always null)




Pete Meyer

unread,
Dec 19, 2017, 10:44:10 AM12/19/17
to Dataverse Users Community
Hi Frank,

I haven't used php for API calls, but you may be running into an issue that I've run into before calling the APIs from curl/python.  Assuming my memory is correct; some of the APIs assume the data is in the form body, and some assume it's in the file field in the form body (aka - things like https://github.com/IQSS/dataverse/issues/3624).

Best,
Pete


Disclaimer - a significant portion of the github issues I've created in the past for dealing with Dataverse APIs turned out to be me doing something wrong; so it's possible I'm off-base here.

Frank Andreas Sposito

unread,
Dec 20, 2017, 1:52:16 AM12/20/17
to Dataverse Users Community
Pete,
Thanks very much. Will try the variations today. I suspect the underlying cause in this case too is that I am doing something wrong, just can't seem to figure out what it could be. It's also possible that our development installation is misconfigured, so maybe I'm even looking in the wrong place altogether. We're all new to dataverse and don't have a dedicated programmer with knowledge of the application internals to help. In any case, definitely appreciate your suggestion, and if I can ever get this working will post the solution and some sample code to help others.

Best, Frank

Frank Andreas Sposito

unread,
Dec 20, 2017, 3:53:43 AM12/20/17
to Dataverse Users Community
I think I figured out what the problem is, but still don't know the solution. When I make a POST to the dataverses endpoint without specifying a dataverse (that is, http:// sandbox.vdc.ac/api/dataverses/) it returns a 405 error:

Failed to open stream: HTTP request failed! HTTP/1.1 405 Method Not Allowed

Interestingly, if I specify a dataverse (for example the root or a subverse such as http:// sandbox.vdc.ac/api/dataverses/test) the server seems to convert the method to GET and returns the usual descriptive information about the dataverse.

So here is the question: is there a setting in Dataverse that enables or disables methods used against endpoints in addition to the endpoints themselves? Is it possible that this is a user restriction (I login as a Dataverse admin)? Alternatively, is there an underlying setting that effectively blocks certain methods even though that is not its primary purpose? 

Any and all brainstorming would be warmly welcomed.

Thanks. Frank

Philip Durbin

unread,
Dec 20, 2017, 8:31:45 AM12/20/17
to dataverse...@googlegroups.com
It's been a while since I've done any PHP. If there are enough PHP hackers out there, I'd be happy to create an empty git repo under the IQSS GitHub organization for the PHP hackers to contribute to and mention it under http://guides.dataverse.org/en/4.8.4/api/client-libraries.html

When documenting the API, I tend to use curl because I don't want to spark any language religion wars. I just tried the task at hand (creating a dataverse) and it seem to work fine on the demo site. Here's the curl syntax (I clicked the button to generate a new API token for myself afterwards):

curl -H "X-Dataverse-key: 9f1cce71-70b2-4b10-ac62-de198710c766" -H "Content-type:application/json" -X POST -d @dataverse-complete.json https://demo.dataverse.org/api/dataverses/harvard

The expected response is long but starts like this:

{"status":"OK","data":{"id":26381,"alias":"science123","name":...

One thing to note is that while I used the same "dataverse-complete.json" example you found on the "Native API" page, I had to edit the file to give it a unique alias.

Also, I initially assumed the root dataverse was called "root" but on that demo server the root dataverse is called "harvard". To discover this, I clicked on the "Advanced Search" link on the home page and looked at the URL.

I would suggest repeating the exercise above on the demo site to make sure it's working for you before turning your attention back to your own installation of Dataverse.

I'm puzzled by "post returns null every time". Even on error, this API endpoint should return some JSON will a message. For example, I got these two errors while fumbling with curl:

- {"status":"ERROR","message":"cause was null! Can't find dataverse with identifier='root'"}
- {"status":"ERROR","message":"A dataverse with alias science already exists"}

Are you saying you don't get any JSON output at all?

Also, I can't reproduce that 405 error. If I remove the "parent" dataverse from the URL I get this:

- {"status":"ERROR","message":"Root Dataverse already exists. Cannot create another one"}

With regard to API endpoints being blocked, the "admin" API should be blocked. Appropriately, I get an error like this:

- { status:"error", message:"Endpoint available from localhost only. Please contact the dataverse administrator"}

I hope this helps!

Phil



 



--
You received this message because you are subscribed to the Google Groups "Dataverse Users Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dataverse-community+unsub...@googlegroups.com.
To post to this group, send email to dataverse-community@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dataverse-community/1f0ae74e-d8b3-4dd2-95b4-2086af3b2745%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--

Frank Andreas Sposito

unread,
Dec 21, 2017, 4:12:59 AM12/21/17
to Dataverse Users Community
Phil,
This is exceptionally helpful, as usual. I successfully created a dataverse on demo.dataverse.org using your curl, then modified it for our test installation (no ssl + redirects) and received a json parse error. The same modified curl executed without error on our development server, but the dataverse itself failed to appear on our website (it may be hidden because it is unpublished). I noticed that there is already a github thread on the json parse error (https://github.com/IQSS/dataverse/issues/3533), was wondering whether any progress on figuring that out had been made.

Thanks again for helping us with this. We obviously have some configuration issues, and our learning curve with Dataverse is still very steep. Frank

To post to this group, send email to dataverse...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages