开发者

cURL in php - I have 2 functions that work seperatly but when combined the second one outputs the results from the 1st cURL call

I am trying to get the fastspring subscription payment processing set up.

I have the following file which contains 2 functions. the first calls their API via cURL and gets basic order information. the second pases in the subscription reference returned by the order functions and gets some subscription info.

BOTH these functions work correctly when run separately. But when run together, the second function is NOT working and is outputting the results of the first cURL call ?

<?php

//order reference..
$OrderReference = 'QUI110930-3371-94207B';

//UNCOMMENT THIS IF JUST WANT TO TEST getSubscriber function on its own..
//$SubscriptionReference1 = 'QUI110926-3371-68125S';

getOrder ( $OrderReference );

getSubscription ( $SubscriptionReference1 );

//test our variaables for getOrder..
echo '<br/>1:' . $OrderReferrer;
echo '<br/>2:' . $beginDate;
echo '<br/>3:' . $display;
echo '<br/>4:' . $value;
echo '<br/>5:' . $productDisplay;
echo '<br/>7:' . $SubscriptionReference;
echo '<br/><br/><br/>';

//test our variaables for getSubscriber
echo '<br/>1:' . $nextPeriodDate;
echo '<br/>2:' . $subscriptionUrlDetail;
echo '<br/><br/><br/>';
print_r ( $test );

function getOrder($OrderReference) {

    $username = 'my_username';
    $pass = 'my_password';

    //Initialize handle and set options


    $payload = '-i -X GET -u ' . $username . ':' . $pass;
    $requrl = 'https://api.fastspring.com/company/quizba/order/' . $OrderReference;

    define ( 'XML_PAYLOAD', $payload );
    define ( 'XML_POST_URL', $requrl );

    $ch = curl_init ();
    curl_setopt ( $ch, CURLOPT_URL, XML_POST_URL );
    curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt ( $ch, CURLOPT_TIMEOUT, 10 );
    curl_setopt ( $ch, CURLOPT_USERPWD, $username . ':' . $pass );
    curl_setopt ( $ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
    curl_setopt ( $ch, CURLOPT_HTTPHEADER, array ('Content-type: Application/xml' ) );
    curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
    curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, 0 );

    //Execute the request and also time the transaction


    $result = curl_exec ( $ch );
    if ($result == '') {
        $result = curl_exec ( $ch );
    }

    //Check for errors


    if (curl_errno ( $ch )) {
        $resultMsg = 'ERROR -> ' . curl_errno ( $ch ) . ': ' . curl_error ( $ch );
    } else {
        $returnCode = ( int ) curl_getinfo ( $ch, CURLINFO_HTTP_CODE );
        switch ($returnCode) {
            case 404 :
                $resultMsg = 'ERROR -> 404 Not Found';
                break;
            case 200 :
                break;
            default :
                $resultMsg = 'HTTP ERROR -> ' . $returnCode;
                break;
        }
    }

    //Assign the variables..

    $resultArray = simplexml_load_string ( $result );
    global $OrderReferrer;
    $OrderReferrer = $resultArray->referrer [0];
    global $beginDate;
    $beginDate = $resultArray->statusChanged [0]; //needs to be modified..             
    global $display;
    $display = $resultArray->status [0];
    global $productDisplay;
    $productDisplay = $resultArray->orderItems->orderItem [0]->productDisplay [0];
    global $value;
    $value = $resultArray->total [0];
    global $SubscriptionReference;
    $SubscriptionReference = $resultArray->orderItems->orderItem [0]->subscriptionReference [0];

    //Close the handle


    curl_close ( $ch );
}

//get the subscription details

function getSubscription($SubscriptionReference) {
    $username = 'my_username';
    $pass = 'my_password';

    $payload = '-i -X GET -u ' . $username . ':' . $pass;
    $requrl = 'https://api.fastspring.com/company/quizba/subscription/' . $SubscriptionReference;

    define ( 'XML_PAYLOAD', $payload );
    define ( 'XML_POST_URL', $requrl );

    $newCh = curl_init ();
    curl_setopt ( $newCh, CURLOPT_URL, XML_POST_URL );
    curl_setopt ( $newCh, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt ( $newCh, 开发者_StackOverflowCURLOPT_TIMEOUT, 10 );
    curl_setopt ( $newCh, CURLOPT_USERPWD, $username . ':' . $pass );
    curl_setopt ( $newCh, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
    curl_setopt ( $newCh, CURLOPT_HTTPHEADER, array ('Content-type: Application/xml' ) );
    curl_setopt ( $newCh, CURLOPT_SSL_VERIFYHOST, 0 );
    curl_setopt ( $newCh, CURLOPT_SSL_VERIFYPEER, 0 );

    //Execute the request and also time the transaction


    $newResult = curl_exec ( $newCh );
    if ($newResult == '') {
        $newResult = curl_exec ( $newCh );
    }

    //Check for errors


    if (curl_errno ( $newCh )) {
        $newResultMsg = 'ERROR -> ' . curl_errno ( $newCh ) . ': ' . curl_error ( $newCh );
    } else {
        $returnCode = ( int ) curl_getinfo ( $newCh, CURLINFO_HTTP_CODE );

        switch ($returnCode) {
            case 404 :
                $newResultMsg = 'ERROR -> 404 Not Found';
                break;
            case 200 :
                break;
            default :
                $newResultMsg = 'HTTP ERROR -> ' . $returnCode;
                break;
        }
    }

    //Assign the variables..


    global $test;
    $test = $newResult;
    //$newResultArray = new SimpleXMLElement($newResult, NULL, FALSE);
    $newResultArray = simplexml_load_string ( $newResult );

    global $nextPeriodDate;
    $nextPeriodDate = $newResultArray->nextPeriodDate [0]; //needs to be modified..                 
    global $subscriptionUrlDetail;
    $subscriptionUrlDetail = $newResultArray->customerUrl [0];

    //Close the handle


    curl_close ( $newCh );
}


Because you use define ( 'XML_POST_URL', $requrl ); you are unable to do 2 request using that.

You cannot overwrite using a define, it will always keep the first value. Unless you are using it somewhere out of this script (and are unable to transfer the variables with regular variables) you should not be using define.

define() is a good function for using in (for example) a config file, where you set a URL you need to use on a lot of other places. These variables should be constants, and should not be changing.

Note: You should not be using globals, but return the value of the function in an array or variable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜