1

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.

mkoster
  • 69
  • 7
  • I wouldn't bother using cURL directly, use a proper API client. I use use [Pest](https://github.com/educoder/pest) - simple and effective. – halfer Nov 17 '13 at 22:45
  • @halfier Thank you for your reply. It might be an alternative, but it's not an answer to my question. Although I might use Pest in the end I still just *have to know* what's going wrong. – mkoster Nov 17 '13 at 23:13
  • It's not a direct answer, no, but it _is_ helpful. – halfer Nov 17 '13 at 23:21
  • In the meantime, try expressing `$data` as an associative array? Whilst [it seems you can use a string](http://stackoverflow.com/questions/5224790/curl-post-format-for-curlopt-postfields), perhaps that needs to be urlencoded? – halfer Nov 17 '13 at 23:22

2 Answers2

0

use the following code

$apiParams = array ("league" => "all",
                "rank_region" => "global",
                "expansion" => "hots",
                "bracket" => "1v1",
                "name" => "Wodan",
                "api_key" => "<my_key>");


$Url = "http://api.sc2ranks.com/v2/characters/search";

$curlHandler = curl_init($Url);

curl_setopt($curlHandler, CURLOPT_POST, true);
curl_setopt($curlHandler, CURLOPT_POSTFIELDS, $apiParams);
curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlHandler, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: text/html',                                                                                
    'Content-Length: ' .strlen($data))                                                                       
);
$statusCode = curl_exec($curlHandler);

curl_close($curlHandler);
0

Hello people and thank you all for your help! I have solved the problem on my own now, but I had to scroll through the PHP manual for setopt itself. This is the following code that works: $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL,"http://api.sc2ranks.com/v2/characters/search");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS,"name=Wodan&bracket=1v1&league=gold&expansion=hots&rank_region=global&api_key=<my_key>");
    //curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_exec ($ch);
    curl_close ($ch); 

Note the commented out line above. The manual say's the following about CURLOPT_RETURNTRANSFER:

TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.

Since this example is not a HTTP GET-request but a HTTP POST-request there will be no direct string collected from the URL. When set to false it will collect any output that gets returned.

My interpretation of the manual might be wrong, but this has solved the issue at least. I hope this will help other people. Thanks everyone for your help.

mkoster
  • 69
  • 7