在 PHP 中将 CURL 转换为 fosckopen

发布于 2024-08-16 09:37:26 字数 1913 浏览 8 评论 0原文

我正在为一个实时网站设置信用卡处理。 PHP 没有使用 CURL 支持进行编译,我不想关闭该网站来重新编译 PHP,因此我尝试使用与提供的示例代码不同的方法。

示例 CURL 代码:

function send($packet, $url) {
    $header = array("MIME-Version: 1.0","Content-type: application/x-www-form-urlencoded","Contenttransfer-encoding: text"); 
    $ch = curl_init();

    // set URL and other appropriate options 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_VERBOSE, 1); 
    curl_setopt ($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); 
    // Uncomment for host with proxy server
    // curl_setopt ($ch, CURLOPT_PROXY, "http://proxyaddress:port"); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $packet); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt ($ch, CURLOPT_TIMEOUT, 10); 

    // send packet and receive response
    $response = curl_exec($ch); 
    curl_close($ch); 
    return($response);
}

我尝试使用不同的方法:

function send($packet, $host, $path) {
    $content = '';
    $flag = false;
    $post_query = urlencode($packet) . "\r\n";
    $fp = fsockopen($host, '80');
    if ($fp) {
        fputs($fp, "POST $path HTTP/1.0\r\n");
        fputs($fp, "Host: $host\r\n");
        fputs($fp, "Content-length: ". strlen($post_query) ."\r\n\r\n");
        fputs($fp, $post_query);

        while (!feof($fp)) {
            $line = fgets($fp, 10240);
            if ($flag) {
                $content .= $line;
            } else {
                $headers .= $line;
                if (strlen(trim($line)) == 0) {
                    $flag = true;
                }
            }
        }
        fclose($fp);
    }

    return $content;
}

虽然这实际上确实从我正在调用的 URL 中返回了一个结果,但它的返回结果是错误的,因为我收到一条错误消息,指出它的格式不正确。我对 CURL 或其他方法都不是很熟悉,所以我不确定我做错了什么。

I am working on setting up credit card processing for a site that is live. PHP wasn't compiled with CURL support and I don't want to take the site down to recompile PHP with that, so I am trying to use a different method than the example code provided.

Example CURL code:

function send($packet, $url) {
    $header = array("MIME-Version: 1.0","Content-type: application/x-www-form-urlencoded","Contenttransfer-encoding: text"); 
    $ch = curl_init();

    // set URL and other appropriate options 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_VERBOSE, 1); 
    curl_setopt ($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); 
    // Uncomment for host with proxy server
    // curl_setopt ($ch, CURLOPT_PROXY, "http://proxyaddress:port"); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $packet); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt ($ch, CURLOPT_TIMEOUT, 10); 

    // send packet and receive response
    $response = curl_exec($ch); 
    curl_close($ch); 
    return($response);
}

My attempt to use a different method:

function send($packet, $host, $path) {
    $content = '';
    $flag = false;
    $post_query = urlencode($packet) . "\r\n";
    $fp = fsockopen($host, '80');
    if ($fp) {
        fputs($fp, "POST $path HTTP/1.0\r\n");
        fputs($fp, "Host: $host\r\n");
        fputs($fp, "Content-length: ". strlen($post_query) ."\r\n\r\n");
        fputs($fp, $post_query);

        while (!feof($fp)) {
            $line = fgets($fp, 10240);
            if ($flag) {
                $content .= $line;
            } else {
                $headers .= $line;
                if (strlen(trim($line)) == 0) {
                    $flag = true;
                }
            }
        }
        fclose($fp);
    }

    return $content;
}

While this does actually give me a return from the URL I am calling, it does so incorrectly because I get an error back saying it was formatted incorrectly. I am not very familiar with either CURL or this other method, so I am not sure what I am doing wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吻风 2024-08-23 09:37:26

我尝试了您的解决方案的修改版本 - 一旦一切似乎都正常工作,我发现多个 fputs() 导致从目标服务器返回间歇性 HTTP 505 错误。

为了解决这个问题,我必须预先构建完整的请求,然后只执行一个 fputs 调用,如下所示

$post_vars  = "";

$post_vars .= "&somekey=somevalue";

$header = "";

$header .= "POST /my.php HTTP/1.0\r\n";

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

$header .= "Host: www.myhost.com \r\n";

$header .= "Content-Length: " . strlen($post_vars) . "\r\n\r\n";

$fp = "";

@$fp = fsockopen ('ssl://www.myhost.com', 443, $errno, $errstr,30);

fputs($fp, $header . $post_vars);

while(!feof($fp)) {

    // Read the data returned

    $response .= @fgets($fp, 4096);

}

fclose ($fp);

I tried a modified version of your solution - once everything seemed to be working, I found the multiple fputs() resulted in an intermittent HTTP 505 error coming back from the target server.

To fix this, I had to pre-construct the full request and just do one fputs call like this

$post_vars  = "";

$post_vars .= "&somekey=somevalue";

$header = "";

$header .= "POST /my.php HTTP/1.0\r\n";

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

$header .= "Host: www.myhost.com \r\n";

$header .= "Content-Length: " . strlen($post_vars) . "\r\n\r\n";

$fp = "";

@$fp = fsockopen ('ssl://www.myhost.com', 443, $errno, $errstr,30);

fputs($fp, $header . $post_vars);

while(!feof($fp)) {

    // Read the data returned

    $response .= @fgets($fp, 4096);

}

fclose ($fp);
浮萍、无处依 2024-08-23 09:37:26

尽管您特别要求 fsockopen() 您可能还想尝试 流 api上下文options

function send($packet, $url) {
  $ctx = stream_context_create(
    array(
      'http'=>array(
        'header'=>"Content-type: application/x-www-form-urlencoded",
        'method'=>'POST',
        'content'=>$packet
      )
    )
  );
  return file_get_contents($url, 0, $ctx);
}

如果您需要更多错误处理,请使用 fopen()stream_get_meta_data() 而不是 file_get_contents()

Though you specifically asked for fsockopen() you might also want to try the stream api and context options

function send($packet, $url) {
  $ctx = stream_context_create(
    array(
      'http'=>array(
        'header'=>"Content-type: application/x-www-form-urlencoded",
        'method'=>'POST',
        'content'=>$packet
      )
    )
  );
  return file_get_contents($url, 0, $ctx);
}

if you need more error handling use fopen() and stream_get_meta_data() instead of file_get_contents()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文