简体   繁体   中英

How to format a curl in PHP?

I know there is an library made by an individual ( https://github.com/tgallice/wit-php ). However, I can't find how he formatted the curl. I only want to do one request, so using his library would be overkill.

Here is the string that works in terminal, but I'm not sure how to write in PHP: curl -H 'Authorization: Bearer ACCESSCODE' 'htps://api.wit.ai/message?v=20160526&q=mycarisbroken'

I tried this but it doesn't work:

$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'];

Since I'm not sure what "doesn't work" represents, I rewrote the desired terminal command to 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 );

Note: The URL in question had incorrect htps protocol so I fixed it to https in the answer.

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