简体   繁体   English

如何将curl的结果分配给变量?

[英]How to assign result fro curl to a variable?

I need to do a simple POST request and parse the result. 我需要做一个简单的POST请求并解析结果。 For this i use curl in php . 为此,我在php使用curl The problem is that i cant assign the result to a variable - it just prints . 问题是我无法将结果分配给变量-它只会打印。

My method: 我的方法:

private function sendRequest($data)
{
    $ch = curl_init(self::IP . ':' . self::PORT);

    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

    $result = curl_exec($ch); // HERE IT PRINTS

    curl_close($ch);

    $parsed_result = @simplexml_load_string(trim($result)); // OR HERE IT

    die(var_dump(isset($parsed_result->request_error)));

    if (isset($parsed_result->request_error))
        $this->AJAXResult(TRUE, (string) $parsed_result->request_error->text);
}

如果要使curl_exec返回发布请求的结果,则必须添加: curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM