将fsockopen转换为CURL PHP

时间:2015-09-27 23:41:58

标签: php curl fsockopen

我有一段执行fsockopen

的代码
      $url = 'http://abcd.com/def';
      $post_params[0] = 'eids='.urlencode($email);
      $post_params[1] = 'mhead='.urlencode($head);
      $post_params[2] = 'mcontent='.urlencode($content);
      $post_string = implode('&', $post_params);
      $parts=parse_url($url);
      $fp = fsockopen($parts['host'],isset($parts['port'])?$parts['port']:80,$errno, $errstr, 30);
      $out = "POST ".$parts['path']." HTTP/1.1\r\n";
      $out.= "Host: ".$parts['host']."\r\n";
      $out.= "Content-Type: application/x-www-form-urlencoded\r\n";
      $out.= "Content-Length: ".strlen($post_string)."\r\n";
      $out.= "Connection: Close\r\n\r\n";
      if (isset($post_string)) $out.= $post_string;
      fwrite($fp, $out);
      fclose($fp);
      return 1;

我试图将其转换为cURL,但无法取得成功

这就是我做的事情

        $ch = curl_init(); 
        curl_setopt($ch, CURLOPT_URL, "http://abcd.com/def");
        curl_setopt($ch, CURLOPT_POSTFIELDS, array(
                'eids' => urlencode($email),
                'mhead' => urlencode($head),
                'mcontent' => urlencode($content)
            ));
        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);   

        return $response;

有什么想法吗?

0 个答案:

没有答案