Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Oct 2011
    Posts
    236
    Thanks
    11
    Thanked 5 Times in 5 Posts

    Curl & Json - Help Needed Please :)

    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 Code:
    <?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($sendto80);
            
            
    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 Code:
    <?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

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,089
    Thanks
    51
    Thanked 506 Times in 493 Posts
    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.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •