This is the first question ever here, so I'll do my best.
Background I'm working on a small Starcraft II website that uses an API from a well known website. After looking at the documentation I got the example working rather quickly, returning the JSON code:
curl -X POST 'http://api.sc2ranks.com/v2/characters/search' -d 'name=Wodan&bracket=1v1&league=gold&expansion=hots&rank_region=global&api_key='<api_key>'
Hoping for an easy ride I created a small script that performs a HTTP-GET request on the API returning the base statistics for every in-game user. This URL can ofcourse be directly formed in the browser:
http://api.sc2ranks.com/v2/characters/eu/1616021?api_key=<api_key>
This is handled in my code in the following way (note: $url is the above URL and $this->session is the initialization of curl):
curl_setopt($this->session,CURLOPT_URL, $url);
curl_setopt($this->session,CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->session,CURLOPT_HEADER, true);
if( !$result = curl_exec($this->session) )
exit("cURL Error: " . curl_error($this->session));
else
return $result;
The problem Well on my way I decided to use the more advanced functions of the API to get more detailed information on the member. This is however where the confusion starts. Looking back at the above original curl post in the terminal, it shows two options. The -X (request) and the -d (post data). Figuring the first would be the URL make the HTTP-POST request to and the later being the data, I came up with the following example:
$url = "http://api.sc2ranks.com/v2/characters/search/";
$data = "league=all&rank_region=global&expansion=hots&bracket=1v1&name=Wodan?api_key=<my_key>";
curl_setopt($this->session, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($this->session, CURLOPT_POSTFIELDS, $data);
curl_setopt($this->session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->session, CURLOPT_HTTPHEADER, array(
'Content-Type: text/html',
'Content-Length: ' .strlen($data))
);
if( !$result = curl_exec($this->session) )
exit("cURL Error: " . curl_error($this->session));
else
return $result;
I've also tried to pass the above through as JSON format.
The output As a result I only get the following as a server response:String '{' was not found in 'HTTP/1.1 404 Not Found Server: nginx Date: Sun, 17 Nov 2013 22:14:03 GMT Content-Type: text/html; charset=utf-8 Content-Length: 0 Connection: close Status: 404 Not Found X-Request-Id: 700726570451c891d48862edfc8545fb X-Runtime: 0.009002 X-Rack-Cache: invalidate, pass '
What I think is going wrong Most likely I'm not forming the request right to the server or I need to target a specific file, other then they have documented it. I have found two similar questions here concerning POST's with Curl. But they were of little help to me and I feel that this might help more people out.
Hopefully someone in this community has some more experience with curl or the actual API.