2

我有一个具有以下功能的课程。当我直接调用该函数时,它可以工作,但是如果我在循环内调用该函数,则会出现错误:Cannot connect to host. 有人可以帮助我吗?:) 提前致谢。

public function exportProfile($xml) {

        $_myRequest = curl_init("https://www.example.com/postProfile");

        curl_setopt($_myRequest, CURLOPT_POST, 1);
        curl_setopt($_myRequest, CURLOPT_USERPWD, "testUser:testPassword");
        curl_setopt($_myRequest, CURLOPT_HTTPHEADER, Array("Content-Type: application/xml"));
        curl_setopt($_myRequest, CURLOPT_POSTFIELDS,$xml );
        curl_setopt($_myRequest, CURLOPT_RETURNTRANSFER, 1);

        // do request, the response text is available in $_response
        $_response = curl_exec($_myRequest);

        $err = curl_error($_myRequest) ;

        echo $err;

        $_statusCode = curl_getinfo($_myRequest, CURLINFO_HTTP_CODE);

        // close cURL resource, and free up system resources
        curl_close($_myRequest);

        return simplexml_load_string($_response);       
    }
4

1 回答 1

0

我无法用一个简单的例子重现这个问题。您的示例代码似乎有语法错误CURLOPT_USERPWD,但我认为这不相关。

听起来很可能您正在查询的站点可能存在速率限制,即当您连续几次点击它们时,它们会将您锁定在外。

也许您可以通过尝试在循环之外多次运行查询来进行调试,即手动调用它 1/2/3 次。

于 2012-11-27T15:18:22.543 回答