如何获取数组值作为 curl php 请求的返回值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13665532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get array value as return value on curl php request?
提问by user1371033
I am having hard time using curl PHP as I am new to PHP. The problem is that I am not getting any return value from a curl request. There is a remote file I am accessing which has this code:
因为我是 PHP 新手,所以我很难使用 curl PHP。问题是我没有从 curl 请求中获得任何返回值。我正在访问一个远程文件,其中包含以下代码:
test.php:
测试.php:
$test->getCall();
public function getCall() {
$var = array('fname'=>'Hyman','lname'=>'williams');
return $var;
}
The script from which I am making the call.
我正在调用的脚本。
requestVal.php
请求值
try{
$ch = curl_init();
if (FALSE === $ch){
throw new Exception('failed to initialize');
}
curl_setopt($ch, CURLOPT_URL,"http://www.xyz.com/app/src/test.php");
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $msg);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$p_result = curl_exec($ch);
var_dump($p_result);
print_r($p_result);
if (FALSE === $p_result) {
throw new Exception(curl_error(), curl_errno());
curl_close($ch);
} else{
curl_close($ch);
return $p_result;
}
}catch(Exception $e) {
trigger_error(sprintf('Curl failed with error #%d: %s',$e->getCode(), $e->getMessage()),E_USER_ERROR);
}
I don't get any value in $p_resultnor an exception curl_error().
我没有得到任何价值,$p_result也没有例外curl_error()。
回答by Ranty
No matter what you echo in your test.php, curl will read it as a String. In your case, you echo nothing. You call a function that returns the array, but you don't print the array, so nothing is sent to output. If you want to get the same array in requestVal.phpafter curl request, you need to encode it in some way, I would suggest JSON as it's easy to start with. For a quick example, in place of $test->getCall();you could do:
无论你在你test.php的String. 在你的情况下,你什么都不回。您调用了一个返回数组的函数,但您不打印该数组,因此不会将任何内容发送到输出。如果您想requestVal.php在 curl 请求之后获得相同的数组,则需要以某种方式对其进行编码,我建议使用 JSON,因为它很容易开始。举个简单的例子,代替$test->getCall();你可以做:
echo json_encode($test->getCall());
And in requestVal.php:
并在requestVal.php:
$array = json_decode(trim($p_result), TRUE);
print_r($array);
You can find each function description at php.net.
您可以在 找到每个功能说明php.net。
回答by Cyprian
If you are using curl you should expect exactly the same result as you'll get if you run your test.php script in any browser. So, if your test.php is like:
如果您使用 curl,您应该期望得到与在任何浏览器中运行 test.php 脚本时完全相同的结果。所以,如果你的 test.php 是这样的:
echo "123";
in your browser you'll see "123" and that's also you'll get inside your $p_resultvariable. If your test.php is like:
在您的浏览器中,您会看到“123”,这也是您进入$p_result变量的原因。如果你的 test.php 是这样的:
function foo() {
return "123";
}
foo();
You see nothing in browser and get nothing in your $p_resultas well.
您在浏览器中什么也看不到,在您的浏览器中也什么也没有$p_result。
So, try change your test.php in something like that:
因此,尝试将您的 test.php 更改为以下内容:
public function getCall() {
$var = array('fname'=>'Hyman','lname'=>'williams');
return $var;
}
var_dump($test->getCall()); // of course you will show these values in better way (depends on your needs)
回答by Imran Nababan
like Ranty said, you can return a string at test.phpcode
就像 Ranty 说的,你可以在test.php代码中返回一个字符串
$test->getCall();
public function getCall() {
$var = array('fname'=>'Hyman','lname'=>'williams');
echo serialize($var);
}
So, you can caught a serialization data at requestVal.phpcode, by unserialize data from remote server
因此,您可以通过从远程服务器反序列化数据,在requestVal.php代码中捕获序列化数据
unserialize($content)

