Avatar of Pedro Chagas
Pedro Chagas
Flag 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

PHP

Avatar of undefined
Last Comment
Pedro Chagas

8/22/2022 - Mon
SOLUTION
Mahdii7

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Roger Baklund

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
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

Roger Baklund

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);
Pedro Chagas

ASKER
Forget @cxr, your code is correct, I'm sory.

Regards, JC
Your help has saved me hundreds of hours of internet surfing.
fblack61