-1

I'm using cURL cmd for trying a API.

I will need help to translate this into PHP :

curl -X POST "https://open.faceit.com/chat/v1/rooms/hub-4d2be024-1573-4312-9c56-425003027f08-general/messages" -H "accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer <ACCES_TOKEN>" -d "{ \"body\": \"test\"}"

Thank you for your future help !

BADZY
  • 3
  • 2
  • 1
    Show us, what have you already tried, https://stackoverflow.com/questions/2138527/php-curl-http-post-sample-code – Pavel Třupek May 20 '19 at 12:31
  • Welcome to stack overflow. Stackoverflow is not a code writing service. If you are stuck at a problem we are glad to help, if you show us you've done some effort. Please provide use a [mcve] that shows us, what you have tries so far. – Pretasoc May 20 '19 at 12:31
  • Hello @Pretasoc. I tried several things, especially with the Guzzle lib. Not being familiar with REST API I have trouble using it. The documentation of my API is not really complete being in BETA so I have to get along as best I can. Until now I managed to exchange a code in token via the connection system of the API. I will now use this token to send a message in a chat provided for this purpose. – BADZY May 20 '19 at 12:53

1 Answers1

0

Something like this should do it, there are a lot of post on using cUrl with PHP

How to POST JSON Data With PHP cURL? , Set bearer token with cURL

<?php
  $curl_handle=curl_init();
  curl_setopt($curl_handle,CURLOPT_URL,'https://open.faceit.com/chat/v1/rooms/hub-4d2be024-1573-4312-9c56-425003027f08-general/messages');
  curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);

  $authorization = "Authorization: Bearer ".$token;
  curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization ));

  # Return response instead of printing.
  curl_setopt( $curl_handle, CURLOPT_RETURNTRANSFER, true );

   $post = json_encode(["body"=>"test"]);
  curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $post);

  $buffer = curl_exec($curl_handle);
  curl_close($curl_handle);

  if (empty($buffer)){
      print "Nothing returned from url.<p>";
  }
  else{
      print $buffer;
  }
?>
Dylan Kas
  • 2,212
  • 1
  • 6
  • 21
  • Thank you for your reply. Even giving the correct access_token, I have this error that appears: {"error_description": "The access token is missing", "error": "invalid_request"} 1 – BADZY May 20 '19 at 12:38
  • Did you specify your $token ? You need to pass a token to the Authorization, see line 6 of my code. Try to see your http headers of your request to see if everything is in there. – Dylan Kas May 20 '19 at 12:39
  • Yes I specified the token – BADZY May 20 '19 at 12:40
  • Did you check your HTTP headers ? Do you see the proper token in Authorization: Bearer XXXX ? – Dylan Kas May 20 '19 at 12:43
  • It seems to work, there was a variable problem $ch and $curl_handle with my old script. Now I would like to be able with an HTML input add a text "body", do you have a solution? I have to decode the json file? – BADZY May 20 '19 at 12:46
  • You can just do a basic form, and have the curl part of your code in the .php that handle your form. Then just get the value of your input text with $_POST['yourInput'] or $_GET['yourInput'] if your are using GET instead of POST – Dylan Kas May 20 '19 at 12:50
  • Thank you for your help. There is still something that I do not understand ... In the script that you sent me what is the value which allows to publish in POST something via the value "body" of the file json? – BADZY May 20 '19 at 12:57
  • I just encoded in json an array in a $post variable, see $post = json_encode(["body"=>"test"]); – Dylan Kas May 20 '19 at 12:58
  • Thank you so much, everything is functional, I will now study php cURL documentation ! – BADZY May 20 '19 at 13:00
  • You are welcolme, try to do it before posting a question next time though. – Dylan Kas May 20 '19 at 13:01
  • No problem, I'll dig a little more head :-) – BADZY May 20 '19 at 13:03