Pipe2290
Pipe2290

Reputation: 168

command line cURL parameters in PHP

I'm working on a PHP script that has to connect to an REST API. The provider of the API, suggested to use cURL. They gave me an example of how to use it in the command line:

curl -D- -u "user:password" -X GET -H "Content-Type: application/json" http://example.com/api/searchFunction?jql=assignee=user1 

The PHP script is the following:

<?php

$defaults = array(
CURLOPT_HEADER => true,
CURLOPT_URL => 'http://example.com/api/searchFunction?jql=assignee=user1', 
CURLOPT_USERPWD => "user:password",
CURLOPT_HTTPAUTH => 'CURLAUTH_BASIC'
);

$ch = curl_init();
curl_setopt_array($ch, ($defaults));

echo "cURL output: ".curl_exec($ch);

curl_close($ch);

 ?>

As you can imagine, the command line version works fine, but in the PHP version I got the following error:

Field 'assignee' does not exist or this field cannot be viewed by anonymous users.

That suggests that the user login validation doesn't works. However, the user and password are correct.

I was looking for already answered posts of cURL parameters equivalents between the command line version and the PHP version but couldn't find the correct parameters for the PHP version.

Upvotes: 1

Views: 1052

Answers (1)

Mr. Llama
Mr. Llama

Reputation: 20889

You haven't fully replicated your cURL command yet.

For starters, you've never set the Content-Type: application/json header option. You need to set that using the CURLOPT_HTTPHEADER option.

Secondly, command line cURL and PHP's cURL use different User-Agent values.

Consider enabling the command line cURL's verbose option so you can see all the information it's sending, then replicate it PHP.

Upvotes: 2

Related Questions