我需要做Postman在PHP中做的事情

时间:2014-03-17 17:13:48

标签: php xml api curl postman

Postman是一个API测试界面,我可以使用它来成功进行特定的API调用。我尝试使用CURL在PHP中做同样的事情,但是我对API期望格式的方式有疑问......

我们说我的网址是/ post。 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 =&#34; ...&#34;是身份验证令牌。但如果我放一个&amp;在身份验证令牌之后,因为在&amp;我必须做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";

真正有用的是,如果邮递员的输出是文字网址,其中包含所有内容,发布网址基础+ authtoken + XML - 因为我可以在邮递员中做到(200 OK),只是可以&#39 ;让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

P.S。如果您认为自己发现了重复内容,请确保他们在POST POST不是httpheader的XML时也会询问该怎么做。 (不像this

1 个答案:

答案 0 :(得分:0)

从代码中删除这些行:

$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>';

将这两个卷曲选项替换为现有卷曲选项。这里发布XML数据:

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

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

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