Reputation: 19297
I want to make the PHP
equivalent for this CURL
statement :
curl -X POST -H 'Content-Type: application/json' -u Administrator:Administrator -d '{"entity-type": "document", "name": "myNewDoc", "type": "File", "properties": {"dc:title": "My new doc", "file:content": {"upload-batch": "<myBatchId>", "upload-fileId": "0"}}}' http://192.168.128.101:8080/nuxeo/api/v1/path/Knowledge Base/workspaces/Archives Projets
Here is my code :
/**
* @Rest\Post("/api/getiterop", name="api_get_iterop")
* @Rest\RequestParam(name="sary", nullable=true)
*/
public function getIteropForm(Request $req)
{
$userNameNuxeo = "api-kb";
$mdpNuxeo = "zKqWQd3QhmigOLqVliqN#";
// création et récupération du batch_id dans nuxeo
$nuxeoBatchId = file_get_contents('http://192.168.128.101:8080/nuxeo/api/v1/upload/', false, stream_context_create([
'http' => [
'method' => 'POST',
'header' => array("Content-type: application/octet-stream", "Authorization: Basic " . base64_encode ($userNameNuxeo.":".$mdpNuxeo)),
'content' => http_build_query([]) // de la forme 'key1' => 'Hello world!', 'key2' => 'second value'
]
])
);
$nuxeoBatchId = json_decode($nuxeoBatchId, true);
$nuxeoBatchId = $nuxeoBatchId['batchId'];
// dossier pour mettre les fichiers uploadés
$iteropFolderPath = "../iterop_files/" . $nuxeoBatchId . "/";
$iteropFolder = true;
if (file_exists($iteropFolderPath) === false) {
$iteropFolder = mkdir($iteropFolderPath, 0777, true);
}
// déplacer les fichiers uploadés
if (stripos($req->headers->get('Content-Type'), 'multipart') !== false) {
$fichiers = $req->files;
if (count($fichiers) > 0) {
$fileIdx = 0;
foreach($fichiers as $field_name => $fichier) {
if ($fichier != null) {
if ($iteropFolder === true) {
// déplacement du fichier iterop
$iteropFile = $fichier->move($iteropFolderPath, $fichier->getClientOriginalName());
// envoi du fichier associé au batch vers nuxeo , par chunks
$nbChunks = 5;
for($chunkIdx = 0 ; $chunkIdx < $nbChunks ; $chunkIdx++) {
$curl = curl_init('http://192.168.128.101:8080/nuxeo/api/v1/upload/' . $nuxeoBatchId . '/' . $fileIdx);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, array('file' => '@' . realpath($iteropFolderPath . $fichier->getClientOriginalName()) . ';filename=' . $fichier->getClientOriginalName()));
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Authorization: Basic " . base64_encode($userNameNuxeo.":".$mdpNuxeo), 'X-Upload-Type: chunked', 'X-Upload-Chunk-Index:'.$chunkIdx, 'X-Upload-Chunk-Count: '.$nbChunks, 'X-File-Name: ' . $fichier->getClientOriginalName(), 'X-File-Type: ' . $iteropFile->getMimeType(), 'Content-Type: application/octet-stream', 'X-File-Size: ' . filesize(realpath($iteropFolderPath . $fichier->getClientOriginalName()))));
curl_exec($curl);
curl_close($curl);
}
// here is the bug
$name = $fichier->getClientOriginalName();
$pos = strrpos($name, '.');
$doc = array("entity-type" => "document",
"name" => false === $pos ? $name : substr($name, 0, $pos),
"type" => "File",
"properties" => array("dc:title" => false === $pos ? $name : substr($name, 0, $pos),
"file:content" => array("upload-batch" => $nuxeoBatchId, "upload-fileId" => $fileIdx)
)
);
$curl = curl_init('http://192.168.128.101:8080/nuxeo/api/v1/path/Knowledge Base/workspaces/Archives Projets');
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($doc));
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Authorization: Basic " . base64_encode($userNameNuxeo.":".$mdpNuxeo), 'Content-Type: application/json'));
curl_exec($curl);
curl_close($curl);
}
}
$fileIdx++;
}
}
}
$response = new Response();
$response->headers->set('Access-Control-Allow-Origin', '*');
$response->headers->set('Content-Type', 'application/json');
$response->setContent(json_encode(array("batchId" => $nuxeoBatchId)));
return $response;
}
But at runtime I get error : HTTP Status 505 – HTTP Version Not Supported
So what is wrong in my codes ?
Upvotes: 0
Views: 557
Reputation: 1547
For example your request converted to this php scripts:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://192.168.128.101:8080/nuxeo/api/v1/path/Knowledge');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"entity-type\": \"document\", \"name\": \"myNewDoc\", \"type\": \"File\", \"properties\": {\"dc:title\": \"My new doc\", \"file:content\": {\"upload-batch\": \"<myBatchId>\", \"upload-fileId\": \"0\"}}}");
curl_setopt($ch, CURLOPT_USERPWD, 'Administrator' . ':' . 'Administrator');
$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
The space character is unsafe because significant spaces may disappear and insignificant spaces may be introduced when URLs are transcribed or typeset or subjected to the treatment of word-processing programs.
If the url is correct try to encode it using this function urlencode
Try to add --head options to your terminal curl request. So you will get supported HTTP version in response. And add new
curl_setopt($ch, CURLOPT_HTTP_VERSION, /*Correct http version here*/);
with equivalent value.
For example:
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0 );
Here is the list of available constants.
Upvotes: 2