2010-06-02 117 views
1

我有這段代碼:PHP的fsockopen捲曲轉換

<?php $host = "registration.mypengo.com"; 
$request = "/webregistration.aspx?taskaction=serviceresponse&partner=157&subid=" . $subid . "&msisdn=" . $msisdn . "&type=TEXT&data=" . $data . "&serviceid=" . $service_id; 
$fp = fsockopen($host, 80, $errno, $errstr, 3.0); 

if($fp) 
{ 
    fwrite($fp, 
    "GET $request HTTP/1.0\r\n" . 
    "Host: $host\r\n". 
    "Connection: close\r\n". 
    "Content-Length: " . strlen($request) . "\r\n" . 
    "\r\n" . 
    $request); 

    stream_set_timeout($fp, 2, 0); 
    $response = ""; 

    while(!feof($fp)) 
    $response .= fread($fp, 1024); 

    fclose($fp);?> 

我想嘗試一下使用curl,但我還挺新捲曲因此任何人都可以分享一些想法?

回答

2

這是使用捲曲相當於

$ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, "http://" . $host . $request); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close')); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 2); 
    $response = curl_exec($ch); 
    curl_close($ch);  

順便說一句,這不是安全作出查詢字符串這樣。您應該使用http_build_query()來構建它,以便正確編碼。