These forums have been archived and are now read-only.

The new forums are live and can be found at https://forums.eveonline.com/

EVE Technology Lab

 
  • Topic is locked indefinitely.
 

php -> api help needed

First post
Author
Trade Future
Sidora Combative Industrialists
#1 - 2016-03-09 18:45:27 UTC  |  Edited by: Trade Future
Hello everyone,

sorry for another "API Help Thread", but I didn't find any suggestions in the other Topics.

I want to create a new Forum completely selfcoded.
So I need several API connectors, my PHP-Knowledge is like from a script-kiddy, but I gonna get it dine via tutorials and searchings on Google ;)

I already got some Code out of the Forum and tested with my personal API.
I got API-Logs on CCP-Servers, so my request is getting though the net.
But the PHP-Result is empty.

Here Comes the function I want to use:

function SendRequest($Request){
  // get file
$ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $Request);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Author: admin@chaoszirkel.de','Project: self-designed-forum')); //did not know if this works
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch);
        $result = json_decode($result);
        return $result;
        curl_close($ch);
}
function HandleReponse($Response){
  // read response
  if($Response <> ""){
   $xml = new SimpleXMLElement($Response);
   // the rest will depent on the returned xml
   echo "XML: ".$xml; // EDIT: Line-Correction
  }
}

Mine hard!!! Minerals are needed somewhere!!!

Dragonaire
Here there be Dragons
#2 - 2016-03-09 23:13:06 UTC
Know you think you want to only use your own code but truly it's much better to use one of the exist PHP API libraries then trying to figure out everything yourself since there are many little 'features' to the XML API that is not documented anywhere which those of us that have been working with it for years had to learn the hard way. Just ask yourself which is more interesting: creating the forum and having it do what you want or spending days, weeks, maybe even months trying to make something that approaches the quality of the existing libraries which have been improved on for years now?

As to the code you linked there are several problems:

  • Because of the return $result in SendRequest() the curl_close($ch); is never executed.
  • You never look to see if curl is returning an error instead of a result or if you are receiving an HTTP error etc.
  • In HandleReponse() it should be $Response !== '' instead as PHP doesn't use what you have.
  • You spelled response wrong in the function method name ;)
  • I believe you also need to URL encode your headers as well before giving them to cURL but I don't every use it directly any more because it tends to be a little 'crushy' to work with and I have better things to do with my time then re-invent the wheel when there several very good wrapper already for it.Blink


I'm sure there are several more things that would need to be fixed as well but those should get you started if you still decide to try doing everything yourself.

Finds camping stations from the inside much easier. Designer of Yapeal for the Eve API. Check out the Yapeal PHP API Library thread.

Trade Future
Sidora Combative Industrialists
#3 - 2016-03-10 07:18:47 UTC  |  Edited by: Trade Future
Dragonaire wrote:
Know you think you want to only use your own code but truly it's much better to use one of the exist PHP API libraries then trying to figure out everything yourself since there are many little 'features' to the XML API that is not documented anywhere which those of us that have been working with it for years had to learn the hard way. ...



Thank you for your answer.

I agree, using already existing "libs" will be more efficient. But I am not familiar with them, because I am not a programmer more a scripter.

Tested my $Reqest-string in browser directly and in powershell.
Both gave me response with http 200 header and xml-content.

Maybe you would be so kind and link me a thread where I can find a "simple" lib for php, because I don't want to get into foreign programming languages.

Edit:
Quote:
Maybe you would be so kind and link me a thread where I can find a "simple" lib for php, because I don't want to get into foreign programming languages.


Maybe I should have read your signiture before;)

Mine hard!!! Minerals are needed somewhere!!!

Steve Ronuken
Fuzzwork Enterprises
Vote Steve Ronuken for CSM
#4 - 2016-03-10 12:38:13 UTC
I'd suggest, if you are going to roll something of your own, you look into the guzzle library for php.

You can set up a client object and pass it around (or, ugg, use a global ;) ) which gives you an easier setup.


Yapeal is a pretty good library for use with the api. another is phealng

Woo! CSM XI!

Fuzzwork Enterprises

Twitter: @fuzzysteve on Twitter

Trade Future
Sidora Combative Industrialists
#5 - 2016-03-11 19:03:40 UTC
Hey,

me stupid Noob again...

I got GuttleHttp-Lib running, but I still do not get correct Response...
It allways Ends in a string that contains "current Time" and "Cached until-timer"...

Maybe you could give me a Hint where to look at again Sad

 namespace GuzzleHttp;

use GuzzleHttp\Guzzle;

function getAPI($url){
  $client = new Client(['base_uri' => 'https://api.eveonline.com','timeout' => 2.0,'verify' => false]);
  $request = $client->request('GET',$url,['stream' => true]);
  $body = $request->getBody();
  while(!$body->eof()){
   $xml = $body->read(1024);
  }
  return $xml;
}


On my Trials I use "account/characters.xml.aspx?keyID=1234567&vCode=asdfsdfxxx..." as URI to call the function...

I hope you won't call me a fool to do so ...

Thx for your HELP.... again ....

Mine hard!!! Minerals are needed somewhere!!!

Dragonaire
Here there be Dragons
#6 - 2016-03-12 22:32:17 UTC
So you are looping through the whole body of the request but only returning the last 1024 char of it that might be part of the problemBlink

Finds camping stations from the inside much easier. Designer of Yapeal for the Eve API. Check out the Yapeal PHP API Library thread.

Steve Ronuken
Fuzzwork Enterprises
Vote Steve Ronuken for CSM
#7 - 2016-03-13 01:21:56 UTC
It's an interesting way to get the body. Most people just grab the entire contents at once.

If you _must_ get it that way, remember to add to $xml, rather than replace.

But you could just return $body

Woo! CSM XI!

Fuzzwork Enterprises

Twitter: @fuzzysteve on Twitter

Dragonaire
Here there be Dragons
#8 - 2016-03-13 04:50:09 UTC
Think it's because he's using stream = true so not getting it all at once but you're right that the easier way to go really.

Finds camping stations from the inside much easier. Designer of Yapeal for the Eve API. Check out the Yapeal PHP API Library thread.

Trade Future
Sidora Combative Industrialists
#9 - 2016-03-13 13:29:24 UTC
Dragonaire wrote:
So you are looping through the whole body of the request but only returning the last 1024 char of it that might be part of the problemBlink


I got it working, somehow I missunderstood that streamhandling a Little...

I used the wrong function/method to extract the content out of the Body...

Now I am handling the XML-Content...

I guess /Close is now right fair.

Thanks again for your very helpful hints Big smile

Mine hard!!! Minerals are needed somewhere!!!