PHP fsockopen 到curl 的转换

发布于 2024-09-04 02:07:11 字数 701 浏览 9 评论 0原文

我有这段代码:

<?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 have this piece of code:

<?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);?>

i want to try it out using curl but i am kinda new to curl so can anyone share some ideas?

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

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

发布评论

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

评论(2

分开我的手 2024-09-11 02:07:11

这与使用 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);      

顺便说一句,这样制作查询字符串是不安全的。您应该使用 http_build_query() 来构建它,以便正确编码。

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, it's not safe to make query string like that. You should use http_build_query() to build it so it's properly encoded.

醉酒的小男人 2024-09-11 02:07:11

我建议你使用这个脚本。这太棒了:
http://www.bin-co.com/php/scripts/load/< /a>

I suggest you to use this script. This is totally awesome:
http://www.bin-co.com/php/scripts/load/

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