简体   繁体   English

如何使用 php curl 将数据正确发布到 IBM api

[英]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我正在尝试使用以下 IBM curl 请求数据来实现 ibm watson 语言翻译器

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"} {“代码”:401,“错误”:“未经授权”}{“代码”:401,“错误”:“未经授权”}

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在这里我找到了这个https://www.tutorialfor.com/questions-111457.htm

I hope it is helpful to someone.我希望它对某人有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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