2

我有这个网站 api 的问题,我使用这个代码通过这个 api 生成一个短网址,但我总是得到一个没有任何内容的白页屏幕!请帮我生成一个网址:

<?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);
?>
4

1 回答 1

3

根据你的工作。

<?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' ) );

?>
于 2013-12-23T14:04:56.927 回答