4

How can i send a CURL request mentioned below in PHP? What functions i will use in php?

$ curl -H 'X-Sifter-Token: 343b1b831066a40e308e0af92e0f06f0' \
-H 'Accept: application/json' \ 
'http://example.sifterapp.com/api/projects'

I have tried this code.. but its not working.. Please do the needful

$curlString = "";

$curlString .= "-H \"X-Sifter-Token: 343b1b831066a40e308e0af92e0f06f0\" \";

$curlString .= "-H \"Accept: application/json\" \";

$url="http://example.sifterapp.com/api/projects";


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, $curlString);
$data = curl_exec($ch);
if (curl_errno($ch)) {
print "Error: " . curl_error($ch);
} else {
// Show me the result
var_dump($data);
curl_close($ch);
}

1 Answer 1

8

You do not use correctly CURLOPT_HTTPHEADER. From the manual:

http://php.net/manual/en/function.curl-setopt.php

CURLOPT_HTTPHEADER An array of HTTP header fields to set, in the format array('Content-type: text/plain', 'Content-length: 100')

So you would need:

  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'X-Sifter-Token: 343b1b831066a40e308e0af92e0f06f0',
        'Accept: application/json',
  ));
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your reply. It completed the CURL request. But i am getting '301 Moved permanently' message. When i used var_dump($response),i got string(184). What all other php CURL function i need to use to complete this request. I am doing this for the first time.
HTTP 301 might mean that your request is slightly off, e.g. you asked for projects instead of projects/. You can check this by having cURL output all the headers. Fixing the request is more performant; but you can probably have cURL automatically retry with the option to follow redirects (see the setopt man page).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.