PHP无法通过curl调用获取数据

时间:2018-10-19 21:39:26

标签: php

我确定已经回答了这个问题,但是我只是想不出正确的问题来询问搜索,所以去了:

我有一个正在对另一个程序进行curl调用的调用程序-但是我从被调用程序中没有得到任何回报。

这是调用程序(请问一下菜鸟编码-我正在学习这个项目,正在学习中)。...

<?php
session_start();

$user = "user";
$pass = "pass";
$part = "12345";

$strConnect = "AppServer://1.2.3.4/appsrv";
$strUser = "cono91|oper=1234";

$_SESSION["part"] = $part;
$_SESSION["strConnect"] = $strConnect;
$_SESSION["strUser"] = $strUser;
$_SESSION["response"] = "init";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://5.6.7.8/test/Pricing.php');
curl_setopt($ch, CURLOPT_POST, true);
$data = array('part' => $part,'strConnect' => $strConnect,'strUser' => 
$strUser);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$r = curl_exec($ch);
curl_close($ch);

$xml = simplexml_load_string($r);

var_dump($_SESSION); /* Just returns the $_SESSION var's I set above */

?>

它在说什么:

<?php
session_start();

$part = $_SESSION['part'];
$strConnect = $_SESSION['strConnect'];
$strUser = $_SESSION['strUser'];


//$part = "341241";
//$strConnect = "AppServer://1.2.3.4/appsrv";
//$strUser = "cono=9|oper=xxx";

$soapUrl = "http://5.6.7.8/api/ApiService.asmx?op=Pricing";

$xml_post_string = '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Pricing xmlns="X.WS">
<connectString>' .$strConnect. '</connectString>
<userCode>' .$strUser. '</userCode>
<requestObject>
<customerNumber>55555</customerNumber>
<warehouse>WHSE</warehouse>
<quantity>1</quantity>
<productCode>' .$part. '</productCode>
</requestObject>
</Pricing>
</soap:Body>
</soap:Envelope>';

$headers = array(
"POST /api/ApiService.asmx HTTP/1.1",
"Host: 5.6.7.8",
"Content-Type: text/xml; charset=utf-8",
"Content-Length: ".strlen($xml_post_string),
"SOAPAction: X.WS/Pricing"
); 

$url = $soapUrl;

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);

$pos = strpos($response,"<OEPricingResult>");
$rx = substr($response,$pos,strlen($response));

$response1 = str_replace("</soap:Body>","",$rx);
$response2 = str_replace("</soap:Envelope>","",$response1);
$response3 = str_replace("</OEPricingResponse>","",$response2);

$response3 = "<xml>" .$response3. "</xml>";

$_SESSION["response"] = $response3;

var_dump($_SESSION);

print_r($response3); /* Gives me the output from the curl call this program makes


?>

因此,当程序#1执行此操作时,它将对程序#2进行surl调用-它仅返回什么。因此,我在程序2中强加了变量,使其独立运行,并获得了预期的输出(在命令行和Web调用中)。我试图以各种方式返回数据-但是几天后,我只是不知道下一个要问的问题。

那么-如何将数据从程序#2返回到程序#1?

我很高兴能对此提供帮助。...

1 个答案:

答案 0 :(得分:0)

通过curl调用的脚本不会从调用脚本继承会话变量。您正在CURLOPT_POSTFIELDS中发送这些参数,并且需要从$_POST而非$_SESSION读取它们。

因此在第二个脚本中,您应该使用:

$part = $_POST['part'];
$strConnect = $_POST['strConnect'];
$strUser = $_POST['strUser'];