0

我正在尝试发布到 linksalpha API。我已经尝试了无数种方法,但在post时我什至没有从他们的服务器得到响应。

这是我设置的开发页面。内容多部分响应对我来说看起来很奇怪。我编写的代码(现在隐藏了 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>
4

1 回答 1

1

如果你通过CURLOPT_POSTFIELDS with array了,那么它将算作multipart/form-data但通过 cURL 发布数据,那么你需要application/x-www-form-urlencoded version

所以在你的函数开始之前加上以下内容

$body = http_build_query($body);
于 2012-10-13T19:15:35.287 回答