1

I can't get my Omeka POST API request work wirh PHP Curl. I need to send to Omeka API a complex json to the Omeka C API (rest) :

{"public":false,"featured":false,"item_type":null,"owner":{"id":3,"url":"https://omeka.url.ltd/api/users/3","resource":"users"},"element_texts":[{"html":false,"text":"mon_titre","element_set":{"id":1,"url":"https://omeka.url.ltd/api/element_sets/1","name":"Dublin Core","resource":"element_sets"},"element":{"id":50,"url":"https://omeka.url.ltd/api/elements/50","name":"Title","resource":"elements"}},{"html":false,"text":"Master 1 G\u00e9opolitique","element_set":{"id":1,"url":"https://omeka.url.ltd/api/element_sets/1","name":"Dublin Core","resource":"element_sets"},"element":{"id":49,"url":"https://omeka.url.ltd/api/elements/49","name":"Subject","resource":"elements"}},{"html":false,"text":"Sous la direction de J\u00e9r\u00e9my Robine","element_set":{"id":1,"url":"https://omeka.url.ltd/api/element_sets/1","name":"Dublin Core","resource":"element_sets"},"element":{"id":41,"url":"https://omeka.url.ltd/api/elements/41","name":"Description","resource":"elements"}},{"html":false,"text":"Mon Nom","element_set":{"id":1,"url":"https://omeka.url.ltd/api/element_sets/1","name":"Dublin Core","resource":"element_sets"},"element":{"id":39,"url":"https://omeka.url.ltd/api/elements/39","name":"Creator","resource":"elements"}},{"html":false,"text":"Universit\u00e9 Paris 8","element_set":{"id":1,"url":"https://omeka.url.ltd/api/element_sets/1","name":"Dublin Core","resource":"element_sets"},"element":{"id":45,"url":"https://omeka.url.ltd/api/elements/45","name":"Publisher","resource":"elements"}},{"html":false,"text":"2022/2023","element_set":{"id":1,"url":"https://omeka.url.ltd/api/element_sets/1","name":"Dublin Core","resource":"element_sets"},"element":{"id":40,"url":"https://omeka.url.ltd/api/elements/40","name":"Date","resource":"elements"}},{"html":false,"text":"DIFF_NON","element_set":{"id":1,"url":"https://omeka.url.ltd/api/element_sets/1","name":"Dublin Core","resource":"element_sets"},"element":{"id":47,"url":"https://omeka.url.ltd/api/elements/47","name":"Rights","resource":"elements"}},{"html":false,"text":"PDF","element_set":{"id":1,"url":"https://omeka.url.ltd/api/element_sets/1","name":"Dublin Core","resource":"element_sets"},"element":{"id":42,"url":"https://omeka.url.ltd/api/elements/42","name":"Format","resource":"elements"}},{"html":false,"text":"Fran\u00e7ais","element_set":{"id":1,"url":"https://omeka.url.ltd/api/element_sets/1","name":"Dublin Core","resource":"element_sets"},"element":{"id":44,"url":"https://omeka.url.ltd/api/elements/44","name":"Language","resource":"elements"}},{"html":false,"text":"Rapport de stage","element_set":{"id":1,"url":"https://omeka.url.ltd/api/element_sets/1","name":"Dublin Core","resource":"element_sets"},"element":{"id":51,"url":"https://omeka.url.ltd/api/elements/51","name":"Type","resource":"elements"}}]}

This json works perfectly when sent by postman with a simple POST request : https://omeka.url.ltd/api/items?key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx I checked the headers used by postman :

    "Content-Type:application/json"
    "Content-Length:<claculated when request is sent>"
    "Host:<claculated when request is sent>",
    "User-Agent:PostmanRuntime/7.32.2"

With all that, here is my code :

<?php

$data = array ( Here is my data as an array );
// I can check it is OK by saving it in a json file and test the result with postman: it's ok
$fp = fopen('results_'.time().'.json', 'w');
fwrite($fp, json_encode($data, JSON_UNESCAPED_SLASHES));
fclose($fp);

// Here is the Curl request
$url = 'https://omeka.url.ltd/api/items?key=xxxxxxxxxxxxxxx';
// Setup request to send json via POST
$payload = json_encode($data, JSON_UNESCAPED_SLASHES);
$length = strlen($payload);
// Create a new cURL resource
$ch = curl_init($url);
// Set the content type to application/json and headers
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Content-Type:application/json",
    "Content-Length:".$length,
    "Host:https://www.geopolitique.net",
    "User-Agent:PostmanRuntime/7.32.2")
    );
// Attach encoded JSON string to the POST fields
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
// Return response instead of outputting
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute the POST request
$result = curl_exec($ch);
// Close cURL resource
echo $result;
curl_close($ch);
?>

And the result is: 400 Bad Request. In the poor Omeka API doc, you can find : 400 Bad Request :

  • Invalid GET request parameter: “[parameter]”
  • Invalid request. Request body must be a JSON object.
  • Error when saving record.

I tried to change the headers or the headers way of declaration, but had only one significant result: il I remove one of the headers, I get a 404 forbiden error.


CBroe asked me what Content-Length did my postman request actually sent, and if the value my script calculates is the same. I tried to test that but I have a new difficulty, a 403 Forbidden error that I can't solve. I wanted to store the headers in a file, thus I created a new file what.php in the same folder than the previous one (see above) and change the $url in that previous file to https://api.url.lttd/what.php. Having a 403 Forbidden response, I created a simpler file, to make tests (test.php) (from How do I send a POST request with PHP?). So I have a test.php file:

<?php
//The url you wish to send the POST request to
$url = 'https://api.domain.ltd/what.php';

//The data you want to send via POST
$fields = [
    'key1' => 'value1',
    'key2' => 'value2',
];

//url-ify the data for the POST
$fields_string = http_build_query($fields);

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//So that curl_exec returns the contents of the cURL; rather than echoing it
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); 

//execute post
$result = curl_exec($ch);
echo "results:\r\n".$result;

?>

And a what.php:

<?php
$data = array_merge(apache_request_headers(), $_POST, $_SERVER);
//  Save in json file
    $fp = fopen('request_'.time().'.json', 'w');
    fwrite($fp, json_encode($data, JSON_UNESCAPED_SLASHES));
    fclose($fp);
?>

I send a POST request to test.php with postman, and get a 200 OK answer, but the result echoed from curl request to the what.php file is 403 Forbidden error. If I sent a POST request to what.php with postman, I get a 200 OK answer.

I can't understand why the 403 Forbidden error… Thanks for any help!

Jeremy
  • 31
  • 3

0 Answers0