通过PHP cURL连接服务以发送不工作的数据
我正试图发布到linksalpha API。我已经尝试了无数种方法,但我甚至没有从他们的服务器得到响应 这是我设置的。内容多部分响应在我看来很奇怪。下面是我编写的代码(api现在隐藏)。我所做的一切都不管用:(:通过PHP cURL连接服务以发送不工作的数据,php,api,curl,Php,Api,Curl,我正试图发布到linksalpha API。我已经尝试了无数种方法,但我甚至没有从他们的服务器得到响应 这是我设置的。内容多部分响应在我看来很奇怪。下面是我编写的代码(api现在隐藏)。我所做的一切都不管用:(: 如果通过数组传递CURLOPT\u POSTFIELDS,则它将计为多部分/表单数据,但要通过cURL发布数据,则需要应用程序/x-www-form-urlencoded版本 因此,在函数开始之前,请先执行以下操作 我这样做了,表单更改了,但它仍然没有发送到服务。您在$response
如果通过数组传递
CURLOPT\u POSTFIELDS
,则它将计为多部分/表单数据
,但要通过cURL发布数据,则需要应用程序/x-www-form-urlencoded版本
因此,在函数开始之前,请先执行以下操作
我这样做了,表单更改了,但它仍然没有发送到服务。您在$response=curl\u exec($ch)之后得到的var\u dump($response);这一行是“string(0)”-您也可以在我的链接上看到
内容多部分响应对我来说看起来很奇怪
您能将此响应添加到pastbin中进行进一步分析吗
<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>
$body = http_build_query($body);