doushang2023 2014-04-22 20:53
浏览 38

simplexml_load_file不使用变量但使用字符串

I am trying to connect to a CRM (Pardot).

I have this to create the URL necessary to call the XML:

//this will log in and print your API Key (good for 1 hour) to the console
$rz_key = callPardotApi('https://pi.pardot.com/api/login/version/3', 
array(
    'email' => 'myemail@email.com',
    'password' => 'password',
    'user_key' => '032222222222222b75a192daba28d' 
)
 );

$number_url  = 'https://pi.pardot.com/api/prospect/version/3/do/query';
$number_url .= '?user_key=032222222222222b75a192daba28d';
$number_url .= '&api_key=';
$number_url .= trim($rz_key);
$number_url .= '&list_id=97676';

$ike = simplexml_load_file($number_url);
print_r($ike);

Now this code returns :

SimpleXMLElement Object ( [@attributes] => Array ( [stat] => fail [version] => 1.0 )   [err] => Invalid API key or user key ) 

However, if I echo $number_url, and copy and paste that URL into my browser, it loads wonderfully. If I copy and paste the same echoed URL into simplexml_load_file it works wonderfully also. I MUST use a variable, because the API key is only good for one hour. Any ideas?

The rest of the code is here, which was provided by Pardot :

<?php
/**
 * Call the Pardot API and get the raw XML response back
 * 
 * @param string $url the full Pardot API URL to call, e.g. "https://pi.pardot.com/api/prospect/version/3/do/query"
 * @param array $data the data to send to the API - make sure to include your api_key and user_key for authentication
 * @param string $method GET", "POST", "DELETE"
 * @return string the raw XML response from the Pardot API
 * @throws Exception if we were unable to contact the Pardot API or something went wrong
 */
function callPardotApi($url, $data, $method = 'GET')
{
// build out the full url, with the query string attached.
$queryString = http_build_query($data, null, '&');
if (strpos($url, '?') !== false) {
    $url = $url . '&' . $queryString;
} else {
    $url = $url . '?' . $queryString;
}

$curl_handle = curl_init($url);

// wait 5 seconds to connect to the Pardot API, and 30
// total seconds for everything to complete
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($curl_handle, CURLOPT_TIMEOUT, 30);

// https only, please!
curl_setopt($curl_handle, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);

// ALWAYS verify SSL - this should NEVER be changed. 2 = strict verify
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYHOST, 2);

// return the result from the server as the return value of curl_exec instead of echoing it
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);

if (strcasecmp($method, 'POST') === 0) {
    curl_setopt($curl_handle, CURLOPT_POST, true);
} elseif (strcasecmp($method, 'GET') !== 0) {
    // perhaps a DELETE?
    curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, strtoupper($method));
}

$pardotApiResponse = curl_exec($curl_handle);
if ($pardotApiResponse === false) {
    // failure - a timeout or other problem. depending on how you want to handle failures,
    // you may want to modify this code. Some folks might throw an exception here. Some might
    // log the error. May you want to return a value that signifies an error. The choice is yours!

    // let's see what went wrong -- first look at curl
    $humanReadableError = curl_error($curl_handle);

    // you can also get the HTTP response code
    $httpResponseCode = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);

    // make sure to close your handle before you bug out!
    curl_close($curl_handle);

    throw new Exception("Unable to successfully complete Pardot API call to $url -- curl error: \"".
                            "$humanReadableError\", HTTP response code was:     $httpResponseCode");
}

    // make sure to close your handle before you bug out!
    curl_close($curl_handle);

    return $pardotApiResponse;
}

//this will log in and print your API Key (good for 1 hour) to the console

 $rz_key = callPardotApi('https://pi.pardot.com/api/login/version/3', 
 array(
    'email' => 'myemail@email.com',
    'password' => 'myPassword',
    'user_key' => '********************' 
 )
);

$number_url  = 'https://pi.pardot.com/api/prospect/version/3/do/query';
$number_url .= '?user_key=****************';
$number_url .= '&api_key=';
$number_url .= $rz_key;
$number_url .= '&list_id=97676';

$number_url = preg_replace('/\s+/', '', $number_url);

$ike = simplexml_load_file($number_url);
print_r($ike);

?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
    • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
    • ¥15 MATLAB中streamslice问题
    • ¥15 如何在炒股软件中,爬到我想看的日k线
    • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
    • ¥15 seatunnel 怎么配置Elasticsearch
    • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
    • ¥15 (标签-MATLAB|关键词-多址)
    • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
    • ¥500 52810做蓝牙接受端