2

I have the HTTP call in php code:

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $urlForTheCallGoesHere);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);    
  $data = curl_exec($ch);
  curl_close($ch);

Which I now need to convert it to convert to VueJS Axios call. What I know so far, is that a Axious calls can be created as:

  axios.get(urlForTheCallGoesHere)
    .then(res => {
      console.log(responseData)
      //do something with the responseData
    })
    .catch(errorData => {
      console.log(errorData)
      //do something with the errorData
    })
  }

I'm aware that custom params to the call can be added like this (an example):

  axios.post(exampleUrlForSomeRegistrationLink, {
    email: "exampleEmail@gmail.com",
    password: "examplePassword"
  }).then(res => {
    console.log(responseData)
    //do something with the responseData
  }).catch(errorData => {
    console.log(errorData)
    //do something with the errorData
  })

And also I'm aware that requests/responses can be manipulated (for example in main.js entry file) with assigning a default values or with interceptors:

//using default values
axios.defaults.baseURL = 'http://changeTheUrlForTheCallExample.com';
axios.defaults.headers.common['Authorization'] = 'exampleValue';
axios.defaults.headers.get['Accepts'] = 'application/json';

//using interceptors
const reqInterceptor = axios.interceptors.request.use(config => {
  console.log('Request Interceptor', config)
  return config
})
const resInterceptor = axios.interceptors.response.use(res => {
  console.log('Response Interceptor', res)
  return res
})

But with all of the above, I am still not sure how to convert the php code to VueJS code (using Axios). The main problem is how to add the values for the options CURLOPT_RETURNTRANSFER, CURLOPT_FOLLOWLOCATION and CURLOPT_SSL_VERIFYPEER as above?

delux
  • 1,694
  • 10
  • 33
  • 64
  • Hi! If your problem is to pass url prom PHP to JS as variable, you can do this: https://stackoverflow.com/a/24834434/9021949 – soeik May 21 '18 at 17:05
  • 1
    Well for the browser `CURLOPT_FOLLOWLOCATION` and `CURLOPT_SSL_VERIFYPEER` are the default behavior. The browser follows redirects and always verifies the servers ssl-certificate (if the request is done over HTTPS). Also as you seen the data is returned from the server so the `CURLOPT_RETURNTRANSFER` option is also satisfied. If you have a problem with your call then it's somewhere else. – vstm May 21 '18 at 17:26
  • 1
    https://stackoverflow.com/questions/45578844/how-to-set-header-and-options-in-axios?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Roland May 21 '18 at 18:03
  • @vstm what I was actually asking was, is there any way to set these values via (for example) headers in the call? Since I know how to set the headers, but not sure which headers should be set. – delux May 21 '18 at 19:48

0 Answers0