使用php通过linkbucks api生成url

使用php通过linkbucks api生成url,php,api,Php,Api,我对这个网站api有问题, 我用这段代码通过这个api生成了一个短URL,但我总是得到一个没有任何内容的白色页面屏幕! 请帮助我生成url: <?php function bucksapi($longUrl) { $bucksapi = 'myapipass'; $sinoone = 'myusername'; $adts = '2'; $contype = '1'; $domainss = 'linkbucks.com';

我对这个网站api有问题, 我用这段代码通过这个api生成了一个短URL,但我总是得到一个没有任何内容的白色页面屏幕! 请帮助我生成url:

<?php
  function bucksapi($longUrl) 
{     
    $bucksapi = 'myapipass'; 
    $sinoone = 'myusername';
    $adts = '2';    
    $contype = '1';
    $domainss = 'linkbucks.com';   
    $postData = array('originalLink' => $longUrl, 'user' => $sinoone, 'apiPassword' => $bucksapi, 'contentType' => $contype, 'adType' => $adts, 'domain' => $domainss);
    $jsonData = json_encode($postData);
    $curlObj = curl_init();
    curl_setopt($curlObj, CURLOPT_URL, 'https://www.linkbucks.com/api/createLink/single');
    curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
    //As the API is on https, set the value for CURLOPT_SSL_VERIFYPEER to false. This will stop cURL from verifying the SSL certificate.
    curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curlObj, CURLOPT_HEADER, 0);
    curl_setopt($curlObj, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
    curl_setopt($curlObj, CURLOPT_POST, 1);
    curl_setopt($curlObj, CURLOPT_POSTFIELDS, $jsonData);
    $response = curl_exec($curlObj);
    $json = json_decode($response);
    curl_close($curlObj);
    return $json->link;
}
?>

这用于打印短链接:

<?php
$long_url = "http://google.com";
echo bucksapi($long_url);
?>

基于您的工作。


如果您更换了echo bucksapi($long\u url)带有
var_dump(bucksapi($long_url))?我猜您的函数返回的是
FALSE
,可能是因为对linkbucks的调用失败,或者是因为生成的json中没有link属性。可能值得在bucksapi()中转储
$json
,以了解您获得的数据类型。我认为linkbucks api存在问题。。。请检查,只有一个白色的page@user1721347当你把它扔掉时会发生什么?当您直接打开页面时,有点正常,因为您没有传递任何值,所以不会看到任何内容。
<?php

function curl($url, $cookies= NULL, $post = NULL)
{
    $ch = @curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);

    if (!empty($cookies)) {
        curl_setopt($ch, CURLOPT_COOKIE, $cookies);
    }

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    if (!empty($post)) {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    }

    curl_setopt($ch, CURLOPT_TIMEOUT, 25);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 25);

    $page = curl_exec($ch);

    var_dump($page);

    curl_close($ch);

    return $page;
}

function bucksapi($longUrl, $bucksapi, $sinoone, $adts = '2', $contype = '1')
{
    $postData = array(
        'originalLink'  => $longUrl,
        'user'          => $sinoone,
        'apiPassword'   => $bucksapi,
        'contentType'   => $contype,
        'adType'        => $adts,
        'domain'        => 'linkbucks.com'
    );

    $json = json_decode(
        curl(
            'https://www.linkbucks.com/api/createLink/single',
            NULL,
            json_encode($postData)
        )
    );

    var_dump($json);

    return $json->link;
}

var_dump( bucksapi( "LINK_URL", 'API_KEY', 'USER_NAME' ) );

?>