Bonjour,

Je suis en train de développer une requête REST dans mon application en utilisant PHP curl.
Auparavant, j'ai testé ma requête avec curl en ligne de commande (hors PHP) et elle fonctionne, par contre en PHP ça bloque.
Voici ma requête curl :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
curl -v --basic -u'username' -F file="@documentTest.pdf;type= application/octet-stream" -F data='{"nomDocument":"test.pdf","externalid":"123456"};type= application/json' https://serveur.com/api/rest/action
En PHP voici mon code :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
        $header = array('Authorization: Basic c2lnbWEud3M6VDZkOWxrIUUy', 'Content-Type: multipart/form-data');
        $dataJson = '{"nomDocument":"test.pdf","externalid":"123456"}';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_VERBOSE, true);
        curl_setopt($ch, CURLOPT_STDERR, $verbose);    
        curl_setopt(
            $ch, CURLOPT_POSTFIELDS, array(
                'file' => new \CurlFile(realpath('documentTest.pdf'), 'application/octet-stream', 'documentTest.pdf'),
                'data' => $dataJson
            )
        );
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_URL, 'https://serveur.com/api/rest/action');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // false for https
        $page_content = curl_exec($ch);
        curl_close($ch);
Je pense que le souci vient du fait qu'il faut que je définisse le Content-Type pour mes données JSON, mais je ne sais pas comment faire avec CURLOPT_POSTFIELDS

Auriez-vous une idée ? Merci de votre aide.