/    Sign up×
Community /Pin to ProfileBookmark

Server-Server HTTP Communication

I don’t know much about server-server HTTP requests, but a client has suddenly revealed to me that he wants to integrate his website with the services of a company he’s working with. Basically, I needed to write a script to receive the data that this other company would send in a HTTP request.

This is what I created by modifying the sample code they provided me:

[CODE]<?php
include(‘../../scripts/data.php’);

function getValidateRequest ($s_req_ref, $s_req_trg, $s_req_shc, $s_req_pho, $s_req_con, $s_req_cam,$s_req_chn, $s_req_pay, $s_req_dat) {
$product = explode($s_req_con);
if (dbquery(‘INSERT INTO order(Reference,Channel,Trigger,Shortcode,MSISDN,Product,Content,DataType,DateReceived,CampaignID,Status) VALUES(“‘ . $s_req_ref . ‘”,”‘ . $s_req_chn . ‘”,”‘ . $s_req_trg . ‘”,”‘ . $s_req_shc . ‘”,”‘ . $s_req_pho . ‘”,”‘ . $product[0] . ‘”,”‘ . $s_req_con . ‘”,”‘ . $s_req_pay . ‘”,”‘ . $s_req_dat . ‘”,”‘ . $s_req_cam . ‘”,”Pending”‘)’))
return(true);
else
return(false);
}

$s_ref = $HTTP_POST_VARS[“Reference”]; // Reference
$s_trg = $HTTP_POST_VARS[“Trigger”]; // trigger
$s_shc = $HTTP_POST_VARS[“Shortcode”]; // shortcode
$s_pho = $HTTP_POST_VARS[“MSISDN”]; // MSISDN
$s_con = $HTTP_POST_VARS[“Content”]; // Content
$s_chn = $HTTP_POST_VARS[“Channel”]; // Channel
$s_pay = $HTTP_POST_VARS[“DataType”]; // Data Type
$s_dat = $HTTP_POST_VARS[“DateReceived”]; // Date Received
$s_cam = $HTTP_POST_VARS[“CampaignID”]; // CampaignID
$b_IsValid = getValidateRequest($s_ref, $s_trg, $s_shc, $s_pho, $s_con, $s_cam, $s_chn, $s_pay, $s_dat);
if ($b_IsValid) {
$s_ResponseCode = “success”;
}
else {
$s_ResponseCode = “fail”;
}
echo $s_ResponseCode;

$products = @dbquery(‘SELECT * FROM product WHERE Code = “‘ . $product[0] . ‘”‘);
$product = @mysql_fetch_assoc($products);
$stock = $product[‘Stock’] – 1;
$sales = $product[‘Sales’] + 1;
@dbquery(‘UPDATE product SET Stock = ‘ . $stock . ‘, Sales = ‘ . $sales . ‘ WHERE Code = “‘ . $product[‘Code’] . ‘”‘);
@mail(‘[email protected]’,’Order Pending…’,$s_con);
?>[/CODE]

I uploaded this to [url]www.thedomainname.com/admin/orders/input.php[/url]

Encase you wonder what dbquery is, it’s a function I created just to make my life simpler, it basically connects to the database server, selects the database, and runs the query which I supply as a parameter.

Then I wanted to test that the script would actually work, so I adapted another sample code that they supplied me with, and created this:

[CODE]<?php
$X_HOST =”www.thedomainname.com”;
$X_URL = “admin/orders/input.php”;
$X_PORT =”8080″;
$X_USERNAME = “foo”;
$X_PASSWORD = “bar”;
$s_POST_DATA = “Reference=FFERGRD”; // Channel
$s_POST_DATA .= “&Trigger=12345”; // Shortcode
$s_POST_DATA .= “&Shortcode=12345”; // Shortcode
$s_POST_DATA .= “&MSISDN=353868111111”; // Phone
$s_POST_DATA .= “&Content=test”; // Content
$s_POST_DATA .= “&Channel=UK.Vodaphone”; // Shortcode
$s_POST_DATA .= “&DataType=0”; // Data Type
$s_POST_DATA .= “&DateReceived=” . time(); // Premium
$s_POST_DATA .= “&CampaignID=4321”; // CampaignID
$s_Request = “POST “.$X_URL.” HTTP/1.0n”;
$s_Request .=”Host: “.$X_HOST.”:”.$X_PORT.”n”;
$s_Request .=”Authorization: Basic “.base64_encode($X_USERNAME.”:”.$X_PASSWORD).”n”;
$s_Request .=”Content-Type: application/x-www-form-urlencodedn”;
$s_Request .=”Content-Length: “.strlen($s_POST_DATA).”n”;
$s_Request .=”n”.$s_POST_DATA.”nn”;
//Sends out the request to the server.
$s_GatewayResponse=”;
$fp = fsockopen ($X_HOST, $X_PORT, $errno, $errstr, 30) or die(“Error!!!”);
fputs ($fp, $s_Request);
while (!feof($fp)) {
$s_GatewayResponse .= fgets ($fp, 128);
}
fclose ($fp);
//Array of official response codes.
$a_Responses = array(
“success” => “Server has received successfully.”,
“fail” => “Server did not recieve.”,
);
echo “<HTML>n<BODY>nn”;
//Checks for an official response code.
$b_CodeReturned=false;
foreach ($a_Responses as $s_ResponseCode => $s_ResponseDescription) {
if (stristr($s_GatewayResponse, “n$s_ResponseCoden”)) {
echo “A response code of $s_ResponseCode was returned – “;
echo $s_ResponseDescription;
$b_CodeReturned = true;
}
else
echo $s_GatewayResponse;
}
//Checks for an authorization failure where an official response code has
//not been recognized.
if (!$b_CodeReturned) {
if (stristr($s_GatewayResponse, “HTTP/1.1 401”)) {
echo “The server rejected your username/password (HTTP 401).”;
} else {
echo “No recognised response code was returned by the server.”;
}
}
echo “nn</BODY>n</HTML>”;
?>[/CODE]

When I run this script I get the error:

HTTP/1.1 400 Invalid URI Server: Apache-Coyote/1.1 Content-Length: 0 Date: Wed, 03 Feb 2010 23:33:11 GMT Connection: close

Can anyone suggest where I’ve gone wrong?

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogFeb 04.2010 — 8080 seems like an abnormal port number. Could that just be a typo for the usual port 80 for HTTP?
Copy linkTweet thisAlerts:
@JTweedieauthorFeb 04.2010 — Well I didn't have a clue which port to use, so I left the port number in that was in the sample code. I just tried it with port 80, but that comes up with a port error, so it can't be port 80, the fact that I didn't get a port error before suggests that the port number 8080 is actually working, it's something else causing the problem.
×

Success!

Help @JTweedie spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...