如何解析Curl响应?

时间:2012-12-27 06:35:49

标签: php curl

我可以发送GET请求并在以下行收到回复。

 $curl_resp = curl_exec($curl);

我使用以下内容来解析响应,但它不起作用,我已手动将某些值设置为$curl_resp,但仍不确定如何分别访问xml的每个标记的值

$xml = simplexml_load_string($curl_resp);

注意:我收到了实际的xml,但无法解析它,(我需要在变量中单独获取每个标记的值

代码:

<?php

   $service_url = ' The Url goes here';
   $curl = curl_init($service_url);
   $curl_post_data = array(
        "PASSWORD" => 'pass',
        "USERNAME" => 'username'
        );
   curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
   curl_setopt($curl, CURLOPT_POST, true);
   curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
   $curl_resp = curl_exec($curl);
   curl_close($curl);

3 个答案:

答案 0 :(得分:2)

您的变量$curl_response$curl_resp不同(您要解析的内容

您可以像访问任何其他数组一样访问每个标记的值。

答案 1 :(得分:1)

如果未设置CURLOPT_RETURNTRANSFER选项,则您的$curl_resp将返回true/false

如果已设置,则可能会返回false或格式不正确的xml string。如果您发布更多代码或实际的卷曲响应文本,我们可能会提供更多信息。

编辑:

阅读代码后,您似乎将响应文本分配给$ curl_response而不是$ curl_resp

答案 2 :(得分:-1)

$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, "your url");    
curl_setopt($ch, CURLOPT_USERAGENT, 'Googlebot/2.1 (+http://www.google.com/bot.html)');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);