如何在Ajax中调用curl命令

这是我的curl命令,无论如何使用ajax执行此命令</ p>

  curl -X POST -u“CONVERSATION_USERNAME”:“CONVERSATION_PASSWORD”-H“Content-Type:application / json”-d“{\”input \“:{\”text \“:\”\“}  }“”https://gateway.watsonplatform.net/conversation/api/v1/workspaces/CONVERSATION_ID/message?version=2016-07-11"

</ div>

展开原文

原文

Here is my curl command is there anyway to execute this command using ajax

curl -X POST -u "CONVERSATION_USERNAME":"CONVERSATION_PASSWORD" -H "Content-Type:application/json" -d "{\"input\": {\"text\":\" \"}}" "https://gateway.watsonplatform.net/conversation/api/v1/workspaces/CONVERSATION_ID/message?version=2016-07-11"

dongyudun6465
dongyudun6465 secure.php.net/curl
3 年多之前 回复

3个回答



创建一个PHP文件。其中文件名为chat.php </ p>

 &lt;  ?php 
if(isset($ _ POST ['conversation'])){
$ data = array(“input”=&gt; array(“text”=&gt; $ _ POST [“conversation”]));
$ url =“https://gateway.watsonplatform.net/conversation/api/v1/workspaces/a9379972-d820-4cdf-b1cb-ad0af898a534/message?version=2016-07-11";
$ch = curl_init( $ url);
curl_setopt_array($ ch,array(
CURLOPT_RETURNTRANSFER =&gt; true,
CURLOPT_POST =&gt; true,
CURLOPT_USERPWD =&gt;“用户名:密码”,
CURLOPT_HTTPHEADER =&gt;数组(“内容” -Type:application / json“),
CURLOPT_POSTFIELDS =&gt; json_encode($ data),
));
$ response = curl_exec($ ch);
curl_close($ ch);
print_r(json_decode($ 响应));
}
?&gt;
</ code> </ pre>

并使用Ajax </ p>

  var来调用它 xhr = new XMLHttpRequest(); 
// xhr.open('get','chat.php');
xhr.open(“GET”,“chat.php?data =”+要传递的数据,false );
//跟踪请求的状态更改。

xhr.on readystatechange = function(){
var DONE = 4; // readyState 4表示请求已完成。
var OK = 200; //状态200是成功返回。
if(xhr.readyState === DONE){
if(xhr.status === OK){
//alert(xhr.responseText);

talking = true;
botMessage = xhr.responseText; //'这是返回的文本。'
} else {
// console.log('错误:'+ xhr.status); //请求期间发生错误。
alert('错误:'+ xhr.status);
}
}
};

//将请求发送到send-ajax-data.php

xhr.send();
</ code> </ pre>
</ div>

展开原文

原文

Create a PHP file.here file name is chat.php

<?php
if(isset($_POST['conversation'])) {
$data = array("input"=>array("text"=>$_POST["conversation"]));
$url = "https://gateway.watsonplatform.net/conversation/api/v1/workspaces/a9379972-d820-4cdf-b1cb-ad0af898a534/message?version=2016-07-11";
$ch = curl_init($url);
curl_setopt_array($ch, array(
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_USERPWD => "username:password",
    CURLOPT_HTTPHEADER => array("Content-Type:application/json"),
    CURLOPT_POSTFIELDS => json_encode($data),
));
$response = curl_exec($ch);
curl_close($ch);
print_r(json_decode($response));
 }
 ?>

and call this by using Ajax

var xhr = new XMLHttpRequest();
//xhr.open('get', 'chat.php');
xhr.open("GET", "chat.php?data=" + data to be pass, false);
// Track the state changes of the request.
xhr.onreadystatechange = function () {
var DONE = 4; // readyState 4 means the request is done.
var OK = 200; // status 200 is a successful return.
if (xhr.readyState === DONE) {
    if (xhr.status === OK) {
        //alert(xhr.responseText); 
        talking = true;
        botMessage=xhr.responseText;// 'This is the returned text.'
    } else {
        // console.log('Error: ' + xhr.status); // An error occurred during the request.
        alert ('Error: ' + xhr.status);
    }
}
};

 // Send the request to send-ajax-data.php
 xhr.send();



这应该有效。</ p>

  $ .ajax({
url:“ https:// conversation_username:conversation_password@gateway.watsonplatform.net/conversation/api/v1/workspaces/CONVERSATION_ID/message?version = 2016-07-11“,
方法:”POST“,
标题:{
“Content-Type”:“application / json”
},
data:{
input:{
text:“”
}
}
})
done(function(data){\ n //处理成功响应
})
.fail(function(err){
// // handle error response
});
</ code> </ pre>

< a href =“http://api.jquery.com/jquery.ajax/”rel =“nofollow noreferrer”> http://api.jquery.com/jquery.ajax/ </ p>

编辑 - 更新以使用promises处理成功和错误响应。</ p>
</ div>

展开原文

原文

This should work.

$.ajax({
  url: "https://conversation_username:conversation_password@gateway.watsonplatform.net/conversation/api/v1/workspaces/CONVERSATION_ID/message?version=2016-07-11",
  method: "POST",
  headers: {
    "Content-Type": "application/json"
  },
  data: {
    input: {
      text: " "
    }
  }
})
done(function(data) {
  // handle success response
})
.fail(function(err) {
  // handle error response
});

http://api.jquery.com/jquery.ajax/

edit - updated to handle success and error responses using promises.

douqianxian7008
douqianxian7008 更新以显示如何处理成功和错误响应。 文档中列出了更多替代方案
3 年多之前 回复
dtnd30892
dtnd30892 在上述情况下,我该如何回应?@Yod
3 年多之前 回复



创建一个php文件,将该命令放在该文件中,从curl响应中返回它,并通过以下方式调用此php文件 ajax。</ p>

文件ajax_curl.php </ p>

 &lt;?php 
//在这里做你的卷曲调用
// curl - X POST -u“CONVERSATION_USERNAME”:“CONVERSATION_PASSWORD”-H“Content-Type:application / json”-d“{\”input \“:{\”text \“:\”\“}}”“https:/ /gateway.watsonplatform.net/conversation/api/v1/workspaces/CONVERSATION_ID/message?version=2016-07-11"
//请参阅http://php.net/manual/en/curl.examples-basic。 php
//如果$ url是你url
$ defaults = array(
CURLOPT_URL =&gt; $ url,
your_other_params =&gt; go_here,
CURLOPT_RETURNTRANSFER =&gt; 1
),就像这样返回 ;

$ ch = curl_init();
curl_setopt_array($ ch,$ defaults);
$ result = curl_exec($ ch);
if(!$ result = curl_exec($ ch))
{
trigger_error(curl_error($ CH));

}
curl_close($ ch);

echo json_encode($ result);
?&gt;
</ code> </ pre>

您的主叫js看起来像</ p>

   $ .post(“ajax_curl.php”,{pass_data:“pass_whatever_you_need”},函数(数据){
console.log(data);
},“json”);
</ code> </ pre >

'data'现在包含一个带有curl调用响应的json </ p>
</ div>

展开原文

原文

Create a php file, put that command inside that file, return from it whatever you need from the curl response and call this php file via ajax.

file ajax_curl.php

<?php
    //do your curl call here
    //curl -X POST -u "CONVERSATION_USERNAME":"CONVERSATION_PASSWORD" -H "Content-Type:application/json" -d "{\"input\": {\"text\":\" \"}}" "https://gateway.watsonplatform.net/conversation/api/v1/workspaces/CONVERSATION_ID/message?version=2016-07-11"
    //see http://php.net/manual/en/curl.examples-basic.php
    //do a return like so if $url is you url
    $defaults = array( 
        CURLOPT_URL => $url,
        your_other_params => go_here,
        CURLOPT_RETURNTRANSFER => 1
    ); 
    $ch = curl_init();
    curl_setopt_array($ch, $defaults);
    $result=  curl_exec($ch);
    if( ! $result = curl_exec($ch)) 
    { 
        trigger_error(curl_error($ch)); 
    } 
    curl_close($ch); 
    echo  json_encode($result);
?>

your calling js looks like

$.post( "ajax_curl.php", { passed_data: "pass_whatever_you_need" }, function( data ) {
  console.log( data ); 
}, "json");

'data' now contains a json with the response from your curl call

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐