View Full Version : Curl & Json - Help Needed Please :)

01-30-2012, 11: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:


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:


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"]);

'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 {

'message' => '',
'islive' => '1',



Thanks :)

01-31-2012, 12:12 AM
For a start, you're using a non cURL method for your http call. You're using sockets and hard coding it the long way.

Hit google, type in PHP cURL. Go to the php.net pages for cURL and look at the samples.

It's a library built into most php configurations that is ready to run. You select and set the options, give it the post/get data and execute it. No need for writing in your line returns, fputs/writes etc. Just create a cURL resource, set the parameters and execute.