14.2. Creating the Campaign Web Service ClientIn order to use one of the Google AdWords API web services, you need to:
The authentication headers require the same information for all of the AdWords API web services, so once you've generated this information, you can use it in repeated calls to the AdWords API web services. The information required for the authentication headers is shown in Table 14-1.
In a real program in the real world you probably won't let users enter the header authentication information; it will be programmatically generated. But in this situation, I've set things up so that the user enters authentication information, as well as the information to create a campaign, as shown in Figure 14-1. Example 14-1 shows authenticate.php, which contains the HTML for the form depicted in Figure 14-1; the form allows a user to input authentication and campaign information. Figure 14-1. The authentication informationthe first four items requestedare used for all the different servicesExample 14-1. HTML form for entry of authentication and campaign information (authenticate.php)
14.2.1. The General PatternWithin the AdWords API web services, there's a fairly generic set of steps to follow. This pattern works the same way at all levels of the hierarchy of AdWords objects. You need a client object based on a particular AdWords API web service. Each time you invoke a new client object, you need to authenticate it. You need to wrap your parameters in XML, and you need to use the identification of the containing object (unless you are at the top of the object hierarchy, as with a campaign). Here are the steps:
14.2.2. Creating the Authentication HeadersYou need authentication information (email, password, user agent, and developer token) to create the authentication headers. The email, password, user agent, and token are passed to campaign.php using HTTP POST, so these can be read in PHP easily: $email = $_POST['email']; $password = $_POST['password']; $useragent = $_POST['useragent']; $token = $_POST['token']; Next, these values need to be wrapped in the appropriate XML element tags. To simplify this a bit, here's a utility function that accepts plain text representing the contents of an element and creates an element with that content (using the supplied element name): function makeDocLit($xml, $var) { return '<' . $xml . '>' . $var . '</' . $xml . '>'; } The function is in the library file and needs to be included in each PHP module that uses it, like this: require_once('hd_lib.inc'); This code then wraps the authentication headers in the appropriate XML: $email = makeDocLit ("email", $email); $password = makeDocLit ("password", $password); $useragent = makeDocLit ("useragent", $useragent); $token = makeDocLit ("token", $token); The header consists of the four XML-wrapped values concatenated: $header = $email . $password . $useragent . $token;
This header will be used to authenticate each of the AdWords API web services used. To make it available in other PHP modules in the application besides the current one, it's easy to use PHP's session-tracking facility. To implement session tracking, put a directive in the file before any HTML has been generated: <?php session_start( ) ?> With that directive in place at the beginning of the file, it's easy to save a variable so it can be used across a multipage application: session_register('header');
14.2.3. Connecting to the WSDL File and Creating a Client ObjectCreating a client object for one of the Google AdWords API web services gives you programmatic access to the members of that web service, meaning its properties and methods. So the next step is to use the URL for the WSDL file to create a client object based on the web service: $campaignwsdl = "https://adwords.google.com/api/adwords/v2/CampaignService?wsdl"; $campaignclient = new soapclient($campaignwsdl, 'wsdl'); 14.2.4. Setting the Authentication Headers for the ClientsOnce the web service has been instantiated in your code, Google needs to know that your program has a legitimate right to use the web service to create (and set) AdWords objects. To accomplish this, set the authentication header that was created in code: $campaignclient->setHeaders($header);
Here's what the script looks like so far (leaving the HTML out of it): ... <?php require_once('NuSOAP/nusoap.php'); require_once('hd_lib.inc'); ... $email = $_POST['email']; $password = $_POST['password']; $useragent = $_POST['useragent']; $token = $_POST['token']; ... // Set up the authentication headers $email = makeDocLit ("email", $email); $password = makeDocLit ("password", $password); $useragent = makeDocLit ("useragent", $useragent); $token = makeDocLit ("token", $token); $header = $email . $password . $useragent . $token; session_register('header'); // Connect to the WSDL file for the CampaignService $campaignwsdl = "https://adwords.google.com/api/adwords/v2/CampaignService?wsdl"; $campaignclient = new soapclient($campaignwsdl, 'wsdl'); // Set the headers for the client $campaignclient->setHeaders($header); |