如何使用curl发布数据并根据发布的数据获得响应

ati*_*tif 2 php curl

这是我的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>
Run Code Online (Sandbox Code Playgroud)

而域/服务器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;
?>
Run Code Online (Sandbox Code Playgroud)

但它总是回归'none'.我错过了什么吗?

alw*_*arn 12

实际的问题是抓住它..

<?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);
 ?>
Run Code Online (Sandbox Code Playgroud)

你的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;*/

 ?>
Run Code Online (Sandbox Code Playgroud)

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

希望对你有帮助


ati*_*tif 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);
?>
Run Code Online (Sandbox Code Playgroud)

鉴于,在 curl.php

<?php
file_put_contents('abc.text', "here is my data " . implode('->', $_POST));
if ($_POST['name']):
    print_r($_POST['name']);
endif;
?>
Run Code Online (Sandbox Code Playgroud)