Link to home
Create AccountLog in
Avatar of Pedro Chagas
Pedro ChagasFlag for Portugal

asked on

Get variable values inside function

Hi E's, In snippet code you can see the code I use for get keywords from google adwords api.
In the last lines you can see the variable that contains the keywords: "$service->response;".
If I change the last lines for:
.................
function show_xml($service) {
  $newwords = $service->response; //remove echo $service->response;
}
echo $newwords; //view the values out of function
========================
The system don't give me any keywords, so I thing is because I just can see the keywords if I execute "echo $newwords;" inside the function.
But if I want get the values inside "$service->response;" out of the function, how I do?

Regards, JC
include('soapclientfactory.php');
$email = '++++++++++++++++';
$password = '++++++++++';
$useragent = 'INSERT_COMPANY_NAME: AdWords API PHP Sample Code';
$developer_token = '++++++++++++++++';
$application_token = '++++++++++++++++++';
$headers =
  '<email>' . $email . '</email>'.
  '<password>' . $password . '</password>' .
  '<clientEmail>' . $client_email . '</clientEmail>' .
  '<useragent>' . $useragent . '</useragent>' .
  '<developerToken>' . $developer_token . '</developerToken>' .
  '<applicationToken>' . $application_token . '</applicationToken>';
$namespace = 'https://adwords.google.cn/api/adwords/v12';
$keyword_tool_service = SoapClientFactory::GetClient(
  $namespace . '/KeywordToolService?wsdl', 'wsdl');
$keyword_tool_service->setHeaders($headers);
$debug = 1;
$seed_keyword =
  '<negative>false</negative>' .
  '<text>'.$_GET['texto'].'</text>' .
  '<type>Exact</type>';
$use_synonyms = '<useSynonyms>true</useSynonyms>';
$request_xml =
  '<getKeywordVariations>' .
  '<seedKeywords>' . $seed_keyword . '</seedKeywords>' .
  $use_synonyms .
  '<languages>pt</languages>' .
  '<countries>PT</countries>' .
  '<countries>BR</countries>' .
  '</getKeywordVariations>';
$variation_lists =
  $keyword_tool_service->call('getKeywordVariations', $request_xml);
$variation_lists = $variation_lists['getKeywordVariationsReturn'];
if ($debug) show_xml($keyword_tool_service);
if ($keyword_tool_service->fault) show_fault($keyword_tool_service);
$to_consider = $variation_lists['additionalToConsider'];
($to_consider) .
  ' variation(s).' . "\n";
$more_specific = $variation_lists['moreSpecific'];
($more_specific) .
  ' variation(s).' . "\n";
function show_xml($service) {
  echo $service->response;
  echo "\n";  
}

Open in new window

SOLUTION
Avatar of Mahdii7
Mahdii7
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Avatar of Pedro Chagas

ASKER

Hi, @cxr I change:
$to_consider = $variation_lists['additionalToConsider'];
($to_consider) .
  ' variation(s).' . "\n";
$more_specific = $variation_lists['moreSpecific'];
($more_specific) .
  ' variation(s).' . "\n";
FOR---------------------------------
 $to_consider = $variation_lists['additionalToConsider'];
echo ($to_consider) .
  ' variation(s).' . "\n";
$more_specific = $variation_lists['moreSpecific'];
echo ($more_specific) .
  ' variation(s).' . "\n";

I change too:
function show_xml($service) {
  echo $service->response;
  echo "\n";  
}
FOR------------------------
function show_xml($service) {
  return $service->response;
}
$newwords = show_xml($keyword_tool_service);
echo $newwords;

I get a error "Fatal error: Call to undefined function show_fault() in /home/tags/public_html/bastidores/api/adwords/index.php on line 38".
Line 38 is: "if ($keyword_tool_service->fault) show_fault($keyword_tool_service);".

Please tell me what is wrong.
Regards, JC

<?
$wordforapi = "quasimodo";
include('soapclientfactory.php');
$email = '++++++++++++';
$password = '+++++++++++++++++';
$useragent = 'INSERT_COMPANY_NAME: AdWords API PHP Sample Code';
$developer_token = '+++++++++++++++++++++++';
$application_token = '+++++++++++++++++++++++++';
$headers =
  '<email>' . $email . '</email>'.
  '<password>' . $password . '</password>' .
  '<clientEmail>' . $client_email . '</clientEmail>' .
  '<useragent>' . $useragent . '</useragent>' .
  '<developerToken>' . $developer_token . '</developerToken>' .
  '<applicationToken>' . $application_token . '</applicationToken>';
$namespace = 'https://adwords.google.cn/api/adwords/v12';
$keyword_tool_service = SoapClientFactory::GetClient(
  $namespace . '/KeywordToolService?wsdl', 'wsdl');
$keyword_tool_service->setHeaders($headers);
$debug = 1;
$seed_keyword =
  '<negative>false</negative>' .
  '<text>'.$wordforapi.'</text>' .
  '<type>Exact</type>';
$use_synonyms = '<useSynonyms>true</useSynonyms>';
$request_xml =
  '<getKeywordVariations>' .
  '<seedKeywords>' . $seed_keyword . '</seedKeywords>' .
  $use_synonyms .
  '<languages>pt</languages>' .
  '<countries>PT</countries>' .
  '<countries>BR</countries>' .
  '</getKeywordVariations>';
$variation_lists =
  $keyword_tool_service->call('getKeywordVariations', $request_xml);
$variation_lists = $variation_lists['getKeywordVariationsReturn'];
if ($debug) show_xml($keyword_tool_service);
if ($keyword_tool_service->fault) show_fault($keyword_tool_service);
$to_consider = $variation_lists['additionalToConsider'];
echo ($to_consider) .
  ' variation(s).' . "\n";
$more_specific = $variation_lists['moreSpecific'];
echo ($more_specific) .
  ' variation(s).' . "\n";
function show_xml($service) {
  return $service->response;
}
$newwords = show_xml($keyword_tool_service);
echo $newwords;
?>

Open in new window

In line 38, you call a function named "show_fault()", but this function is not defined. Define it, or don't call it! You can try this:

if ($keyword_tool_service->fault) var_dump($keyword_tool_service->fault);
Forget @cxr, your code is correct, I'm sory.

Regards, JC