无法获得CURL输出

时间:2016-08-01 07:20:14

标签: php xml web-services curl soap

这是我的PHP代码。

<?php
$xml_post_string = '<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
 <soap12:Body>
<RequestList xmlns="http://tempuri.org/">
  <category>syncapppr</category>
  <subcategory>put_app_presc</subcategory>
  <paralist>[{"app_List":[{"server_id":"","doctorid":"1266","patientid":"19569","prescriptionid":"870472109","parentid":"","parentserverid":"","FeesDue":"0","clinicid":"1273","caseno":"KAS-5712022016514","casetype":"","reason":"","note":"","diagnosis":"","adate":"22/07/2016","atime":"05:50 AM","fee":"300","refdoctorname":"","canceledbit":"false","rescheduledbit":"false","reschedule_reason":"","recschduledid":"","cancelled_reason":""}],"doc_id":"1266"}]</paralist>
</RequestList>
</soap12:Body>
</soap12:Envelope>';

$headers = array(
"Content-type: application/soap+xml; charset=utf-8",
"Content-length: ".strlen($xml_post_string),
);
$ch = curl_init();
$fh = fopen('soap_response.xml', 'w'); 

//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, 'http://fakeurl.com/webservices/desktopservices.asmx?op=RequestList');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_TIMEOUT, 100000000);
//curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//curl_setopt($ch, CURLOPT_HEADER, 1 );
curl_setopt($ch, CURLOPT_FILE, $fh);
$response = curl_exec($ch);
//$xml = iconv("GB18030", "utf-8", $response);
$parser = simplexml_load_string(stripslashes($response));
$json = json_encode($parser);
echo($json);
curl_close($ch);
fclose($fh);
?>

当我执行我的脚本时,它会给我错误。

  

警告:simplexml_load_string():实体:第1行:解析器错误:期望开始标记,'&lt;'在第41行的C:\ xampp \ htdocs \ DsAdmin \ call_1.php中找不到

     

警告:第41行的C:\ xampp \ htdocs \ DsAdmin \ call_1.php中的simplexml_load_string():1

     

警告:第41行的C:\ xampp \ htdocs \ DsAdmin \ call_1.php中的simplexml_load_string():^

但是你可以看到我也将CURL输出保存到文件中。并在该文件中我得到正确的输出。我无法理解我的代码中出了什么问题。

我曾经在网上提过了许多解决方案,但没有任何效果,所以有人可以帮助我吗?

文件输出

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><RequestListResponse xmlns="http://tempuri.org/"><RequestListResult><syncapp_pres_list><SyncApp_Pres_Master><app_List><Appointment_Master><id>0</id><server_id>15238</server_id></Appointment_Master></app_List><startfrom>0</startfrom><endfrom>0</endfrom><clinicid>0</clinicid></SyncApp_Pres_Master></syncapp_pres_list></RequestListResult></RequestListResponse></soap:Body></soap:Envelope>

3 个答案:

答案 0 :(得分:0)

EditText et = (EditText)findViewById(R.id.et);
...
et.append("your text");

你正在关闭这行中的php标签。让它像... ...

$xml_post_string = '<?xml version="1.0" encoding="utf-8"?>

答案 1 :(得分:0)

不要在响应上运行stripslashes,它会完全破坏xml。 rawurlencode $xml_post_string CURLOPT_POSTFIELDS

答案 2 :(得分:0)

我尝试了你的代码,但无济于事。你提供的假冒无效(谢谢船长),如果没有它的输出,我不知道是什么问题。问题出在cURL中,它可能会返回TRUE而不是代码(但这就是你首先提出的问题)

我曾经遇到过没有获得cURL响应的问题并通过将CURLOPT_FOLLOWLOCATION设置为TRUE来修复它 - 没有它你将在重定向上返回NULL,而不是重定向的页面内容。

如果您向我提供cURL的工作网址,我会很高兴看到这个问题。