我正在尝试POST到linksalpha API。我尝试了无数种方法,但在post时,我甚至没有得到他们服务器的回复。
以下是我设置的dev page。内容多部分响应对我来说很奇怪。我写的代码(api现在隐藏)在下面。我没有尝试做任何事情:(:
<html>
<head></head>
<body>
<?php
$link = 'http://api.linksalpha.com/ext/post';
$data = array(
'api_key' => 'xxxxxxxxxxxxxx',
'post_id' => '434345',
'post_link' => 'www.google.com',
'post_title' => 'test title',
'post_content' => 'test msg'
);
function networkpub_http_post($link, $body) {
$url = $link;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$response = curl_exec($ch);
function dbg_curl_data($ch, $data=null) {
static $buffer = '';
if ( is_null($ch) ) {
$r = $buffer;
$buffer = '';
return $r;
}
else {
$buffer .= $data;
return strlen($data);
}
}
echo '<fieldset><legend>request headers</legend>
<pre>', htmlspecialchars(curl_getinfo($ch, CURLINFO_HEADER_OUT)), '</pre>
</fieldset>';
echo '<fieldset><legend>response</legend>
<pre>', htmlspecialchars(dbg_curl_data(null)), '</pre>
</fieldset>';
curl_close($ch);
return $response;
}
$response_full = networkpub_http_post($link, $data);
echo($response_full);
?>
</body>
</html>
答案 0 :(得分:1)
如果您通过CURLOPT_POSTFIELDS with array
,那么它将计为multipart/form-data
,但是要通过cURL发布数据,那么您需要application/x-www-form-urlencoded version
所以在你的功能开始之前放下来
$body = http_build_query($body);