php - simplexml_load_file not working with variable but works with string -
i trying connect crm (pardot).
i have create url necessary call xml:
//this log in , print api key (good 1 hour) 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 code returns :
simplexmlelement object ( [@attributes] => array ( [stat] => fail [version] => 1.0 ) [err] => invalid api key or user key )
however, if echo $number_url
, , copy , paste url browser, loads wonderfully. if copy , paste same echoed url simplexml_load_file
works wonderfully also. must use variable, because api key 1 hour. ideas?
the rest of code here, provided pardot :
<?php /** * call pardot api , raw xml response * * @param string $url full pardot api url call, e.g. "https://pi.pardot.com/api/prospect/version/3/do/query" * @param array $data data send api - make sure include api_key , user_key authentication * @param string $method get", "post", "delete" * @return string raw xml response pardot api * @throws exception if unable contact pardot api or went wrong */ function callpardotapi($url, $data, $method = 'get') { // build out full url, 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 connect pardot api, , 30 // total seconds 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); // verify ssl - should never changed. 2 = strict verify curl_setopt($curl_handle, curlopt_ssl_verifyhost, 2); // return result server return value of curl_exec instead of echoing 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 delete? curl_setopt($curl_handle, curlopt_customrequest, strtoupper($method)); } $pardotapiresponse = curl_exec($curl_handle); if ($pardotapiresponse === false) { // failure - timeout or other problem. depending on how want handle failures, // may want modify code. folks might throw exception here. might // log error. may want return value signifies error. choice yours! // let's see went wrong -- first @ curl $humanreadableerror = curl_error($curl_handle); // can http response code $httpresponsecode = curl_getinfo($curl_handle, curlinfo_http_code); // make sure close handle before bug out! curl_close($curl_handle); throw new exception("unable complete pardot api call $url -- curl error: \"". "$humanreadableerror\", http response code was: $httpresponsecode"); } // make sure close handle before bug out! curl_close($curl_handle); return $pardotapiresponse; } //this log in , print api key (good 1 hour) 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); ?>
Comments
Post a Comment