简体   繁体   中英

how to properly post data to an IBM api using php curl

Am trying to implement ibm watson language translator using this IBM curl request data below

url -X POST -u "apikey:{apikey}" --header "Content-Type: application/json" --data "{\"text\": [\"Hello, world! \", \"How are you?\"], \"model_id\":\"en-es\"}" "{url}/v3/translate?version=2018-05-01"

I have written two codes in an attempt to get it work but when I run both codes below. it displays error

{"code":401, "error": "Unauthorized"}{"code":401, "error": "Unauthorized"}

please what can I do to get it to work. Thanks

first code attempt

<?php

$apikey ="my-api-key-goes-here";

$url = "https://api.eu-gb.language-translator.watson.cloud.ibm.com/instances/48bed10c-ce07-4a77-adec-014e0729de40/v3/translate?version=2018-05-01";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
    'apikey'=>$apikey

]));

//$params_post ="{\"text\": [\"Hello, world! \", \"How are you?\"], \"model_id\":\"en-es\"}";
$params_post ='{"text":["Hello"],"model_id":"en-es"}';
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));  

curl_setopt($ch,CURLOPT_CUSTOMREQUEST,'POST');
curl_setopt($ch,CURLOPT_POSTFIELDS, $params_post);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
 echo $response = curl_exec($ch);

curl_close($ch);

print_r($response);


?>

second code attempt

<?php

$apikey ="my-api-key-goes-here";

//$params_post ="{\"text\": [\"Hello, world! \", \"How are you?\"], \"model_id\":\"en-es\"}";
$params_post ='{"text":["Hello"],"model_id":"en-es"}';
$uname ="apikey";
$pass =$apikey;

/*
$header = array(
   'Content-Type: application/json',
   "Authorization: apikey:$apikey"
);
*/

$header = array(
    'Content-Type:application/json',
    'Authorization: Basic '.$apikey
);


// Set options for REST call via curl

$endpointurl ="https://api.eu-gb.language-translator.watson.cloud.ibm.com/instances/48bed10c-ce07-4a77-adec-014e0729de40/v3/translate?version=2018-05-01";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $endpointurl);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERPWD, "$uname:$pass"); 
//curl_setopt($ch, CURLOPT_USERPWD, $uname . ":" . $pass);

//curl_setopt($curl, CURLOPT_USERPWD, 'apikey:' . $apikey);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);


curl_setopt($curl,CURLOPT_CUSTOMREQUEST,'POST');
curl_setopt($curl,CURLOPT_POSTFIELDS, $params_post);

echo $result = curl_exec($curl);


print_r($result);



?>

I know what this topic is old, but here is what helped me.

$url = "{URL here}/v3/translate? version = 2018-05-01";
$user = "apikey";
$pass = "{API key here}";
$options = array (
    CURLOPT_RETURNTRANSFER =>true,
    CURLOPT_FOLLOWLOCATION =>true,
    CURLOPT_AUTOREFERER =>true,
);
$data = [
  'text' =>[
    'Hello, world!',
    'How are you?'
  ],
  'model_id' =>'en-es'
];
$ch = curl_init ();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_USERPWD, $user. ":". $pass);
curl_setopt ($ch, CURLOPT_HTTPHEADER, array ('Content-Type: application/json'));
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt ($ch, CURLOPT_VERBOSE, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, json_encode ($data));
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt_array ($ch, $options);
$result = curl_exec ($ch);
curl_close ($ch);

And here is where I found this https://www.tutorialfor.com/questions-111457.htm

I hope it is helpful to someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM