PHP fsockopen进行curl转换 [英] PHP fsockopen to curl conversion
本文介绍了PHP fsockopen进行curl转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这段代码:
<?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尝试,但我有点新的curl,所以任何人都可以分享一些想法?
i want to try it out using curl but i am kinda new to curl so can anyone share some ideas?
推荐答案
这是等效的使用cURL,
This is the equivalent using cURL,
$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);
BTW,这样的查询字符串是不安全的。您应该使用 http_build_query()
来构建它,以便正确编码。
BTW, it's not safe to make query string like that. You should use http_build_query()
to build it so it's properly encoded.
这篇关于PHP fsockopen进行curl转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文