cURL WONT WORK!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
puma10
Forum Newbie
Posts: 11
Joined: Thu Aug 18, 2011 2:18 pm

cURL WONT WORK!

Post by puma10 »

I have been at this for days. I can not get even the simplest of cURL scripts to work. I am on hostmonster.com and they have curl enabled.

curl information = libcurl/7.21.7 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5

The tech support employee from hostmosnter I spoke with said he uses curl on the server daily.

Do I have to specify some sort of file path to the curl library? Mine would be /usr/bin/curl if this is the case.

Please help I'm at my witts end and 3 days past a deadline.

Thanks,
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: cURL WONT WORK!

Post by phphelpme »

I think you need to show us your code you have already otherwise nobody can do anything otherwise we are just guessing.

Maybe provide an example of your php enable functions etc

Code: Select all

<?
 phpinfo();
?>
Then paste the contents up here so we can see it.

Best wishes
puma10
Forum Newbie
Posts: 11
Joined: Thu Aug 18, 2011 2:18 pm

Re: cURL WONT WORK!

Post by puma10 »

Thank you for being patient with me phphelpme and thank you for your help. I rarely use forums so I apologize for not putting the whole story up at once.

Here is a link to my phpinfo http://joshwardini.com/sandbox/phpinfo.php

I have tried multiple ways but here is one that should really be working.

The first calls the class of Snoopy which is a library that acts a web browser. It uses cURL to accsess https websites. I posted the class code at http://joshwardini.com/sandbox/Snoopy_class_code.php

Here is my instance

Code: Select all

<?php 
	include "Snoopy.class.php";
		$snoopy = new Snoopy;
		$snoopy->curl_path="/usr/bin/curl"; //the directory provided my host
		
 
$post_data = array(
         'xnQsjsdp' => '*xfaer5q5',
         'xmIwtLD' => 'aasdfaf2kfjpaioufapofh525',
        'actionType' => 'afdkslfj8905',
	//'returnURL' => 'http://www.joshwardini.com/andrew',
	'First Name' => $_POST['First'],
	'Last Name' => $_POST['Last'],
	'Zip Code' => $_POST['Zip'],
	'Phone' => $_POST['Phone'],
	'Email' => $_POST['Email'],
	'Birth' => $_POST['Birth'],
	
);
    
 $snoopy->submit($submit_url,$post_data);
 //additionaly you can print the results with:
 print $snoopy->results;
?>
The snoopy library is an open source project. It's strange that the example says print $snoopy->results; because I can not find any variable called $result in the the submit function of the snoopy class.
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: cURL WONT WORK!

Post by phphelpme »

I think the first step here is to establish whether or not you can actually use cURL and if so has it got any issues. You say you can not use cURL at all even though your web hosts states that he uses it all the time. That depends on what account he has etc and what has been enabled for him.

It is clear from your phpinfo() that your cURL has been enabled so thanks for providing that for me just to rule out the obvious. :)

Now what you need to do is actually run a cURL script to see if it is actually working as it might need to be reset by your host:

Code: Select all

<?php 
        // create curl resource 
        $ch = curl_init(); 

        // set url 
        curl_setopt($ch, CURLOPT_URL, "http://google.com"); 

        //return the transfer as a string 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

        // follow location that value url takes
        curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );

        // $output contains the output string 
        $output = curl_exec($ch); 

        // close curl resource to free up system resources 
        curl_close($ch); 
		
		echo $output;
		
		?>
See if you get the output or an error of some kind. I assume you are not getting any error codes when you run your scripts mentioned earlier?

I have not known really that you have to specify a path to use cURL and I personally have never had to do that. If it is enabled then it should work just by calling the cURL function.

Best wishes
Last edited by phphelpme on Mon Aug 22, 2011 4:36 am, edited 1 time in total.
puma10
Forum Newbie
Posts: 11
Joined: Thu Aug 18, 2011 2:18 pm

Re: cURL WONT WORK!

Post by puma10 »

Thanks again for all your help phphelpme

The script you gave me returned this

"301 Moved

The document has moved here."

At which point you can click "here" and it takes you to google. Does that mean we got it partially working :D ?
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: cURL WONT WORK!

Post by phphelpme »

Can you provide a link at all to the file with the code I gave you so I can see what is happening. It would seem that it may have executed the cURL function and produced an output within another file. Not too sure though without having a look myself.

Also, just to make sure you might want to load the default php.ini file on your servers as the forums point to HostMonster customers having to complete this once or twice.

Here are the instructions for you to do this: https://my.hostmonster.com/cgi/help/128

Best wishes
RCA86
Forum Commoner
Posts: 32
Joined: Thu Mar 10, 2011 1:03 pm

Re: cURL WONT WORK!

Post by RCA86 »

Hey phphelpme, yes, this does mean cURL is working for you. :) The '301 Moved' output is produced because when the request got to the Google servers, they said you'd be better going to 'www.google.com' instead of 'google.com'. This happens if you try to access '.com' instead of '.co.uk' (and don't have cookies that say you actually want the US version... etc etc).

So you should be able to try it with other addresses now.

EDIT: sorry, just for a bit more detail. If you were to go to 'google.com' in your browser, you'll notice your browser jumps to the full version. It does this because the Google server returns the '301' output, which your browser interprets, and it follows the link to the full version. Your code doesn't know what '301' means, so it just takes that output and gives it to you. You can add an extra cURL option to force cURL to follow the redirects until it gets a valid result:

curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: cURL WONT WORK!

Post by phphelpme »

To be honest I thought I had put the followlocation within the cURL example I sent to puma10. lol I did not realise I had missed it out... lol Silly thing to do but you totally correct about the 301 producing what it did. lol

Hahaha, it is actually puma10 who started this post but thanks for the kind advice.. lol :)

Best wishes
puma10
Forum Newbie
Posts: 11
Joined: Thu Aug 18, 2011 2:18 pm

Re: cURL WONT WORK!

Post by puma10 »

Let me reiterate my gratefulness for the help guys. I would still be stabbing around in the dark pulling my hair out if it wasn't for the both of you.

The script that phphelpme gave me worked!! Google was directly retrieved after inputting FOLLOWLOCATION.
RCA86 wrote:curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
Now we can be sure cURL is working. However I still can't get the post to work. I tried two different scripts, both which I found online and modified slightly.

The first can be seen at joshwardini.com/sandbox/curl.php. You can see that we are redirected from zoho to manageengine.com upon execution. I just substituted strings in for the values of post for the time being so I wouldn't have to enter info into the form every time. It would appear that this script doesn't use the _POST but instead passes values through the url. This could be why it's not being accepted. The the strange characters are some sort of encoding that pertains to the particular zoho account I am working with. I have modified them in this forum post for security purposes.

Code: Select all

<?php
extract($_POST);

$url = 'https://crm.zoho.com/crm/WebToLeadForm';
$fields = array(
                'xnQ6asfgp' => '*5t626sgVJI$',
    		'xmIfafD' => 'nGtvJTI-26fg363Rr6PHYDTxR5kYBU',
    		'actionType' => 'TGVh43ag',
		'Last Name'=>urlencode("Joe"),
                'First Name'=>urlencode("Schmoe")
        );

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

// set referer:
curl_setopt($ch, CURLOPT_REFERER, "http://www.google.com/");

//return the transfer as a string 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// This allows you to type things like google.com and not http://www.google.com
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

print_r($result);
?>



The second actually uses _POST but returns no output, no html and no entry in the database. I got this one from http://curl.haxx.se/libcurl/php/example ... epost.html

Code: Select all

<?php
//
// A very simple PHP example that sends a HTTP POST to a remote site
//
$post_data = array(
        'xnQsjsdp' => '*5tv2RVJI$',
        'xnQ6asfgp' => '*5t626sgVJI$',
    	'xmIfafD' => 'nGtvJTI-26fg363Rr6PHYDTxR5kYBU',
    	'actionType' => 'TGVh43ag',
	'First Name' => 'joe',
	'Last Name' => 'schmoe',
	
);



$ch = curl_init();
// set referer:
curl_setopt($ch, CURLOPT_REFERER, "http://www.google.com/");

// This allows you to type google.com and not http://www.google.com
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );

curl_setopt($ch, CURLOPT_URL,"https://crm.zoho.com/crm/WebToLeadForm");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);

curl_exec ($ch);
curl_close ($ch); 
?>
I'm not sure if I have mentioned this yet or not but when I set the form action to https://crm.zoho.com/crm/WebToLeadForm instead of a php processor script everything works smoothly.
Thanks again so much for your help guys.
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: cURL WONT WORK!

Post by phphelpme »

I am a little confused as I still dont get what it is you are actually trying to achieve here. lol

Your script on your example page works and produces the page as a string value on your own domain name. cURL allows you to connect and scrape content from one website to another. What is it you actually want to achieve here.

If you change the action to the official handler on the official site it will work no problem because you are using their script to output the results. lol

Best wishes
puma10
Forum Newbie
Posts: 11
Joined: Thu Aug 18, 2011 2:18 pm

Re: cURL WONT WORK!

Post by puma10 »

I think perhaps I communicate better graphically.
Here is what I'm attempting to do http://joshwardini.com/andrew/images/data_structure.gif

The reason I opted to use cURL is because I must post to zoho with HTTPS. Other methods could be used for http but everything I have found online states that cURL is the best (if not only) method to post to HTTPS.

Thanks again for your persistence haha.
poster123
Forum Newbie
Posts: 3
Joined: Fri Aug 26, 2011 3:02 am

Re: cURL WONT WORK!

Post by poster123 »

Hey phphelpme, yes, this does mean cURL is working for you. The '301 Moved' output is produced because when the request got to the Google servers, they said you'd be better going to 'www.google.com' instead of 'google.com'. This happens if you try to access '.com' instead of '.co.uk' (and don't have cookies that say you actually want the US version... etc etc).
Post Reply