Forum Moderators: coopster

Message Too Old, No Replies

php post - curl works while sockets don't?

         

ZviJ

7:24 am on Jun 14, 2005 (gmt 0)

10+ Year Member



Hi All,

Theoretically I am trying to do a fairly simple thing. post data to a form. CURL handles this task. However when I try to use the same data and query the same form with the same data but use http_post (from source code here, which is using sockets) I get the error 302 file moved.

Basically I am trying ot access daily stats on google adsense.
The code using curl is I guess quite widespread on the web...

$destination="/adsense/reports-aggregate?product=afc&dateRange.dateRangeType=simple&dateRange.simpleDate=thisweek&csv=true";
$postdata="destination=".urlencode($destination)."&username=".urlencode($username)."&password=".urlencode($password)."&null=Login";

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL,"https://www.google.com/adsense/login.do");
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8b)");
curl_setopt ($ch, CURLOPT_TIMEOUT, 20);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec ($ch);
curl_close($ch);

I've tried to use this:


$destination="/adsense/reports-aggregate?product=afc&dateRange.dateRangeType=simple&dateRange.simpleDate=thisweek&csv=true%22;";
$postdata = array("destination" => $destination, "username" => $username, "password" => $password , "null" => "Login");

$result = http_post("64.233.179.99",
80,
"https://www.google.com/adsense/login.do",
null,
$postdata
);

function http_post($server /**< Server's or proxie's host name or IP.*/,
$port, /**< Server's or proxie's port */
$url, /**< Should point to the URL on the server or full address if a proxy is used. */
$cookie, /**< The optional cookie string. */
$vars /**< Associative array of POST variables. Each key is the name of the variable and the value
is the variable's content */
)
{
$user_agent = "Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)";

$urlencoded = "";
while (list($key,$value) = each($vars))
$urlencoded.= urlencode($key) . "=" . urlencode($value) . "&";
$urlencoded = substr($urlencoded,0,-1);

$content_length = strlen($urlencoded);

$headers = "POST $url HTTP/1.1\r\nHost: $server\r\nAccept: */*\r\nAcceptLanguage: en-au\r\nConnection: close\r\nContent-Type: application/x-www-form-urlencoded\r\nUser-Agent: $user_agent\r\nCache-Control: no-cache\r\nCookie: {$cookie}\r\nContent-Length: $content_length\r\n\r\n";

$fp = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

if (!socket_connect($fp, $server, $port))
{
socket_close($fp);
return false;
}

socket_set_option($fp, SOL_SOCKET, SO_KEEPALIVE, TRUE);
socket_send($fp, $headers, strlen($headers), 0);
socket_send($fp, $urlencoded, strlen($urlencoded), 0);

$ret = "";
while ($rd = socket_read($fp, 1024))
{
$ret .= $rd;
}

socket_close($fp);

$pos = strpos($ret, "\r\n\r\n");

if (!is_bool($pos))
{
$ret = substr($ret, $pos);
}

return $ret;

}

And the result is 302, file moved here and that here points to the same [google.com...]
Any ideas what might be going wrong?

Romeo

9:24 am on Jun 14, 2005 (gmt 0)

10+ Year Member



Hi ZviJ,

you try a

$result = http_post("64.233.179.99",
80,
"https://www.google.com/adsense/login.do",
null,
$postdata
);

Port 80 is the http service port, but the adsense stuff is behind a https server, so google is right in redirecting you the their https service URL.
The https (ssl) is another service, sitting on another server port, by default on port 443.

Just changing your function call from port 80 to port 443 would most likely not work. Your function http_post would have to speak "https" protocol in this case, too, with ssl-session-key negotiation and ssl data stream encryption.
The curl obviously does this ssl stuff well.
Try to get a https_ function somewhere.

Regards,
R.

ZviJ

6:28 pm on Jun 14, 2005 (gmt 0)

10+ Year Member



Thanks.
U were right on both counts. THOugh changing to 443 provided more info and different error. I guess I'll have to dig https :)