在 PHP 中将 CURL 转换为 fosckopen
我正在为一个实时网站设置信用卡处理。 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我尝试了您的解决方案的修改版本 - 一旦一切似乎都正常工作,我发现多个 fputs() 导致从目标服务器返回间歇性 HTTP 505 错误。
为了解决这个问题,我必须预先构建完整的请求,然后只执行一个 fputs 调用,如下所示
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
尽管您特别要求 fsockopen() 您可能还想尝试 流 api 和 上下文options
如果您需要更多错误处理,请使用 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
if you need more error handling use fopen() and stream_get_meta_data() instead of file_get_contents()