简体   繁体   English

将 CURL 请求转换为 javascript

[英]Convert CURL request into javascript

Can you please help me to convert this curl post request into a javascript(Axios) request?你能帮我把这个 curl 发布请求转换成 javascript(Axios) 请求吗?

    
https://url.com/api/send.php

function send_sms($url,$apikey,$smsno=1,$destination,$content){
    $fields = array(
       'apikey' => $apikey,
       'smsno' => $smsno,
       'destination' => $destination,
       'content' => $content
    );
    $fields_string = http_build_query($fields);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($ch);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    return $output;
}

Using fetch can help you convert a CURL command to fetch code.使用fetch可以帮助您将 CURL 命令转换为 fetch 代码。

Since fetch is open source, you can modify or extend the source code at will.由于 fetch 是开源的,您可以随意修改扩展源代码。


You can also copy a request recorded with the Chrome DevTools as a fetch.您还可以将使用 Chrome DevTools 记录的请求复制为获取。

https://github.com/kigiri

Source: kigiri资料来源:基吉里

The following example will do the job:以下示例将完成这项工作:

function send_sms(apikey, smsno, destination, content){
    axios({
        method: 'post',
        url: 'https://url.com/api/send.php',
        data: {
            apikey: apikey,
            smsno: smsno,
            destination: destination,
            content: content
        }
    }).then((response) => {
        console.log(response);
    }, (error) => {
        console.log(error);
    });
}

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

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