天天看點

php fsocketopen

好長時間沒自己寫fsockopen遠端通路了,習慣了curl的強大,查了下資料寫了段post方式代碼:

<?php

$url = 'test.fsk.com';

$fp = @fsockopen($url, 80, $errno, $errstr, 3);//遠端打開url

$str = "key1=val1&key2=val2";  //post資料

$flag = 0; //區分header和傳回資料  

$msg = '';//擷取傳回的json資料

if (!$fp) { //失敗

   echo "$errstr ($errno)<br />\n";  

} else {

   ////根據實際調用填寫url路徑和get方式參數  

   $out = "POST /controller/action?query_string HTTP/1.1\r\n";  

   $out .= "Host: " . $url . "\r\n";

   //post方式必須設定該type    

   $out .= "Content-Type: application/x-www-form-urlencoded\r\n";

   //post方式必須設定該length

   $out .= "Content-Length: " . strlen($str) . "\r\n";