【问题标题】:I need to do what Postman does in PHP我需要做 Postman 在 PHP 中所做的事情
【发布时间】:2014-04-23 01:03:33
【问题描述】:

Postman 是一个 API 测试接口,我可以使用它来成功地进行特定的 API 调用。我正在尝试使用 CURL 在 PHP 中做同样的事情,但是 API 期望格式的方式存在问题......

假设我的网址是 /post。 /post 之后的唯一变量是“auth=asdf1234etc”,然后是 XML 数据。所以我用邮递员放了

/post?auth=asdf1234etc

在 URL 部分,它有一个原始 XML 数据发布部分,我将我的 XML 放在其中:

<?xml version="1.0" encoding="UTF-8"?><usageReports><usageReport></usageReport></usageReports>

我的问题是 XML 不是变量,其中 asdf1234etc 是密钥 auth 的值,我的 XML 是没有密钥的值——所以我不知道如何使用 CURL 发布帖子。

这是我的 PHP 不起作用 - 我得到 401 未授权,因为程序认为 ?auth="..." 之后的所有内容都是身份验证令牌。但是,如果我在 auth 令牌之后放一个 & 也不起作用,因为在 & 之后我必须做 key=value,而我没有密钥。

$URL = "/post?auth=asdf1234etc";
$usage_report = '<?xml version="1.0" encoding="UTF-8"?><usageReports><usageReport></usageReport></usageReports>';
$fields = array("Host" => "/post",
                    "Content-type" => "application/xml",
                    "Content-length" => strlen($usage_report),
                    "data" => $usage_report);//note that my xml is not a variable and I don't expect this specific line to work.
$fields_string = "&";
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
//open connection
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $fields);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
$array_data = json_decode(json_encode(simplexml_load_string($result)), true);
//view response
echo "\n\n";
print_r($array_data);
echo "\n";

真正有用的是,如果邮递员的输出是一个包含所有内容的文字 URL,则发布 URL 基础 + authtoken + XML——因为我可以在邮递员中做到这一点(200 OK),只是不能让 PHP 做同样的事情。

我打开了 CURLOPT_VERBOSE,这里可能有一些有用的信息:

POST /post?asdf123etc HTTP/1.1
Host: hidden.domain.com
Accept: */*
Content-Length: 335      //Why 335? strlen($usage_report) is 248?
Content-Type: application/x-www-form-urlencoded    //why is it changing from application/xml to this?

-it’s not a certificate issue
-it’s not a connection to host issue
-it’s kindof an auth issue, because it thinks that everything after ? is the auth token
-it’s an interesting problem because I can’t set the xml to a key in the fields array
-the xml constructed and urls I use work when using postman
-not an issue of xml format

附言如果您认为您找到了重复项,请确保他们也在询问在 POST 不是 httpheader 的 XML 时要做什么。 (不像this

【问题讨论】:

  • 您的$usage_report = ... 行是一个彻底的语法错误,并且您正在为$fields_String 构建一个不正确的数组
  • 谢谢,已编辑。在这里复制时出错。
  • 如果您将键/值对数组传递给 curl,您可以 NOT 对值进行 url_encode。 curl 为你做这一切。数组应该只有原始的未编码数据。
  • 好的,我停止对我的 $fields 数组使用 urlencode。我也开始使用 CURLOPT_VERBOSE 并编辑以包含我从中获得的信息。还有其他想法吗?
  • 它正在更改为 form-url-encoded 因为您发送的是 key=value 对。如果你想只发送 xml,那么直接将 $xml 传递给 postfields。没有键,只有值。然后 curl 将尊重您的内容类型。

标签: php xml api curl postman


【解决方案1】:

从您的代码中删除这些行:

$fields = array("Host" => "/post",
                "Content-type" => "application/xml",
                "Content-length" => strlen($usage_report),
                "data" => $usage_report);//note that my xml is not a variable and I don't expect this specific line to work.
$fields_string = "&";
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');

现在,首先修复您的 xml 字符串。您没有从 xml 中转义引号。所以用这个:

$usage_report = '<?xml version="1.0" encoding="UTF-8"?><usageReports><usageReport></usageReport></usageReports>';

将这两个 curl 选项替换为您现有的选项。这里是它发布的 XML 数据:

curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/xml")); // or text/xml
curl_setopt($ch, CURLOPT_POSTFIELDS, $usage_report);

您的详细输出表明您使用了正确的 url。但仍要仔细检查,确保网址正确。目前它在您的代码中使用 URI 而不是 URL:

$URL = "/post?auth=asdf1234etc";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-19
    • 2011-02-27
    相关资源
    最近更新 更多