dsfjk44656
2017-02-24 07:28
浏览 401

如何在Ajax中调用curl命令

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"

图片转代码服务由CSDN问答提供 功能建议

这是我的curl命令,无论如何使用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"
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douju5062 2017-02-28 09:55
    已采纳

    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();
    
    已采纳该答案
    打赏 评论
  • doutang7415 2017-02-24 07:34

    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

    打赏 评论
  • dongtingrun4973 2017-02-24 07:43

    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.

    打赏 评论

相关推荐 更多相似问题