Navigate
Home
ArticleWiki
Forum
Journal
Search
Newsletter
Links
Tech News
expertsrt.com
Welcome Guest.
Username:

Password:

Remember me

cURL not working
Welcome, Guest. Please login or register.
November 21, 2008, 04:06:26 PM
11306 Posts in 1249 Topics by 501 Members
Latest Member: rosaline
Experts Round Table Network  |  Serverside Technology  |  PHP  |  cURL not working « previous next »
Pages: [1] 2
Author Topic: cURL not working  (Read 2214 times)
tableman

Offline Offline

Posts: 26


« on: April 17, 2007, 09:44:45 PM »

A form has entries like this:

Code:
<input type="text" name="first_name" size="24" />
<input type="text" name="last_name" size="24" />

<input type="hidden" name="must_have" value="12345" />

<input type="hidden" name="recipient" value="myemail@comcast.net" />
<input type="hidden" name="redirect" value="http://mywebsite.com" />
<input type="hidden" name="subject" value="Topic In Question" />

When the action in the form is set to go directly to the receiving file like this:

Code:
<form method="post" action="http://theirwebsite/receive.jsp" name="goodname">

everything works. The entries are received by http://theirwebsite/receive.jsp.

However, I need to use a separate file to send the form entries, so the form action is set like this:

Code:
<form method="post" action="http://mywebsite/separate.php" name="goodname">

Code in http://mywebsite/separate.php is like this:

Code:
<?php
$siteUrl="http://theirwebsite/receive.jsp/";
 $variablesToPost = array
(
"first_name",
"last_name",
"must_have",
"recipient",
"redirect",
"subject"
);

$postData= array(); foreach($variablesToPost as $variable)
{
$postData[] = $variable . '=' . htmlspecialchars($_REQUEST[$variable]);
}
$postData = encodeQueryString(implode('&', $postData));
 
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $siteUrl);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
curl_exec ($curl); curl_close ($curl);
?>

All hidden fields required by the destination file are in the form and listed in the array.

There must be something wrong with the code. No values are received by http://theirwebsite/receive.jsp.
Logged
CrYpTiC_MauleR
Site Builder

Offline Offline

Posts: 489



WWW
« Reply #1 on: April 17, 2007, 11:14:13 PM »

Could it be possible the other site's form code checks to see if a user-agent, referrer etc present? Have you tried submitting the other form excluding those from being sent from your browser to see if it still goes through?
Logged

[x] Fight | www.crypticmauler.com
"You must be
tableman

Offline Offline

Posts: 26


« Reply #2 on: April 17, 2007, 11:58:27 PM »

I can try excluding user-agent and referrer for the form direct submission, but I don't see how user-agent and referrer would be different in nature when submitting values from the separate file compared to the form.

A value in a hidden field identifies my account as the one the values are going to.

Why would referrer and user-agent not be present when using the separate file instead of the form?

On what basis would the other site's form code differentiate between the separate file and the form when it comes to user-agent and referrer?




« Last Edit: April 18, 2007, 12:54:19 AM by tableman » Logged
VGR
Mentor

Offline Offline

Posts: 682



WWW
« Reply #3 on: April 18, 2007, 06:15:22 AM »

I agree : try to send the data to a different server with a simple PHP script that just displays the POST message received (I can build sucha  receiver script on my server if you need)

This said, if your purpose is to just send via POST the data to a different server, there are other possibilities than cURL
if you can send the data via GET, it's also a lot easier. You can hide the transmission. Data passes in clear text on the network anyway, so it's not a real difference between GET and POST.
Logged

techie overlord, answers all kind of questions on http://www.europeanexperts.org
tableman

Offline Offline

Posts: 26


« Reply #4 on: April 18, 2007, 09:42:25 AM »

VGR, the different server test would be ideal.

Please go ahead and let me know.
Logged
GrandSchtroumpf
Mentor

Offline Offline

Posts: 409



« Reply #5 on: April 18, 2007, 02:32:15 PM »

> Data passes in clear text on the network anyway, so it's not a real difference between GET and POST.

AFAIK, not using GET for sensitive data is not a matter of clear vs encrypted, it's a matter of logged vs not logged.
POST data is not supposed to be logged, or if it is it's supposed to be kept in a very secure place so it does not show in traffic reports, webstats and others.
Logged
CrYpTiC_MauleR
Site Builder

Offline Offline

Posts: 489



WWW
« Reply #6 on: April 18, 2007, 07:46:57 PM »

Why would referrer and user-agent not be present when using the separate file instead of the form?

Because the form is being submitted by cURL not your browser you need to set the useragent and referrer in cURL. If the other form has checks for preventing malicious submitting like from bots it will likely be checking if useragent is set, many bots/scripts don't do that so crude layer of protection, also checking if the form was submitted with proper referrer too. I don't know how the form was coded so don't know if they do these checks. So you can trying setting those and see if the form submits properly.

I also concur with GrandSchtroumpf you should use POST, not only is it safer since it doesn't log and by the looks of your code it would log people's name which would not be very nice. Also if you are submitting the form using something other than what the form was intended it might alert the webmaster that someone is using a script to submit the form, which might get your IP banned.
Logged

[x] Fight | www.crypticmauler.com
"You must be
VGR
Mentor

Offline Offline

Posts: 682



WWW
« Reply #7 on: April 18, 2007, 11:02:53 PM »

done
minimalist
I already have a testpost.php so I changed the name of this one

http://www.fecj.org/extra/testpost_curl.php

I can add _REQUEST, _GET or phpinfo() call if you'd like
Logged

techie overlord, answers all kind of questions on http://www.europeanexperts.org
tableman

Offline Offline

Posts: 26


« Reply #8 on: April 19, 2007, 12:02:53 AM »

VGR, I tried sending to you.

I also refined the script a little:

Code:
<?php
$siteUrl="http://www.fecj.org/extra/testpost_curl.php/";
 $variablesToPost = array
(
"first_name",
"last_name",
"must_have",
"recipient",
"redirect",
"subject"
);

$postData= "";

foreach($variablesToPost as $variable)
{
$postData = '$variable'.url_encode($_REQUEST[$variable]);
}

//echo $postData;

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $siteUrl);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
curl_exec ($curl); curl_close ($curl);
?>

When I uncomment the echo, I would expect something to print, but nothing shows.

I know the values are there because I can get them to print otherwise.



Logged
VGR
Mentor

Offline Offline

Posts: 682



WWW
« Reply #9 on: April 19, 2007, 06:01:09 AM »

why is there a trailing slash in the called URI ?
Logged

techie overlord, answers all kind of questions on http://www.europeanexperts.org
tableman

Offline Offline

Posts: 26


« Reply #10 on: April 19, 2007, 09:35:04 AM »

For no good reason.  It is now removed and resending attempted.

All I need is to post form entries to http://mywebsite/separate.php and then post most of those entries to http://theirwebsite/receive.jsp.

As you stated before, there are other possibilities than cURL.

If there is a simpler way, maybe I should be doing that instead.

Any suggestions?

« Last Edit: April 19, 2007, 10:03:32 AM by tableman » Logged
VGR
Mentor

Offline Offline

Posts: 682



WWW
« Reply #11 on: April 19, 2007, 11:40:19 AM »

are you sure you have turned display_errors to On ? And error_level to ALL ?

because when I try your script, I get

[Thu Apr 19 19:39:44 2007] [error] [client 127.0.0.1] PHP Fatal error:  Call to undefined function url_encode()

the function is urlencode()
Logged

techie overlord, answers all kind of questions on http://www.europeanexperts.org
VGR
Mentor

Offline Offline

Posts: 682



WWW
« Reply #12 on: April 19, 2007, 11:51:55 AM »

fixed

Code:
<?php
$siteUrl="http://www.fecj.org/extra/testpost_curl.php/";
 $variablesToPost = array
(
"first_name",
"last_name",
"must_have",
"recipient",
"redirect",
"subject"
);

$postData=array(); //VGR19042007 FIXed from PHP curl documentation (cf infra)

foreach($variablesToPost as $variable)
{
$postData[$variable]=urlencode($_REQUEST[$variable]); //VGR19042007 FIXed : it works now ;-)
}

//echo $postData;

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $siteUrl);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
$postResult = curl_exec ($curl); //VGR19042007 récupérer le résultat est toujours une bonne idée pour une FONCTION ;-)
//VGR19042007 ADDed this as per http://fr.php.net/manual/en/ref.curl.php
if (curl_errno($ch)) {
  print curl_error($ch);
}
curl_close ($curl);
print "résult : $postResult";
//EoAdd
?>
Logged

techie overlord, answers all kind of questions on http://www.europeanexperts.org
tableman

Offline Offline

Posts: 26


« Reply #13 on: April 19, 2007, 03:36:46 PM »

Merci.

When I send to $siteUrl="http://www.fecj.org/extra/testpost_curl.php";, it looks like it is working. Did you receive anything from me?

However, when I send to the vendor (norvax), I get this:

500 Servlet Exception
java.lang.NullPointerException
   at javax.mail.internet.MimeUtility.checkAscii(MimeUtility.java:1286)
   at javax.mail.internet.MimeUtility.encodeWord(MimeUtility.java:613)
   at javax.mail.internet.MimeUtility.encodeText(MimeUtility.java:444)
   at javax.mail.internet.MimeMessage.setSubject(MimeMessage.java:793)
   at javax.mail.internet.MimeMessage.setSubject(MimeMessage.java:757)
   at norvaxbeans.MailUtils.sendMail(MailUtils.java:363)
   at _jsp._sendMail__jsp._jspService(sendMail.jsp:15)
   at com.caucho.jsp.JavaPage.service(JavaPage.java:60)
   at com.caucho.jsp.Page.pageservice(Page.java:570)
   at com.caucho.server.dispatch.PageFilterChain.doFilter(PageFilterChain.java:175)
   at com.caucho.server.webapp.DispatchFilterChain.doFilter(DispatchFilterChain.java:115)
   at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:229)
   at com.caucho.server.webapp.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:277)
   at com.caucho.server.webapp.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:106)
   at com.caucho.jsp.PageContextImpl.forward(PageContextImpl.java:980)
   at _jsp._quoteMail__jsp._jspService(quoteMail.jsp:9)
   at com.caucho.jsp.JavaPage.service(JavaPage.java:60)
   at com.caucho.jsp.Page.pageservice(Page.java:570)
   at com.caucho.server.dispatch.PageFilterChain.doFilter(PageFilterChain.java:175)
   at com.caucho.server.webapp.WebAppFilterChain.doFilter(WebAppFilterChain.java:178)
   at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:229)
   at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:268)
   at com.caucho.server.port.TcpConnection.run(TcpConnection.java:389)
   at com.caucho.util.ThreadPool.runTasks(ThreadPool.java:507)
   at com.caucho.util.ThreadPool.run(ThreadPool.java:433)
   at java.lang.Thread.run(Thread.java:619)

--------------------------------------------------------------------------------
Resin Professional 3.0.19 (built Mon, 15 May 2006 05:00:32 PDT) résult : 1

When I send directly from the form to them, that exception does not happen and everything works.
« Last Edit: April 19, 2007, 03:59:58 PM by tableman » Logged
VGR
Mentor

Offline Offline

Posts: 682



WWW
« Reply #14 on: April 19, 2007, 04:01:50 PM »

add perhaps the form encoding ?
nullpointer exception with a 500 internal error : the server expects something more...

silly java ;-)
Logged

techie overlord, answers all kind of questions on http://www.europeanexperts.org
Pages: [1] 2
« previous next »
    Jump to: