16.4. Returning the Keyword EstimateThe information returned from a keyword estimate can be pretty valuable for making decisions about whether you want to target a keyword or whether you want to raise your bid for the keyword. All the example in this chapter does is display the raw data, but it's easy to see how this data could be used to provide intelligence for a business application that divides resources among different keywords.
If you want to do something with the information that the response to a keyword estimate request provides, you need to follow a number of steps to retrieve the data. These steps are not that complex, although it is a bit tricky to construct XML-wrapped keyword requests and request messages that don't cause the NuSOAP library to generate a SOAP error.
To return a keyword estimate:
16.4.1. Creating a Service ObjectYou can use the NuSoap soapclient class to create a new client service object by supplying the WSDL file for the web service. Once you have instantiated the client object, you can authenticate it and then send it messages and use the responses. Here's the code for creating a new client object based on the WSDL file: $wsdl = "https://adwords.google.com/api/adwords/v2/TrafficEstimatorService?wsdl"; $client = new soapclient($wsdl, 'wsdl'); 16.4.2. Using the Header Information for AuthenticationAuthentication works the same way for all of the AdWords API web services, but it must be done each time a new client based on one of the services is created. The header information consists of the email address for an AdWords account, the password for the account, an arbitrary UserAgent string, and a developer key. In this example, the user has supplied all this information. The $header variable stores the authentication information supplied by the user, wrapped in XML, and concatenated together. The $header variable is used to authenticate the service: $client->setHeaders($header); You should check to see that this worked by checking for SOAP errors: if($client->fault) { showErrors($client); echo '<a href="estimate_keyword.php">Try again</a>'; } else{ // construct the keyword request ... If there are no SOAP faults displayed, the program can move on and construct the keyword request. 16.4.3. Constructing the Keyword RequestTo construct the keyword request, the fields of the request need to be wrapped in XML to meet the requirements of the NuSOAP library and document literal SOAP (see Chapter 14 for details): $keyword = makeDocLit ("text", $keyword); $maxCpc = makeDocLit ("maxCpc", $maxCpc); In addition, information that in this case was not requested of the user (the type of match) needs to be added: $otherinfo = $maxCpc . "<type>Broad</type>"; $keywordRequest = $keyword . $otherinfo; Then the entire request is, itself, wrapped in XML: $keywordxml = makeDocLit ("KeywordRequest", $keywordRequest); 16.4.4. Send a Request MessageTo send the request message, wrap the keyword requests (in this case, there is only one keyword request) in XML as an estimateKeywordList object: $param = makeDocLit ("estimateKeywordList", $keywordxml); Call the web service client estimateKeywordList method: $response = $client->call("estimateKeywordList", $param); The response will contain the estimate information: $response = $response['estimateKeywordListReturn']; 16.4.5. Displaying Information Using the Keyword EstimateBefore attempting to display the estimate information, check to see if there were any SOAP faults: if($client->fault) { showErrors($client); echo '<a href="estimate_keyword.php">Try again</a>'; } else{ // display estimate information ... If there are no errors, go ahead and display the results element fields: echo "<h2>Here is your keyword estimate:</h2>"; echo "<h3>" . $keyword . "</h3>"; printResults($response); ... function printResults ($estimate) { echo "\n<br>cpc = " . $estimate['cpc'] . " in micro-units"; echo "\n<br>clicks = " . $estimate['ctr'] * $estimate['impressions']; echo "\n<br>ctr = " . ($estimate['ctr'] * 100) . "%"; echo "\n<br>impressions = " . $estimate['impressions']; echo "\n<br>notShown = " . $estimate['notShownPerDay']; echo "\n<br>position = " . $estimate['avgPosition']; } When this code is run, depending on the keyword entered, the display will look like that shown in Figure 16-4. Figure 16-4. Keyword estimation statistics, simply displayed here, can be used as part of an automated keyword selection process
Example 16-3 shows the library functions that add XML to a variable, required by the NuSOAP library and document literal SOAP, and the function that displays errors (see Chapter 14 for a detailed explanation). Example 16-3. Adding XML and Displaying Errors (hd_lib.inc)
Example 16-4 shows the code required to construct the request object and request message, and then to display the fields of the request response. Example 16-4. Constructing the Request and Displaying the Response (show_estimate.php)
|