简体   繁体   English

什么是PHP中的等效curl请求

[英]What is the equivalent curl request in PHP

I'm trying to get in PHP this curl: 我想用PHP卷曲这个卷曲:

curl -X POST -u "apikey:MY_API_KEY" \
--header "Content-Type: text/plain;charset=utf-8" \
--header "Accept: application/json" \
--data-binary "MY_TEXT" \
"https://MY_DIRECTION"

so far I came up with this: 到目前为止我想出了这个:

$curl = curl_init();
 $post_args = array('data-binary' => $MY_TEXT );
 $header_args = array(
     'Content-Type: text/plain;charset=utf-8',
     'Accept: application/json'
 );
 $apiKey = '$MY_API_KEY';
 $api_args = array('apikey: ' . $apiKey);
 curl_setopt($curl, CURLOPT_POST, true);
 curl_setopt($curl, CURLOPT_HTTPHEADER, $api_args);
 curl_setopt($curl, CURLOPT_POSTFIELDS, $post_args);
 curl_setopt($curl, CURLOPT_HTTPHEADER, $header_args);
 curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
 curl_setopt($curl, CURLOPT_URL, $MY_DIRECTION);
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

 $result = curl_exec($curl);
 curl_close($curl);
 json_decode($result, true);

I'm trying to use Personality Insights service of IBM. 我正在尝试使用IBM的Personality Insights服务。

Let's try with this- 让我们试试这个 -

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://MY_DIRECTION');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "MY_TEXT");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'apikey' . ':' . 'MY_API_KEY');

$headers = array();
$headers[] = 'Content-Type: text/plain;charset=utf-8';
$headers[] = 'Accept: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close ($ch);

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

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