2017-03-18 2 views
0

quelqu'un peut me aider à traduire à un code PHP:cURL avec PHP/Travailler avec PUT et GET

curl -X GET --header "Accept: text/plain" "https://test:[email protected]:1443/rest/items/test/state" 

et:

curl -X PUT --header "Content-Type: text/plain" --header "Accept: application/json" -d "OFF" "https://test:[email protected]:1443/rest/items/test/state" 

Ma tentative a échoué. En général, j'utiliser ceci:

function sendCommand($item, $data) { 
    $url = "http://192.168.1.121:8080/rest/items/" . $item; 

    $options = array(
    'http' => array(
     'header' => "Content-type: text/plain\r\n", 
     'method' => 'POST', 
     'content' => $data //http_build_query($data), 
    ), 
); 

    $context = stream_context_create($options); 
    $result = file_get_contents($url, false, $context); 

    return $result; 
} 

Mais je ne sais pas comment ajouter SSL support et l'authentification au script et je sais que cela est plus facile avec cURL.

Répondre

0
function sendCommand($item,$itemValue){ 
     $ch = curl_init("http://[IP from openHAB2]:8080/rest/items/".$item."/state"); 
     $curlOptions = array(
      CURLOPT_RETURNTRANSFER => TRUE, 
      CURLOPT_HTTPHEADER => array('Content-Type: text/plain','Accept: application/json'), 
      CURLOPT_CUSTOMREQUEST => 'PUT', 
      CURLOPT_POSTFIELDS => NULL, 
      CURLOPT_POSTFIELDS => $itemValue 
     ); 
     curl_setopt_array($ch, $curlOptions); 
     if(curl_exec($ch) === false){ 
      $this->updateDatabase(curl_error($ch)); 
      echo 'Curl-Fehler: ' . curl_error($ch); 
      exit(); 
     }else{ 
      curl_exec($ch); 
      return true; 
     } 
     curl_close($ch); 
    } 
0

La meilleure façon de le faire fonctionner avec https est d'ajouter:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

Si vous voulez faire de la bonne façon, vous pouvez lire here.

Pour authentifiant, il y a une seule ligne:

curl_setopt($process, CURLOPT_USERPWD, "username:password");

+0

Merci, je l'ai déjà trouvé le moyen d'ajouter SSL et l'authentification à cURL. Mon problème est de traduire ceci: 'curl -X GET --header" Accepter: texte/plain "" https: // test: [email protected]: 1443/reste/items/test/état "' et ' curl -X PUT --header "Content-Type: text/plain" --header "Accepter: application/json" -d "OFF" "https: // test: testp[email protected]: 1443/rest/items/test/state "' à un script PHP. – Psycho0verload

+0

Vérifiez la 2ème partie de ma réponse pour la partie authentification. Dans votre cas, il devrait être "test: testpassword". – Alex

+0

Mon problème n'est pas le SSL et l'authentification. Mon problème est de convertir le script du shell curl en un script PHP – Psycho0verload