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
https://forums.phpfreaks.com/topic/189163-post-request-w-socket-connection/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.