简体   繁体   English

PHP无法从curl调用取回数据

[英]PHP Can't get data back from a curl call

I'm sure this has been answered, but I just can't figure out the right question to ask the search, so here goes: 我敢肯定,这已经得到了回答,但是我只是无法找出正确的问题来进行搜索,因此,这里是:

I have a calling program that's doing a curl call to another program - however I'm not getting anything back from the called program. 我有一个正在对另一个程序进行curl调用的调用程序-但是,我从被调用程序没有得到任何回报。

Here's the calling program (Please excuse the rookie coding - I'm learning as I go with this project).... 这是调用程序(请原谅新手编码-我正在学习这个项目,正在学习中)。

<?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 */

?>

And what it's calling: 它的意思是:

<?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


?>

So when program #1 does it's surl call to program #2 - it just returns nothing. 因此,当程序#1执行此操作时,将对程序#2进行surl调用-它仅返回任何内容。 So I forced in the variables in program #2 ran it stand alone and get the output I expected (in a command line and web call). 因此,我在程序2中强加了变量,使其独立运行,并获得了预期的输出(在命令行和Web调用中)。 I've tried to return the data in every way I can figure out - but after a few days of this, I just don't know the next question to ask. 我试图以各种方式返回数据-但是几天之后,我只是不知道接下来要问的问题。

So - how do I get the data from program #2 back to program #1? 那么-如何将数据从程序#2返回到程序#1?

I appreciate any help I can get on this.... 感谢您能为此提供的任何帮助。...

A script invoked through curl doesn't inherit the session variables from the calling script. 通过curl调用的脚本不会从调用脚本继承会话变量。 You're sending those parameters in the CURLOPT_POSTFIELDS , and you need to read them from $_POST , not $_SESSION . 您正在CURLOPT_POSTFIELDS发送这些参数,并且需要从$_POST而不是$_SESSION读取它们。

So in the second script, you should use: 因此,在第二个脚本中,您应该使用:

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

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

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