Jump to content

Post Request w/ Socket Connection


markvaughn2006

Recommended Posts

I'm pretty noob so please don't kill me ::)

 

I'm trying to track my websites search rank in google, I know there are others ways to do this but I really need to do it like this if possible.. so in the below example, where would i put http://google.com/ and where would i put what I'm searching for?? is google.com the host? and the search the path? If i can get the results i'm pretty sure i can extract the info i'm looking for, just not really sure about the whole socket thing...

 

THANK YOU!!!!

 

<?php

/*

** The function:

*/

 

function PostRequest($url, $referer, $_data) {

 

    // convert variables array to string:

    $data = array();   

    while(list($n,$v) = each($_data)){

        $data[] = "$n=$v";

    }   

    $data = implode('&', $data);

    // format --> test1=a&test2=b etc.

 

    // parse the given URL

    $url = parse_url($url);

    if ($url['scheme'] != 'http') {

        die('Only HTTP request are supported !');

    }

 

    // extract host and path:

    $host = $url['host'];

    $path = $url['path'];

 

    // open a socket connection on port 80

    $fp = fsockopen($host, 80);

 

    // send the request headers:

    fputs($fp, "POST $path HTTP/1.1\r\n");

    fputs($fp, "Host: $host\r\n");

    fputs($fp, "Referer: $referer\r\n");

    fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");

    fputs($fp, "Content-length: ". strlen($data) ."\r\n");

    fputs($fp, "Connection: close\r\n\r\n");

    fputs($fp, $data);

 

    $result = '';

    while(!feof($fp)) {

        // receive the results of the request

        $result .= fgets($fp, 128);

    }

 

    // close the socket connection:

    fclose($fp);

 

    // split the result header from the content

    $result = explode("\r\n\r\n", $result, 2);

 

    $header = isset($result[0]) ? $result[0] : '';

    $content = isset($result[1]) ? $result[1] : '';

 

    // return as array:

    return array($header, $content);

}

 

 

 

/*

** The example:

*/

 

// submit these variables to the server:

$data = array(

    'test' => 'foobar',

    'okay' => 'yes',

    'number' => 2

);

 

// send a request to example.com (referer = jonasjohn.de)

list($header, $content) = PostRequest(

    "http://www.example.com/",

    "http://www.jonasjohn.de/",

    $data

);

 

// print the result of the whole request:

print $content;

 

// print $header; --> prints the headers

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.