Did something change with API? — Guild Wars 2 Forums

Did something change with API?

Reanne.5462Reanne.5462 Member ✭✭
edited January 15, 2018 in API Development
<?php 

            $url = "https://api.guildwars2.com/v2/commerce/prices/19684"; 

            $string = file_get_contents($url); $next = json_decode($string, true); $response = array($next); 

            foreach ($response as $value) { 

            echo var_dump($value); 

            } 

This results in NULL instead of the expected results. Why? What has changed since last time I was working on this API?

Comments

  • Eearslya.6309Eearslya.6309 Member ✭✭
    edited January 15, 2018
    [email protected] ~ $ php temp.php
    array(4) {
      ["id"]=>
      int(19684)
      ["whitelisted"]=>
      bool(false)
      ["buys"]=>
      array(2) {
        ["quantity"]=>
        int(362705)
        ["unit_price"]=>
        int(79)
      }
      ["sells"]=>
      array(2) {
        ["quantity"]=>
        int(1887746)
        ["unit_price"]=>
        int(95)
      }
    }
    

    Works just fine for me. First thing I'd do is dump $string to the console, and make sure that you're not encountering an error with file_get_contents().

  • smiley.1438smiley.1438 Member ✭✭
    edited January 15, 2018

    Should work as expected, except that the API times out every now and then so that file_get_contents() returns false and everything breaks.
    Check if there's a valid response like so:

    $response = file_get_contents('https://api.guildwars2.com/v2/commerce/prices/19684');
    
    if($response !== false){
        $response = json_decode($response);
    
        foreach($response as $key => $value) {
            var_dump(['key' => $key, 'value' => $value]);
        }
    }
    else{
        // handle error
    }
    
  • Reanne.5462Reanne.5462 Member ✭✭
    edited January 15, 2018

    Ok, thanks for the help - I am pretty sure I found the problem - I think it has to do with the ssl and file_get_contents. I will need to mess with my php.ini as soon as I find time.

    Yeah, its definitely my server problem, not the API :anguished:

  • I'm not gonna be able to do this using localhost anymore am I? Am I right in that I will need a Server Certificate?

  • smiley.1438smiley.1438 Member ✭✭
    edited January 16, 2018

    try this https://github.com/codemasher/php-oauth/blob/master/src/HTTP/StreamClient.php#L93 (that whole thing includes a GW2 API wrapper btw. and does everything that is required for a proper connection)

  • @Reanne.5462 said:
    I'm not gonna be able to do this using localhost anymore am I? Am I right in that I will need a Server Certificate?

    You won't need a server certificate and you still should be able to use it for local network purposes. Not exactly sure why you think this wouldn't work. Been doing similar with my own stuff and works fine.

  • smiley.1438smiley.1438 Member ✭✭
    edited January 17, 2018

    cacaert.pem != server certificate btw. and the cacert is what you need for cURL (or file_get_contents) and SSL

  • my scripts work fine on a paid host, but I can't seem to get them to work on my win10 iis - I used the localhost for more convenience, but I don't want to spend hours trying to figure my home server out, I'll just use my other host :)

©2010–2018 ArenaNet, LLC. All rights reserved. Guild Wars, Guild Wars 2, Heart of Thorns, Guild Wars 2: Path of Fire, ArenaNet, NCSOFT, the Interlocking NC Logo, and all associated logos and designs are trademarks or registered trademarks of NCSOFT Corporation. All other trademarks are the property of their respective owners.