1

I have a unique situation. We are using Ytel's X5 cloud contact center. They have an API that will allow us to add people to our database.

You send the data to the API via GET (strange, I know, but I have no control over the server, as it is owned by Ytel).

So I have URL encoded all of my fields. I know that the resulting string is correct, because If I copy the string and paste it into my browser and hit enter, I get a success message.

The success message is plain text.

What would be the correct way to make a call to this type of API?

I tried using jQuery/Ajax but kept getting "Allow-Control-Access-Origin header is not present, therefore localhost is not allowed...". So then I tried setting the "datatype" to "jsonp".

    jQuery.ajax({
    url: ytelPostingString,
    cache: false,
    type: "POST",
    dataType: "jsonp",
    //What do you want to do when the request succeeds?
    success: function(result) {
        console.log(result);
        console.log("Success");
    },
    //What do you want to do when the request fails?
    error: function(result) {
        console.log("Error:" + result);
        console.log("Failure");
    }

});

And this worked! It successfully put the data into the database, the only downside is, the response from the server is NOT jsonp, it's plain text. So the "error" callback will always be called, even if the api request was successful, because ajax is expecting a jsonp object to be returned.

So now I'm trying to make an API call via PHP, But I still don't know what the "correct" way to do so is?

Is it possible to "open" a webpage in javascript (or PHP) and store the output (aka the "webpage") to a variable, which I can then evaluate?

Another interesting note, file_get_contents fails if I call urlencode(URL) or rawurlencode(URL) BEFORE file_get_contesnts(URL). For some reason urlencode and rawurlencode is encogind the : and the / which causes file_get_contents to fail. any ideas?

Native Coder
  • 1,792
  • 3
  • 16
  • 34
  • curl or file_get_contents? http://stackoverflow.com/questions/3062324/what-is-curl-in-php – Alexander Weihmayer May 25 '16 at 20:49
  • I've spent hours trying to do this with CuRL, I'm trying file_get_contents now, but it keeps telling me the parameter is incorrect. I just double checked, and allow_url_fopen is set to on in php.ini. – Native Coder May 25 '16 at 21:03
  • mind posting your curl / file_get_contents so we can see if there is anything out of place? – Alexander Weihmayer May 25 '16 at 21:23
  • Sure thing, as soon as I figure out how to reply to my own post. LOL – Native Coder May 25 '16 at 21:33
  • I figured it out. file_get_contents failes if you use urlencode or rawurlencode. This is a HUGE problem for me, becuase i'm building the stirng on the fly from user input. So I guess I'll just have to use string replace to urlencode the spaces only. Why would urlencode and rawurlencode BREAK the urL? it doesn't make sense. It encodes the : and the / too. – Native Coder May 25 '16 at 21:51
  • Would you mind to post your first comment as an answer so I can accept it? Turns out file_get_contents worked perfectly. But fails if I urlencode() OR rawurlencode() the string before calling file_get_contents. Doing some research now, may open a separate question for it if I don't get anywhere. – Native Coder May 25 '16 at 21:57
  • 1
    API is expecting a `GET` but you are sending `type: "POST"`. This is strange. And did you tried `dataType: "text"`? – Emre Bolat May 25 '16 at 22:01

2 Answers2

0

TO ANYONE READING THIS IN THE FUTURE:

I had to POST my values to a php script on my server, and use PHP to make the API call. It was as simple as calling file_get_contents($URL);

NOTE: In order for file_get_contents(); to work on a URL, you MUST have "allow_fopen_url" set to "yes" in your PHP.ini file.

Native Coder
  • 1,792
  • 3
  • 16
  • 34
0
  • Never user type: "POST" for dataType: "jsonp".
  • If you run on localhost you will get the errors Allow-Control-Access-Origin header is not present, So try to use this plugin to Allow access from origin :

for Google Chrome: https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi

for Firefox : https://github.com/spenibus/cors-everywhere-firefox-addon

A. Khaled
  • 1,468
  • 16
  • 28