【问题标题】:How to format a curl in PHP?如何在 PHP 中格式化卷曲?
【发布时间】:2017-07-21 09:57:56
【问题描述】:

我知道有一个由个人 (https://github.com/tgallice/wit-php) 创建的库。但是,我找不到他是如何格式化卷曲的。我只想做一个请求,所以使用他的库会是大材小用。

这是在终端中工作的字符串,但我不确定如何用 PHP 编写:curl -H 'Authorization: Bearer ACCESSCODE' 'htps://api.wit.ai/message?v=20160526&q=mycarisbroken'

我试过了,但它不起作用:

$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL,"htps://api.wit.ai/message?v=20160526&q=my%20car%20doesnt%20work");
curl_setopt($ch1, CURLOPT_POST, 1);
// curl_setopt($ch1, CURLOPT_POSTFIELDS,$vars);  //Post Fields
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);

$headers = [
    'Authorization: Bearer ACCESSCODEOMITTED',
];

curl_setopt($ch1, CURLOPT_HTTPHEADER, $headers);
$server_output = curl_exec ($ch1);
curl_close($ch1);

Data::$answer = json_decode($server_output)['entities']['intent'][0]['value'];

【问题讨论】:

    标签: php curl http-get


    【解决方案1】:

    由于我不确定“不起作用”代表什么,我将所需的终端命令重写为 PHP curl:

    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_URL, 'https://api.wit.ai/message?v=20160526&q=mycarisbroken' );
    curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Authorization: Bearer ACCESSCODE' ) );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE );
    $server_output = curl_exec( $ch );
    curl_close( $ch );
    

    注意:有问题的 URL 的 htps 协议不正确,因此我在答案中将其修复为 https

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-03
      • 2018-02-23
      • 1970-01-01
      • 1970-01-01
      • 2012-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多