Jump to content

Help with CURL / form submission


djarvis270

Recommended Posts

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

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.