How to run this Curl in PHP?

Asked

Viewed 380 times

2

I’m trying to run this Curl in PHP and do not know how to convert it. Follow the model:

Curl -u "apikey:{apikey}" -X POST "https://gateway-wdc.watsonplatform.net/assistant/api/v2/assistants/{idassistente}/Sessions? version=2019-02-07"

My main question is at this point here: -u "apikey:{apikey}" I don’t know how to do it in Curl with PHP.

The terminal works. I want to pass this logic to PHP curl_exec.

Thanks!

  • What is your doubt? -u or --user specifies user and password for server authentication.

  • The php documentation is very detailed. http://php.net/manual/en/function.curl-setopt.php Take a look at this documentation, but as our friend said, you can use curl_exec..

  • I was looking at this content last week, I hit my head too! kkkk

  • Yes... I want to pass this logic to curl_exec() .... via terminal works... but I don’t know how to pass to curl_exec

1 answer

0


You can use the functions curl_exec and curl_setopt that are native to the PHP, see;

$ch = curl_init();
;
curl_setopt($ch, CURLOPT_URL, "https://gateway-wdc.watsonplatform.net/assistant/api/v2/assistants/{idassistente}/sessions?version=2019-02-07");

curl_setopt($ch, CURLOPT_USERPWD, "apikey:apikey");
curl_setopt($ch, CURLOPT_HTTPHEADER, []);
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, []);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);

Note that it does not replace any generic value like {apikey} or {idassistente}, you can do this by concatenating strings.

  • that’s what I need... Only I believe that the line: $header = ["apikey:"." {apikey}"]; is not correct yet. is sure that the -u "apiey:..." will go in the header like this?

  • @Guilhermeferreira is right, I confused myself, now there is the line with USERPWD

Browser other questions tagged

You are not signed in. Login or sign up in order to post.