Jump to content

strtok help


xpapax

Recommended Posts

Okay I'm new to PHP, i've only coded in Java, C, C++, and some Ruby.  I'm trying to get an Auth key from the google base server.  I coded it all in java first and now im trying to change it into PHP.  I'm able to send the POST to the server with my email and password and the server returns the proper response from the server and i need to get the key out of the string it returns.  I wrote a test method with the string the server returns passed in and it works perfectly, but when i run both methods together it doesnt work.  Here is my code.

 

<?php

function test($string){

$tok = strtok($string, " =");

while($tok !== false){
	echo "token = $tok <br />";

	if($tok == "Auth"){
		$key = strtok(" ");
		echo "key = $key <br />";
		break;
	}
	$tok = strtok(" =");
}
return $key;
}

$string = "SID=DQAAALYAAACgPRQOevmWKwTGjegZf7OKr3m8yVlpU47sOFFvxXasp-TcPHrDnvUcgk71h_cRgm_FIYj8qONzLZgL8WoXESNHeoip-Il4Cezxwk8-QBLAJsDG3_IizkO8rwsG8o8GgpPI6VbCAaR2WdiHyzFPPODq1fNfNG2EZNnT3qJP1E6PW_s3GlRwoH0HDZVqX6H7mPLXUtmCVXER7bxiT1OhYYFBBOqD-VRd-USKLmb_4x62mBrSxo0HYDdw1C0cCvqp_sc LSID=DQAAALkAAACbYL6psrBizFGSvCXUnHN9-ObTh0ayt7IGVHF_bmT2-0S2OFF_OziM_KDEsME3W-7DB5HwUANBiqnbMnJPwDUy5ReVfyhscJAcNmR7e9JRV2lveNlzteiXqCzYLCYXH6GG2Voy1YGvnpY4Lw2jR3Ujkigrha1qW83_25dDwQAncvabUv7XZCJyXZtksNQJeUlyPQ29rNdY8PQUnCeEbsIXB5e7k4VmXHimzabr3WY6ue7N6E4ACOAuMmnOIFw6OSY Auth=DQAAALoAAACbYL6psrBizFGSvCXUnHN9-ObTh0ayt7IGVHF_bmT2-0S2OFF_OziM_KDEsME3W-7DB5HwUANBiqnbMnJPwDUy5ReVfyhscJAcNmR7e9JRV582bJ0JFlKpiFYan5xHnZjz48rtptooURHKLykkDtJanoz-3o8_68p5noz07P9S77yAv9ezI4PDJyanoS4wO01lfu5cP4-M2OcA-4oET3_YRaod4TCbyVYUXm4A8ugeGx3FUjxqE-RJscoc7O67Exg";
$token = test($string);
echo "final token = $token <br />";
?>

 

and the output looks like

 

token = SID

token = DQAAALYAAACgPRQOevmWKwTGjegZf7OKr3m8yVlpU47sOFFvxXasp-TcPHrDnvUcgk71h_cRgm_FIYj8qONzLZgL8WoXESNHeoip-Il4Cezxwk8-QBLAJsDG3_IizkO8rwsG8o8GgpPI6VbCAaR2WdiHyzFPPODq1fNfNG2EZNnT3qJP1E6PW_s3GlRwoH0HDZVqX6H7mPLXUtmCVXER7bxiT1OhYYFBBOqD-VRd-USKLmb_4x62mBrSxo0HYDdw1C0cCvqp_sc

token = LSID

token = DQAAALkAAACbYL6psrBizFGSvCXUnHN9-ObTh0ayt7IGVHF_bmT2-0S2OFF_OziM_KDEsME3W-7DB5HwUANBiqnbMnJPwDUy5ReVfyhscJAcNmR7e9JRV2lveNlzteiXqCzYLCYXH6GG2Voy1YGvnpY4Lw2jR3Ujkigrha1qW83_25dDwQAncvabUv7XZCJyXZtksNQJeUlyPQ29rNdY8PQUnCeEbsIXB5e7k4VmXHimzabr3WY6ue7N6E4ACOAuMmnOIFw6OSY

token = Auth

key = DQAAALoAAACbYL6psrBizFGSvCXUnHN9-ObTh0ayt7IGVHF_bmT2-0S2OFF_OziM_KDEsME3W-7DB5HwUANBiqnbMnJPwDUy5ReVfyhscJAcNmR7e9JRV582bJ0JFlKpiFYan5xHnZjz48rtptooURHKLykkDtJanoz-3o8_68p5noz07P9S77yAv9ezI4PDJyanoS4wO01lfu5cP4-M2OcA-4oET3_YRaod4TCbyVYUXm4A8ugeGx3FUjxqE-RJscoc7O67Exg

final token = DQAAALoAAACbYL6psrBizFGSvCXUnHN9-ObTh0ayt7IGVHF_bmT2-0S2OFF_OziM_KDEsME3W-7DB5HwUANBiqnbMnJPwDUy5ReVfyhscJAcNmR7e9JRV582bJ0JFlKpiFYan5xHnZjz48rtptooURHKLykkDtJanoz-3o8_68p5noz07P9S77yAv9ezI4PDJyanoS4wO01lfu5cP4-M2OcA-4oET3_YRaod4TCbyVYUXm4A8ugeGx3FUjxqE-RJscoc7O67Exg

 

so as you can see the string breaks up on "(space) and ="

 

But when i run with the server call

 

<?php

function tokenizeString($output){
echo "Oringal String = $output <br />";
$tok = strtok($output, " =");

while($tok){
	echo "token = $tok <br />";

	if($tok == "Auth"){
		$key = strtok(" ");
		echo "key = $key <br />";
		break;
	}
	$tok = strtok(" =");
}
return $key;
}

function getKey() {
$auth_url = "https://www.google.com/accounts/ClientLogin";
$dev_key= "";
$email = "";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $auth_url);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "Email=");
$output = curl_exec($ch);

$token = tokenizeString($output);
echo "final token = $token <br />";
curl_close($ch);
}

getKey();

?>

 

i get this as output

 

Oringal String = SID=DQAAALYAAABT-PE3IVVCUDuRvlyYhFPviPuxRDZYd8RWvAcaZlijeGfVuNd-IcufZ6F-68UCsj_3tAJ657NDumPnj4mCEgvuwgtemZ4xzs8dyb93GW2YbUGpAL0ETUqu1TgrB7so6x_j-A5uBnm6GTB6gZbKGe2538vXOObLi3gXWOg-8C9Ie-MSzTNCUR1s94rsThQq0CBTTVbXcOq0OqftLOUFp3ahyi4iPGFg8DlTYvKWkZEmvkuITn-TTBLu9n_vedDOCUw LSID=DQAAALkAAAAZuyDdmMsSErWOnsImDmctyYquQD95aYasnrUr2tENjP3xHTRugjKSDoczXEnURva-QF0ooEni4g7ZNYf_w5MlfdISLgjstvD1d8V_M5OD-bquC5pHNiFT6c-Tq1AjynTG2pBJfLqMH-OWWBPuZ-WYU99tGn_jUR__eXfPwHuJrT1p40DoFAR4Rv7scm_MTj-g0XWrHsOoHC1x8AKlATZYwy2jx5lMI6PYBq8ZojuwOYAp9rYF8g4ZijfWlyhxOrc Auth=DQAAALgAAAAZuyDdmMsSErWOnsImDmctyYquQD95aYasnrUr2tENjP3xHTRugjKSDoczXEnURva-QF0ooEni4g7ZNYf_w5MlfdISLgjstvD1d8V_M5OD-Y6l9tzUtnpd4HzFpBfTZjOsSAq3noV4Oe8n5BnrF9vkqkviiX2gNhSWIQ3JnF3UvJoCFWXNSmFxsX6f_UEKV-XYWjriZ5qWPOiY-pusyogcCfgIwzdAM9ohmdTVhfbCu6kPxVC2aK92TdA2ZF627og

token = SID

token = DQAAALYAAABT-PE3IVVCUDuRvlyYhFPviPuxRDZYd8RWvAcaZlijeGfVuNd-IcufZ6F-68UCsj_3tAJ657NDumPnj4mCEgvuwgtemZ4xzs8dyb93GW2YbUGpAL0ETUqu1TgrB7so6x_j-A5uBnm6GTB6gZbKGe2538vXOObLi3gXWOg-8C9Ie-MSzTNCUR1s94rsThQq0CBTTVbXcOq0OqftLOUFp3ahyi4iPGFg8DlTYvKWkZEmvkuITn-TTBLu9n_vedDOCUw LSID

token = DQAAALkAAAAZuyDdmMsSErWOnsImDmctyYquQD95aYasnrUr2tENjP3xHTRugjKSDoczXEnURva-QF0ooEni4g7ZNYf_w5MlfdISLgjstvD1d8V_M5OD-bquC5pHNiFT6c-Tq1AjynTG2pBJfLqMH-OWWBPuZ-WYU99tGn_jUR__eXfPwHuJrT1p40DoFAR4Rv7scm_MTj-g0XWrHsOoHC1x8AKlATZYwy2jx5lMI6PYBq8ZojuwOYAp9rYF8g4ZijfWlyhxOrc Auth

token = DQAAALgAAAAZuyDdmMsSErWOnsImDmctyYquQD95aYasnrUr2tENjP3xHTRugjKSDoczXEnURva-QF0ooEni4g7ZNYf_w5MlfdISLgjstvD1d8V_M5OD-Y6l9tzUtnpd4HzFpBfTZjOsSAq3noV4Oe8n5BnrF9vkqkviiX2gNhSWIQ3JnF3UvJoCFWXNSmFxsX6f_UEKV-XYWjriZ5qWPOiY-pusyogcCfgIwzdAM9ohmdTVhfbCu6kPxVC2aK92TdA2ZF627og

final token =

 

It doesnt seem to delimit on the whitespace.

 

Does anyone know what im doing wrong?

 

Link to comment
https://forums.phpfreaks.com/topic/204958-strtok-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.