PHP cURL is driving me mad!


#1

Hi there,

I’m new to the forum and unfortunately new to the cURL library too. This function has been driving me mad. Can anyone help me out and tell me what I’m doing wrong in it?

Thanks in advance,
Paul.

/*RequestIncoming Function */
function RequestIncoming($user, $pass) {
global $bcURL;

print 'user: ’ . $user . ‘

’;
print 'pass: ’ . $pass . ‘

’;

$data = “userId=”.urlencode($user) . “&password=”.urlencode($pass);

print 'data: ’ . $data;
$get_url = $bcURL . ‘?’ . $data;
$cUrl = curl_init();

curl_setopt($cUrl, CURLOPT_URL, $bcURL);
curl_setopt($cUrl, CURLOPT_TIMEOUT, 30);
curl_exec($cUrl);

$code = curl_getinfo($cUrl, CURLINFO_HTTP_CODE);

curl_close($cUrl);

return $code;
print 'code: ’ . $code;
}


#2

Well for starters:
What is it supposed to do?
What is it actually doing?
Do you get any errors?
If you do get errors, what are they?

Having the answers to these questions will help us help you. :wink:

Thanks!


#3

Hi there,

I?m having a real heap of trouble with PHP and cURL.

My aim is to use this code (or something like it) to submit 2 fields to a remote server (using GET) and receive back 6 fields (again with GET).

I had chopped the code about quite a bit but below is my basic, not messed about with version. When I run the function, I get back a 401 unauthorized error although the Username and Password are correct. Presumably then, they are not reaching the remote server at all or in the correct state. Problem is, I know so little about cURL that I don?t know what the correct state is., and that?s before I start worrying about getting data back from the server!

Below is the guide from the service provider about communicating with their server. Unfortunately they have been of absolutely no help so far so I?d be really grateful if you could give me any suggestions about where I am going wrong and what I should do instead.

Yours,
Paul.

//Constants
$bcUser = ‘ConnectBusinessUK’;
$bcPass = ‘xxxxxx’;
$bcURL = ‘http://service.bulletinconnect.net/api/1/sms/in’;
$bcRatecode = FALSE;

//Receive function
function Receive($user, $pass) {
global $bcURL;
$data = “userId=”.urlencode($user)
. “&password=”.urlencode($pass);
$cUrl = curl_init();
curl_setopt($cUrl, CURLOPT_URL, $bcURL);
curl_setopt($cUrl, CURLOPT_HEADER, ‘Content-type: application/x-www-form-urlencoded’);
curl_setopt($cUrl, CURLOPT_GET, 1);
curl_setopt($cUrl, CURLOPT_GETFIELDS, $data);
curl_setopt($cUrl, CURLOPT_TIMEOUT, 30);
curl_exec($cUrl);
$code = curl_getinfo($cUrl, CURLINFO_HTTP_CODE);
curl_close($cUrl);
return ($code == 200 || $code == 204) ? TRUE : FALSE;
}

//Execute function
Receive($bcUser, $bcPass);


To Receive a Message

Clients can GET incoming messages from http://service.bulletinconnect.net/api/1/sms/in . Users of the AAPT Bulletin Connect will need to use a different URL for Sending and Receiving Messages/Staus updates. See the AAPT Bulletin Connect section for more details on what URL you should use.
Required Input parameters are:
? userId
? password

The userId and password are supplied to you by Bulletin Wireless when you sign up for a Bulletin Connect account. You may pass them to the server in a query string, or in the HTTP Authorization header in Basic format.
Bulletin Connect will respond to each and every HTTP request with one of the following result codes.
? 200- OK
? 204- No content to return
? 401- Unauthorized
? 500- Internal error
For 200 codes (success) Bulletin Connect will include a form encoded parameter list containing message information as described here
? messageID
? from
? to
? rateCode
? inReplyTold
? body
N.B. The order of the parameters may change so use value/pair matching rather than location mapping.


#4

It looks pretty basic for data acquisition, especially if the host has it set up to use the query string.

Have you tried this outside a script? I.e. running the query string from your browser, or telnet. It sounds like you’re not sure what exactly the ‘handshake’ protocol is, and by all means, before implementing it in a script, try it out :wink:

Other than that, Unauthorized usually means your credentials are incorrect, but it really depends on what your host considers ‘unauthorized’ (could even be the used HTTP protocol).