Home > Backend Development > PHP Tutorial > php curl接收数据的问题

php curl接收数据的问题

WBOY
Release: 2016-06-06 20:52:04
Original
1475 people have browsed it

接收端是print_r($_POST);

发送端写了一个curl提交post。

$ch = curl_init();
curl_setopt_array($ch,
array(
	CURLOPT_URL => 'http://myurl/abc.php',
	CURLOPT_RETURNTRANSFER => true,
	CURLOPT_POST => true,
	CURLOPT_POSTFIELDS =>'name=abc&id=123'
)
);
$content = curl_exec($ch);
print_r($content);
echo '<br>=========<br>';
echo $content['name'];
Copy after login
Copy after login

打印出来的结果:

Array(
    [name] => abc
    [id] => 123
)
=========
A
Copy after login
Copy after login

为什么$content['name']得到的是一个字母A...

回复内容:

接收端是print_r($_POST);

发送端写了一个curl提交post。

$ch = curl_init();
curl_setopt_array($ch,
array(
	CURLOPT_URL => 'http://myurl/abc.php',
	CURLOPT_RETURNTRANSFER => true,
	CURLOPT_POST => true,
	CURLOPT_POSTFIELDS =>'name=abc&id=123'
)
);
$content = curl_exec($ch);
print_r($content);
echo '<br>=========<br>';
echo $content['name'];
Copy after login
Copy after login

打印出来的结果:

Array(
    [name] => abc
    [id] => 123
)
=========
A
Copy after login
Copy after login

为什么$content['name']得到的是一个字母A...

$content = curl_exec($ch);
Copy after login

这行代码拿到的 $content 是一个字符串。所以你在 echo 的时候打印的是这个字符串。
把print_r换成 var_dump 就一目了然了

var_dump($content);
Copy after login

得到结果:

string(44) "Array
(
    [name] => abc
    [id] => 123
)
"
Copy after login

echo $content['name']; // 这一句

$content是一个字符串, 'name'在这里被转乘整数0,所以是第一个字母A.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template