PHP卷曲到nodejs

时间:2019-08-15 21:55:09

标签: node.js curl axios

我有一些运行curl命令的php代码。但是我工作的公司正在切换到nodejs,我不确定如何在node中以相同的方式复制请求。如果我可以使用axios在前端复制请求,我什至会很高兴。我并不在乎我该怎么做,我只需要以完全相同的方式复制请求即可。

    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_HTTPHEADER => array(
                'Authorization: Token '.$token
            ),
        CURLOPT_URL => 'https://myWebsite.com/api/led/contents/',
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_POST => 1,
        CURLOPT_POSTFIELDS => array(
            url => '@bannerStockUpload/'.$assetName.'/'.$assetName.'_'.$dimensions[1].'.mp4',
                    thumbnail => '@bannerStockUpload/'.$assetName.'/'.$assetName.'_thumb.jpg',
            type => 'video',
                    width => $dimensions[0],
                    height => $dimensions[1],
                    duration => $dur,
                    name => $assetName."_".$dimensions[0]."x".$dimensions[1]."mp4"
        )
    ));
    // Send the request & save response to $resp
    $resp = curl_exec($curl);

1 个答案:

答案 0 :(得分:0)

您可以通过npm安装axios,然后:

const axios = require('axios');

const data = {
    url: '@bannerStockUpload/'.$assetName.'/'.$assetName.'_'.$dimensions[1].'.mp4', // chnage $assetName to js variable
    thumbnail: '@bannerStockUpload/'.$assetName.'/'.$assetName.'_thumb.jpg',
    type: 'video',
    width: $dimensions[0], // change dimension to js variable
    height: $dimensions[1], // change dimension to js variable
    duration: $dur,
    name: $assetName."_".$dimensions[0]."x".$dimensions[1]."mp4"
}

const headers = {
  headers: {
    Authorization: 'Token ' + $token // add javascript token
  }
}
// Make a request for a user with a given ID
axios.post('https://myWebsite.com/api/led/contents/', data, headers)
    .then(function (response) {
    // handle success
    console.log(response);
    })
    .catch(function (error) {
    // handle error
    console.log(error);
    })