Processing
 

Twitter doesn't accept PHP's cURL, but Linux'

03/12/2015 06:43#1

timmyRS

Member

Joined at: 7 months ago

Post: 3

Thank: 0

Thanked: 0

In attempt to use the GET statuses/user_timeline API of Twitter, i wrote myself some nice PHP Script to generate all needed values and it works fine on the cURL-Linux-Command, but if I want to give PHP this Job, it just gives me

string(0) "" 

and Code 400. Code 400 means, according to Twitter's API Response Codes

The request was invalid or cannot be otherwise served. An accompanying error message will explain further. In API v1.1, requests without authentication are considered invalid and will yield this response. 

But as I already mentioned, it works fine in command line, so this doesn't apply.

You sure need the information about, what the command and PHP script is, so here is the snippet from my script, that generates the Linux-Command and executes the request:

$auth_str  = "Authentication: OAuth ..."; $headers = array($auth_str); // ...  if($method == "POST") {     echo "curl --request 'POST' '{$url}' --data '{$fields_string}' --header '{$auth_str}' --verbose"; } else {     echo "curl --get '{$url}' --data '{$fields_string}' --header '{$auth_str}' --verbose"; }  $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url."?".$fields_string); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER , true); curl_setopt($ch, CURLOPT_VERBOSE, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $content = curl_exec($ch); 
03/12/2015 07:47Top#2

Joined at: 10 months ago

Post: 11

Thank: 0

Thanked: 0

You should separate the GET/POST in two different section similar to your commandline example.

$ch = curl_init(); if($method == "POST"){     curl_setopt($ch, CURLOPT_URL, $url);     curl_setopt($ch, CURLOPT_POST, true);     curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string); } else{     curl_setopt($ch, CURLOPT_URL, $url."?".$fields_string); } // remove CURLOPT_CUSTOMREQUEST  // rest of the curl opts go here! 
14/09/2016 09:33Top#3

timmyRS

Member

Joined at: 7 months ago

Post: 3

Thank: 0

Thanked: 0

I did not get this particular script to work, but I found TwitterOAuth, which is a very helpful PHP library that made my life so much easier.

Similar articles

How do twitter giveaways work?

9 months ago - Reply: 2 - Views: 130

How may I get genuine followers on Twitter?

3 years ago - Reply: 33 - Views: 1,832

Login to Twitter sometime fail

7 months ago - Reply: 1 - Views: 27

Twitter doesn't accept PHP's cURL, but Linux'

1 year ago - Reply: 2 - Views: 92

Twitter OAuth on iOS

6 months ago - Reply: 1 - Views: 96

why am i getting an error on .verifyCredentials()

6 months ago - Reply: 1 - Views: 37

TwitteR setup_twitter_oauth() gives an error

6 months ago - Reply: 1 - Views: 31

Tweepy - Getting the author of a retweeted tweet

6 months ago - Reply: 0 - Views: 7

Retrieving data from twitter with JavaScript

6 months ago - Reply: 0 - Views: 4

Combined social media feed returning the wrong data

6 months ago - Reply: 0 - Views: 3

Django and Twitter request_token

6 months ago - Reply: 0 - Views: 3

Python HMAC hashed value encoding to base64

6 months ago - Reply: 1 - Views: 35

At the rate symbol in facebook

6 months ago - Reply: 0 - Views: 3

vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re