dongpan9760
2014-08-26 04:49
浏览 1.0k
已采纳

如何将cURL结果传递给变量

Hi all I need help regarding cURL, I'm new to using cURL in PHP so I want to ask this.

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $get_url);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$variable = curl_exec($curl);

$variable's value is boolean while cURL echoes a string during execution. Is there anyway I could get that echoed string pass to a variable?

Thanks in advance

图片转代码服务由CSDN问答提供 功能建议

大家好我需要有关cURL的帮助,我是新手在PHP中使用cURL所以我想问这个。

  $ curl = curl_init(); 
curl_setopt($ curl,CURLOPT_URL,$ get_url); 
curl_setopt($ curl,CURLOPT_USERAGENT,'Mozilla / 4.0(兼容; MSIE 6.0)  ; Windows NT 5.1; SV1)'); 
curl_setopt($ curl,CURLOPT_SSL_VERIFYPEER,false); 
 $ variable = curl_exec($ curl); 
   
 
 

$ 变量的值是boolean,而cURL在执行期间回显一个字符串。 无论如何我可以将回显的字符串传递给变量吗?

提前致谢

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongsaolian8786 2014-08-26 04:53
    已采纳

    Set the option curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); to return the result as a string, in your case the string will be stored in $variable. Then, pass $variable to whatever function you desire.

    已采纳该答案
    打赏 评论
  • dsnrixf6765 2014-08-26 04:55

    Another solution is this way:

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $get_url);
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)');
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    
    ob_start();
    curl_exec($curl);
    $variable = ob_get_clean();
    
    打赏 评论

相关推荐 更多相似问题