2011-01-30 82 views
2

我的主機不允許的fsockopen一些代碼,但它允許捲曲。我很高興使用cli的curl,但沒有必要在PHP中使用它。我該如何使用curl來編寫它?重寫使用fsockopen使用捲曲,而不是

$ xmlrpcReq和長度早先定義。

$host = "http://rpc.pingomatic.com/"; 
$path = ""; 

$httpReq = "POST /" . $path . " HTTP/1.0\r\n"; 
$httpReq .= "User-Agent: My Lovely CMS\r\n"; 
$httpReq .= "Host: " . $host . "\r\n"; 
$httpReq .= "Content-Type: text/xml\r\n"; 
$httpReq .= "Content-length: $xmlrpcLength\r\n\r\n"; 
$httpReq .= "$xmlrpcReq\r\n"; 

if ($pinghandle = fsockopen($host, 80)) { 
    fputs($pinghandle, $httpReq); 
    while (!feof($pinghandle)) { 
     $pingresponse = fgets($pinghandle, 128); 
    } 
    fclose($pinghandle); 
} 

非常感謝!

+0

你怎麼知道你的主機不允許的fsockopen?託管服務器這種做法並不常見,因爲它會禁用所有漂亮的http庫(=不捲曲)。你收到哪條錯誤消息? (否則,我會認爲錯誤來自無效的主機:頭。) – mario 2011-01-30 21:15:31

+0

是的,我已經確認與主機,fsockopen是不允許的。垃圾! – 2011-01-30 21:22:32

回答

2

試試這個:

$curl = curl_init(); 
curl_setopt($curl, CURLOPT_USERAGENT, 'My Lovely CMS'); 
curl_setopt($curl, CURLOPT_POST, true); 
curl_setopt($curl, CURLOPT_POSTFIELDS, $xmlrpcReq); 
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: text/xml")); 
curl_setopt($curl, CURLOPT_URL, "http://rpc.pingomatic.com/"); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
$pingresponse = curl_exec($curl);