Solved

urlencode data for CURLOPT_POSTFIELDS in PHP?

Posted on 2009-04-14
2
2,482 Views
Last Modified: 2012-05-06
Hi,

1) Just wondering if we're supposed to be urlencoding the data before submitting via CURLOPT_POSTFIELDS in PHP?

2) And am I supposed to use CURLOPT_COOKIEJAR option when I've already successfully logged in.

I'm having trouble posting. I can login successfully but I can't seem to post. Any ideas?
$postdata["pass"] = $pass;
        $postdata["id"] = $id;
        $postfields = generate_postfields($postdata);
        curl_setopt($ch, CURLOPT_URL, $url);
        //curl_setopt($ch, CURLOPT_REFERER, $referer);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // fake to be Internet Explorer 6.0
        //curl_setopt($ch, CURLOPT_COOKIEJAR, "/home/discoup9/public_html/curl/cookieFileName.txt");
        curl_setopt($ch, CURLOPT_COOKIEFILE, "/home/discoup9/public_html/curl/cookieFileName.txt");
        curl_setopt($ch, CURLOPT_POST, 1 );
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
        $result = curl_exec($ch);
        curl_close($ch);

Open in new window

0
Comment
Question by:Victor Kimura
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 5

Accepted Solution

by:
palanee83 earned 500 total points
ID: 24146832
HI,

Answer #1

You can either use array or a string to send the post data ($postfields).  If this $postfields is a query string then you should encode the url using urlencode() function. If its an array then its not required.


Answer #2

I used to set  CURLOPT_COOKIEFILE as well as CURLOPT_COOKIEJAR to maintain session between the pages. The cookie file path should be absolute, it may not work if the path is relative.


Also i got the below information (just i copied and pasted here)  from php.net  User contribution note.  Please give a try if above step not fix ur issue. (http://in.php.net/curl_setopt)

Sometimes you can't use CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE becoz of the server php-settings(They say u may grab any files from server using these options). Here is the solution
1)Don't use CURLOPT_FOLLOWLOCATION
2)Use curl_setopt($ch, CURLOPT_HEADER, 1)
3)Grab from the header cookies like this:
preg_match_all('|Set-Cookie: (.*);|U', $content, $results);  
$cookies = implode(';', $results[1]);
4)Set them using curl_setopt($ch, CURLOPT_COOKIE,  $cookies);
0
 

Author Closing Comment

by:Victor Kimura
ID: 31570266
Hi palanee83,
Thanks for the info.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question