djarvis270 Posted January 10, 2008 Share Posted January 10, 2008 I posted a few days ago about a google translator app that I was having trouble with. My issue then was solved, but now I have another one: I am using curl to send a POST request to the page http://translate.google.com/translate_t With my curl handle, i also send all the required post fields. However, it is not working correctly. What is supposed to happen: In a browser window if I physically type in the entire url, post fields included it looks like this: http://translate.google.com/translate_t?hl=en&ie=UTF8&text=Hello, my name is Dan.&langpair=en%7Ces When I hit enter, the page loads and does the translation correctly. When I do this in curl, and I set CURLOPT_RETURNTRANSFER to false I receive the page source in my command prompt window. However, the page source does not contain the translated text because the form is not submitting. Here is part of the script: // initialze a curl handle $googleHandle = curl_init(); // set options for the googleHandle curl session curl_setopt($googleHandle, CURLOPT_RETURNTRANSFER, 0); curl_setopt($googleHandle, CURLOPT_COOKIEJAR, "cookies.txt"); curl_setopt($googleHandle, CURLOPT_COOKIEFILE, "cookies.txt"); curl_setopt($googleHandle, CURLOPT_REFERER, $referer); curl_setopt($googleHandle, CURLOPT_URL, "http://translate.google.com/translate_t?"); curl_setopt($googleHandle, CURLOPT_POST, 1); curl_setopt($googleHandle, CURLOPT_POSTFIELDS, "hl=en&ie=UTF8&text=$textToTrans&langpar=$langPair"); // execute curl_exec($googleHandle); // retrieve translated text // TODO // close the connection curl_close($googleHandle); Any suggestions/insight onto what is happening? Also, one last thing. Is it possible to write the source code of the web page I requested with curl_exec() to a text file? I'm going to need to parse for the translated text when I get this working, but I can't figure out how to send the source to a text file... Thanks Quote Link to comment Share on other sites More sharing options...
djarvis270 Posted January 10, 2008 Author Share Posted January 10, 2008 Ok wow... I just noticed the typo in CURLOPT_POSTFIELDS... So that's solved So... how do I send the source to a text file? Quote Link to comment Share on other sites More sharing options...
cunoodle2 Posted January 10, 2008 Share Posted January 10, 2008 You can store the entire file contents in a variable by just modifying this line like this... $file_contents = curl_exec($googleHandle); In case you need it here is how you write to a text file... <?php // Open the file and erase the contents if any $fp = fopen("textfile_name.txt", "w"); // Write the data to the file fwrite($fp, $file_contents); // Close the file fclose($fp); ?> Quote Link to comment Share on other sites More sharing options...
djarvis270 Posted January 10, 2008 Author Share Posted January 10, 2008 Thank you sooo much. I just spent a good hour screwing around with CURLOPT_WRITEFUNCTION, CURLOPT_WRITEDATA... Kudos. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.