Building sending stock-trading php system

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

jorgeng
Forum Commoner
Posts: 43
Joined: Mon Aug 04, 2008 8:05 am

Building sending stock-trading php system

Post by jorgeng »

Hello!

I am trying to build a php system which will send transactions to
a server on a financial firm (receiver). The firm will then return
status of done transactions to me.

I have been told to do this in php from the firm and have got some
coding-examples. My problem is to do the coding in php since i don't have
much experience in php.

As i see it i have to build a loop which sends data every 10 second,
read the answers from the firm.

The code i have managed to build so far and which doesn't work:

Can someone help me to get the code working?

I have exluded correct ip-address due to restrictions from the firm:

Code: Select all

 
<?php
$fp = fsockopen("tcp://xx.xx.xx.xx", xxxx, $errno, $errstr);
if (!$fp) {
    echo "ERROR: $errno - $errstr<br />\n";
} else {
    $szData = '<?xml version="1.0" encoding="Windows-1252" ?> 
                <Signal xmlns="http://b.se/TESSignal.xsd"> 
                <Version>1.0</Version> 
                <SignalType>Entry</SignalType>  
                <PositionType>Long</PositionType>  
                <Instrument>OMXS30-8LT</Instrument> 
                <Strategy>RankorTest</Strategy>                                 <Price>889</Price> 
                <Weight>1</Weight> 
                <Comment>RankorTest</Comment> 
                </Signal>'; 
                
    fwrite($fp, $szData);
    print $szData;
    print("XML-sträng skickad ");
    
//  Skickar frågor på port xxxx.
    $fp2 = fsockopen("tcp://xx.xx.xx.xx", xxxx, $errno, $errstr);
    $szData2 = 'info;RankorTest'; 
    fwrite($fp2, $szData2 );
    print $szData2;
    print("Fråge-sträng skickad på port xxxx");
    
//  $buffer hamnar allt som mottagits ifrån servern 
//  $fp innehåller din fsockopen connection 
    while(true) 
    {  
        $str = fread($fp2, 1);      
        $buffer .= $str; 
        print $buffer;
        if($str == "\n") 
        { 
            break; 
        }     
    } 
    
//    fwrite($fp, "\n");
//    echo fread($fp, 26);
    fclose($fp);
}
?>
Last edited by Benjamin on Mon Aug 10, 2009 5:02 am, edited 1 time in total.
Reason: Added [code=php] tags.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Building sending stock-trading php system

Post by Eric! »

jorgeng wrote: The code i have managed to build so far and which doesn't work:

Can someone help me to get the code working?
Can you clue us in to what does not work? I can see you do not have any loops in there for time. But do the socket functions work?
Last edited by Eric! on Wed Jul 08, 2009 4:38 pm, edited 1 time in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Building sending stock-trading php system

Post by requinix »

jorgeng wrote:The code i have managed to build so far and which doesn't work:
Doesn't work how?
jorgeng
Forum Commoner
Posts: 43
Joined: Mon Aug 04, 2008 8:05 am

Re: Building sending stock-trading php system

Post by jorgeng »

I got error:

Warning: fsockopen() [function.fsockopen]: unable to connect to tcp://xx.xx.xx.xx:xxxx (Anslutningsförsöket misslyckades eftersom den anslutna datorn inte svarade inom en viss tid eller på grund av att den etablerade anslutningen till värddatorn inte längre fungerar. ) in C:\Program Files\xampp\htdocs\RankorSystem_TES_6.php on line 2
ERROR: 10060 - Anslutningsförsöket misslyckades eftersom den anslutna datorn inte svarade inom en viss tid eller på grund av att den etablerade anslutningen till värddatorn inte längre fungerar.
SvanteH
Forum Commoner
Posts: 50
Joined: Wed Jul 08, 2009 12:25 am

Re: Building sending stock-trading php system

Post by SvanteH »

jorgeng wrote:I got error:

Warning: fsockopen() [function.fsockopen]: unable to connect to tcp://xx.xx.xx.xx:xxxx (Anslutningsförsöket misslyckades eftersom den anslutna datorn inte svarade inom en viss tid eller på grund av att den etablerade anslutningen till värddatorn inte längre fungerar. ) in C:\Program Files\xampp\htdocs\RankorSystem_TES_6.php on line 2
ERROR: 10060 - Anslutningsförsöket misslyckades eftersom den anslutna datorn inte svarade inom en viss tid eller på grund av att den etablerade anslutningen till värddatorn inte längre fungerar.
Translation from Swedish:
Connection timed out. A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

Are you sure that it's the right IP-address you're connecting to?
Can you check with your firewall and see whether it's blocking outbound connections or not?
jorgeng
Forum Commoner
Posts: 43
Joined: Mon Aug 04, 2008 8:05 am

Re: Building sending stock-trading php system

Post by jorgeng »

SvanteH wrote:
jorgeng wrote:I got error:

Warning: fsockopen() [function.fsockopen]: unable to connect to tcp://xx.xx.xx.xx:xxxx (Anslutningsförsöket misslyckades eftersom den anslutna datorn inte svarade inom en viss tid eller på grund av att den etablerade anslutningen till värddatorn inte längre fungerar. ) in C:\Program Files\xampp\htdocs\RankorSystem_TES_6.php on line 2
ERROR: 10060 - Anslutningsförsöket misslyckades eftersom den anslutna datorn inte svarade inom en viss tid eller på grund av att den etablerade anslutningen till värddatorn inte längre fungerar.
Translation from Swedish:
Connection timed out. A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

Are you sure that it's the right IP-address you're connecting to?
Can you check with your firewall and see whether it's blocking outbound connections or not?
Hmm, how to check outbound connections?
I have windows vista firewall.

:roll:
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Building sending stock-trading php system

Post by Eric! »

The easiest way is to turn it off and try the script again. But I have to ask, where is the server that is running the script? Are you testing this on your own local windows server?
jorgeng
Forum Commoner
Posts: 43
Joined: Mon Aug 04, 2008 8:05 am

Re: Building sending stock-trading php system

Post by jorgeng »

Eric! wrote:The easiest way is to turn it off and try the script again. But I have to ask, where is the server that is running the script? Are you testing this on your own local windows server?
Yes, i runt the code in my wamp in internet explorer.
I think that the php code connects to ip xx.xx.xx on port xxxx, is this correct?

I want to connect to the ip xx.xx.xx on port xxxx ant thought fsockopen did that.

:roll:
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Building sending stock-trading php system

Post by Eric! »

Not if your firewall blocks it.
jorgeng
Forum Commoner
Posts: 43
Joined: Mon Aug 04, 2008 8:05 am

Re: Building sending stock-trading php system

Post by jorgeng »

Eric! wrote:Not if your firewall blocks it.
I have added the two ports in windows vista firewall with tcp but no success.
I added to own list the ip-address, still no success.

I am stucked so far on this code, after this code i got the error message:

Code: Select all

 
<?php
$fp = fsockopen("tcp://xx.xx.xx.xx", xxxx, $errno, $errstr);
if (!$fp) {
    echo "ERROR: $errno - $errstr<br />\n";
} else {
    $szData = '<?xml version="1.0" encoding="Windows-1252" ?> 
                <Signal xmlns="http://b.se/TESSignal.xsd"> 
                <Version>1.0</Version> 
                <SignalType>Entry</SignalType>  
                <PositionType>Long</PositionType>  
                <Instrument>OMXS30-8LT</Instrument> 
                <Strategy>RankorTest</Strategy>                                 <Price>889</Price> 
                <Weight>1</Weight> 
                <Comment>RankorTest</Comment> 
                </Signal>'; 
                
    fwrite($fp, $szData);
    print $szData;
    print("XML-sträng skickad på port xxxx");
Last edited by Benjamin on Mon Aug 10, 2009 5:03 am, edited 1 time in total.
Reason: Added [code=php] tags.
SvanteH
Forum Commoner
Posts: 50
Joined: Wed Jul 08, 2009 12:25 am

Re: Building sending stock-trading php system

Post by SvanteH »

Try turning the firewall off completely then try.
jorgeng
Forum Commoner
Posts: 43
Joined: Mon Aug 04, 2008 8:05 am

Re: Building sending stock-trading php system

Post by jorgeng »

SvanteH wrote:Try turning the firewall off completely then try.
I turned off firewall, but still same result, seems that something is wrong.

Is this the right command to login to a receiving site and transmit data?
:?
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Building sending stock-trading php system

Post by Eric! »

The host IP could be blocking you too.

Try to telnet to the ip:port that your fsocket is trying to open and see if you can connect that way.
jorgeng
Forum Commoner
Posts: 43
Joined: Mon Aug 04, 2008 8:05 am

Re: Building sending stock-trading php system

Post by jorgeng »

Eric! wrote:The host IP could be blocking you too.

Try to telnet to the ip:port that your fsocket is trying to open and see if you can connect that way.
Well, i downloaded easyterm:
http://www.arachnoid.com/easyterm/
installed and connected to ip-address and the port-number and then easyterm says
"Disconnected".

I assume this is a progress, what do you think?
:?
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Building sending stock-trading php system

Post by Eric! »

I don't know easyterm. You must be using vista and got screwed by the lack of dos and telnet.

It sounds like that port is blocked to me. You should get something like

Code: Select all

c:>telnet http://www.example.com 80 <CR>
Connecting to http://www.example.com....
 
[at this point the screen will go blank (wait for this to happen) and anything you type will not be echoed back]
 
HEAD / HTTP/1.1<CR>
Host: http://www.example.com<CR>
<CR>
When it doesn't work you should get something like Connect failed.

Code: Select all

C:> telnet 60.92.12.56 80 <CR>
Connecting To 60.92.12.56...Could not open connection to the host, on port 80: Connect failed.
Talk to the admin running the host you are trying to connect to and tell them what you are trying to do. Perhaps they are using a different port or only accepting SSL connections on XX port or something.....
Post Reply