PHP fsockopen to curl conversion

左心房为你撑大大i 提交于 2019-11-28 11:28:41

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/2957068/php-fsockopen-to-curl-conversion

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!