5

这是我的POST数据代码:

<?php
$data = array("account" => "1234", "dob" => "30051987", "site" => "mytestsite.com");
$data_string = json_encode($data);

$url = 'http://mydomain.com/curl.php';

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
curl_close($ch);
$json_result = json_decode($result, true);
?>

<p>Your confirmation number is: <strong><?php echo $json_result['ConfirmationCode']; ?></strong></p>

而在域/服务器curl.php文件代码如下:

<?php
// header 
header("content-type: application/json");

if($_POST):
    echo json_encode(array('ConfirmationCode' => 'somecode'));
else:
    echo json_encode(array('ConfirmationCode' => 'none'));
endif;
?>

但它总是返回'none'。我错过了什么吗?

4

3 回答 3

16

真正的问题在于抓住它..

<?php
 $data = array("account" => "1234", "dob" => "30051987", "site" => "mytestsite.com");
 $data_string = json_encode($data);

 $url = 'http://mydomain.com/curl.php';

 $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                     'Content-Type: application/json',
                 'Content-Length: ' . strlen($data_string))
   );
  $result = curl_exec($ch);
  curl_close($ch);

  echo $result;

  //$json_result = json_decode($result, true);
 ?>

你的 curl.php 代码

<?php
 $fp = fopen('php://input', 'r');
 $rawData = stream_get_contents($fp);

 echo "<pre>";
 print_r($rawData);
 echo "</pre>";


 /*if($rawData):
 echo json_encode(array('ConfirmationCode' => 'somecode'));;
 else:
 echo json_encode(array('ConfirmationCode' => 'none'));
 endif;*/

 ?>

由于您在正文中将数据作为原始 JSON 发送,因此它不会填充 $_POST 变量

希望对你有帮助

于 2013-03-13T04:30:45.117 回答
5

此链接上的功能将起作用。

    <?php

function post_to_url($url, $data) {
    $fields = '';
    foreach ($data as $key => $value) {
        $fields .= $key . '=' . $value . '&';
    }
    rtrim($fields, '&');

    $post = curl_init();

    curl_setopt($post, CURLOPT_URL, $url);
    curl_setopt($post, CURLOPT_POST, count($data));
    curl_setopt($post, CURLOPT_POSTFIELDS, $fields);
    curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);

    $result = curl_exec($post);

    curl_close($post);
    return $result;
}

$data = array(
    "name" => "new Name",
    "website" => "http://bavotasan.com",
    "twitterID" => "bavotasan"
);

$surl = 'http://mydomain.com/curl.php';
echo post_to_url($surl, $data);
?>

鉴于,在curl.php

<?php
file_put_contents('abc.text', "here is my data " . implode('->', $_POST));
if ($_POST['name']):
    print_r($_POST['name']);
endif;
?>
于 2013-03-13T07:53:48.283 回答
2

Content-TypePOST 请求的“正确”标头应该是application/x-www-form-urlencoded,但是您用它覆盖了它application/json(仅在服务器端需要)。

将您的代码更改为以下内容:

<?php
$data = array("account" => "1234", "dob" => "30051987", "site" => "mytestsite.com");
$data_string = json_encode($data);

$url = 'http://mydomain.com/curl.php';

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true); // no need to use custom request method

// ----- METHOD #1: no need to "stringify"
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_array);
// ----- METHOD #2: or if you really like to JOSN-ify
curl_setopt($ch, CURLOPT_POSTFIELDS, array("json"=>$data_string));

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
/* ------ this will be handled by PHP/cURL
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string))
);
*/
$result = curl_exec($ch);
curl_close($ch);
$json_result = json_decode($result, true);
?>
于 2013-03-13T05:02:13.353 回答