将CURL转换为PHP中的fosckopen
问题描述:
我正在为一个正在运行的站点设置信用卡处理。 PHP没有使用CURL支持进行编译,我不想让该站点重新编译PHP,所以我尝试使用与示例代码不同的方法。将CURL转换为PHP中的fosckopen
例卷曲代码:
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或其他方法不是很熟悉,所以我不确定我做错了什么。
答
虽然你特别要求fsockopen()你可能也想尝试stream api和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);
}
,如果你需要更多的错误处理,而不是file_get_contents()
答
使用fopen()和stream_get_meta_data()我试图修改后的版本您的解决方案 - 一旦一切似乎都奏效,我发现多个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);
谢谢!这工作完美。 – 2009-12-29 01:21:13
嗯,实际上这不适用于HTTPS,有没有办法使这项工作? – 2009-12-29 16:18:40
哦https。是的......但你需要一个提供https的ssl部分的模块。这就是你想避免的:重新启动网络服务器,以便让php加载另一个模块。大多数“标准”版本的php使用openssl-extension,http://docs.php.net/openssl,但它也可以是... taadaa ...卷曲扩展。我有点怀疑你会发现ssl传输层的生产级“纯”php实现。可能是错误的。 – VolkerK 2009-12-29 17:38:39