devinmaking
01-30-2012, 10:49 PM
Hi guys, this is the first time i have ever done this and to be honest there is very little out there that can even help regarding what i am trying to do.
Can you guys please tell me what i am doing wrong as this is not working and as i do not know cUrl or Json this is a learning experience that is making me pull my hair out lol.
Anyway i am wanting to retrieve data from 1 website to another.
For instance, when ever someone logs in it then sends url to a website and then a message is sent back and inserted into the database.
For a guess i am not even close to what is needed.
Here is the code for the website that is sending the url out to receive the message back:
<?php
class api_connect{
public function getdata(){
$url = "www.domain.co.uk";
$token = "www.domain.co.uk";
$sendto = "http://www.domain-with-api.com/api.php";
$boundary = md5(microtime(true));
$post = "";
$post .= "--{$boundary}\r\n";
$post .= "Content-Disposition: form-data; name=\"post-data\"\r\n\r\n";
$post .= $token . "\r\n";
$post .= "{$url}\r\n";
$post .= "--{$boundary}--\r\n";
$head = "POST {$sendto} HTTP/1.1\r\n";
$head .= "HOST: {$url}\r\n";
$head .= "Content-Type: multipart/form-data; boundary=\"{$boundary}\"\r\n";
$head .= "Content-Length " . $strlen($post) . "\r\n";
$head .= "Connection: close\r\n\r\n";
$socket = fsockopen($sendto, 80);
fwrite($socket, "{$head}{$post}");
end(explode("\r\n\r\n", check_api_website($token, $url)));
json_decode(array('message' => $message,'islive' => $islive,));
$insertresults = "UPDATE usage SET message='".mysql_real_escape_string($message)."', islive='".mysql_real_escape_string($islive)."' WHERE id = 1 LIMIT 1";
$insertresults_doit = mysql_query($insertresults) OR die(mysql_error());
}
}
Here is the code for the website that is getting the url and sending the information back to the website:
<?php
function check_api_website($token, $url){
$token = trim(htmlentities($token));
$safetoken = mysql_real_escape_string($token);
$url = trim(htmlentities($url));
$safeurl = mysql_real_escape_string($url);
$checkwebsite = "SELECT message,islive FROM websitetokens WHERE url='".$safeurl."' AND token='".$safetoken."'";
$checkwebsite_result = mysql_query($checkwebsite) OR die();
$numberofrows = mysql_num_rows($checkwebsite_result);
if($numberofrows > 0){
$website = mysql_fetch_array($checkwebsite_result);
$message = stripslashes($website["message"]);
$islive = stripslashes($website["islive"]);
json_encode(array(
'message' => $message,
'islive' => $islive,
));
$date = date('Y-m-d');
$time = gmdate('H:i');
$loginwebsite = "UPDATE websitetokens SET loggedin='".$date."',time='".$time."' WHERE url='".$safeurl."' AND token='".$safetoken."'";
$loginwebsite_result = mysql_query($loginwebsite) OR die();
} else {
json_encode(array(
'message' => '',
'islive' => '1',
));
}
}
Thanks :)
Can you guys please tell me what i am doing wrong as this is not working and as i do not know cUrl or Json this is a learning experience that is making me pull my hair out lol.
Anyway i am wanting to retrieve data from 1 website to another.
For instance, when ever someone logs in it then sends url to a website and then a message is sent back and inserted into the database.
For a guess i am not even close to what is needed.
Here is the code for the website that is sending the url out to receive the message back:
<?php
class api_connect{
public function getdata(){
$url = "www.domain.co.uk";
$token = "www.domain.co.uk";
$sendto = "http://www.domain-with-api.com/api.php";
$boundary = md5(microtime(true));
$post = "";
$post .= "--{$boundary}\r\n";
$post .= "Content-Disposition: form-data; name=\"post-data\"\r\n\r\n";
$post .= $token . "\r\n";
$post .= "{$url}\r\n";
$post .= "--{$boundary}--\r\n";
$head = "POST {$sendto} HTTP/1.1\r\n";
$head .= "HOST: {$url}\r\n";
$head .= "Content-Type: multipart/form-data; boundary=\"{$boundary}\"\r\n";
$head .= "Content-Length " . $strlen($post) . "\r\n";
$head .= "Connection: close\r\n\r\n";
$socket = fsockopen($sendto, 80);
fwrite($socket, "{$head}{$post}");
end(explode("\r\n\r\n", check_api_website($token, $url)));
json_decode(array('message' => $message,'islive' => $islive,));
$insertresults = "UPDATE usage SET message='".mysql_real_escape_string($message)."', islive='".mysql_real_escape_string($islive)."' WHERE id = 1 LIMIT 1";
$insertresults_doit = mysql_query($insertresults) OR die(mysql_error());
}
}
Here is the code for the website that is getting the url and sending the information back to the website:
<?php
function check_api_website($token, $url){
$token = trim(htmlentities($token));
$safetoken = mysql_real_escape_string($token);
$url = trim(htmlentities($url));
$safeurl = mysql_real_escape_string($url);
$checkwebsite = "SELECT message,islive FROM websitetokens WHERE url='".$safeurl."' AND token='".$safetoken."'";
$checkwebsite_result = mysql_query($checkwebsite) OR die();
$numberofrows = mysql_num_rows($checkwebsite_result);
if($numberofrows > 0){
$website = mysql_fetch_array($checkwebsite_result);
$message = stripslashes($website["message"]);
$islive = stripslashes($website["islive"]);
json_encode(array(
'message' => $message,
'islive' => $islive,
));
$date = date('Y-m-d');
$time = gmdate('H:i');
$loginwebsite = "UPDATE websitetokens SET loggedin='".$date."',time='".$time."' WHERE url='".$safeurl."' AND token='".$safetoken."'";
$loginwebsite_result = mysql_query($loginwebsite) OR die();
} else {
json_encode(array(
'message' => '',
'islive' => '1',
));
}
}
Thanks :)