0

My PHP Curl code is not returning an error or any response text. The JavaScript code I based it off of returns a Json Array.

PHP Curl

  header('Access-Control-Allow-Origin: ' . $_SERVER['SERVER_NAME']);

  $subscriptionKey = '';

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, 'https://southcentralus.tts.speech.microsoft.com/cognitiveservices/voices/list');
  curl_setopt($ch, CURLOPT_HTTPHEADER, array("Ocp-Apim-Subscription-Key", $subscriptionKey));
  curl_setopt($ch, CURLOPT_HEADER, true);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $response = curl_exec($ch);

  if($response === false)
  {
      echo 'Curl error: ' . curl_error($ch);
  }
  else
  {
      echo 'Operation completed without any errors<br>';
      echo "Response: $response";
  }
  $voices = json_decode($response);

  curl_close($ch);

PHP Results

Php Results Image Working Javascript

    var request = new XMLHttpRequest();
    var subscriptionKey = '';
    request.open('GET', 'https://southcentralus.tts.speech.microsoft.com/cognitiveservices/voices/list', true);

    request.setRequestHeader("Ocp-Apim-Subscription-Key", subscriptionKey)
    request.onload = function() {
        if (request.status >= 200 && request.status < 400) {
            const response = this.response;
            const data = JSON.parse(response);
            console.log(data);
        } else {
            window.console.log(this);
        }
    };

    request.send()

JavaScript Results

JavaScript Output Image

I changed CURLOPT_HEADER to true (thanks to Lucas's comments) and now get a 401 error. This puzzles me because it is the exact same URL and Subscription key.

6
  • Have you tried adding echo statements earlier in your script to see how far through it gets? If neither the if or else echo statements are being triggered, that suggests there's a problem earlier in the execution. I'd begin with an echo right on line 1 and a few points in between to see how far through it's getting... Commented Dec 19, 2020 at 0:59
  • @lucas "Operation completed" prints out. I probably should try turning on all errors when I get home. Commented Dec 19, 2020 at 1:11
  • Is the word "Response:" printing out? I don't see why the 'operation completed' line would print, but no the one immediately after. BTW, if the url in question is returning a 401 with no body, I'd expected the variable $response to be an empty string, because CURLOPT_HEADER isn't true. Commented Dec 19, 2020 at 1:19
  • @lucas it is getting printed. Commented Dec 19, 2020 at 1:22
  • Can you paste the exact output you're seeing from both the PHP & javascript? Also is subscriptionKey definitely an empty string in bith cases? Commented Dec 19, 2020 at 8:53

1 Answer 1

1

The array for CURLOPT_HTTPHEADER takes one item per header, rather than separate items for key & value. So in this case, try:

curl_setopt($ch, CURLOPT_HTTPHEADER, array("Ocp-Apim-Subscription-Key: $subscriptionKey"));

Sign up to request clarification or add additional context in comments.

1 Comment

Brilliant!! I would have never that to do that. Thanks for you persistence.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.