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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15377042/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:07:36  来源:igfitidea点击:

How to post data using curl and get reponse based on posted data

phpcurl

提问by atif

Here is my code to POSTdata:

这是我的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>

Whereas on domain/server curl.phpfile code as under:

而在域/服务器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;
?>

But it always return 'none'. Am I missing something?

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

回答by alwaysLearn

The actual problem is in grabbing it..

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

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

your code for curl.php

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

 ?>

Since You are sending the data as raw JSON in the body, it will not populate the $_POST variable

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

Hope this will help you

希望能帮到你

回答by atif

The function on this linkwill work.

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

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

Whereas, on curl.php

鉴于,在 curl.php

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

回答by Passerby

The "correct" Content-Typeheader for a POST request should be application/x-www-form-urlencoded, but you overrode it with application/json(which is only needed from the server side).

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

Change your code to below:

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

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