Data posting isn't working in PHP Curl - php

I'm trying to make a script that makes things easier for Apple users, I don't really want to expose it right now but those are the steps I need to follow.
Login to Apple and saving the cookies.
Getting the data from apple with the saved cookies.
Creating automation tools.
And i'm stuck in the first step, Curl isn't submitting the custom headers as I need to submit the json header.
Here is my code:
<?php
function login($url,$data){
$data=json_encode($data);
$login = curl_init();
curl_setopt($login, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($login, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($login, CURLOPT_NOBODY, false);
curl_setopt($login, CURLOPT_HEADER, false);
curl_setopt($login, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($login, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($login, CURLOPT_TIMEOUT, 40000);
curl_setopt($login, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($login, CURLOPT_URL, $url);
curl_setopt($login, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($login, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($login, CURLOPT_POST, TRUE);
curl_setopt($login, CURLOPT_POST, 1);
curl_setopt($login, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data))
);
curl_setopt($login, CURLOPT_POSTFIELDS, $data);
//ob_start();
$rez=curl_exec ($login);
}
$data = array("accountName" => "myemail#me.com", "password" => "MyPassWord","rememberMe" => "true");
$url="https://idmsa.apple.com/appleauth/auth/signin";
login($url,$data);
?>
Whenever I try tampering the data when sending request, it never shows the custom headers

Apple uses X-Apple-Widget-Key like CSRF. I think you need to catch it. You can find it on source code. You can get status by using curl_getinfo()
// your code .....
curl_setopt($login, CURLOPT_POSTFIELDS, $data);
//ob_start();
$rez = curl_exec($login);
$status = curl_getinfo($login);
print_r($status);

This would be unmanageable as a comment, so here is how i see what goes on the wire:
$verbiage = null;
if ($this->verbose) {
curl_setopt($ch , CURLINFO_HEADER_OUT , false);
curl_setopt($ch , CURLOPT_VERBOSE , true);
$verbiage = fopen('php://temp' , 'w+');
curl_setopt($ch , CURLOPT_STDERR , $verbiage);
}
$resp = curl_exec($ch);
if ($this->verbose) {
rewind($verbiage);
$verboseLog = stream_get_contents($verbiage);
$log->info("Verbose information:\n$verboseLog");
fclose($verbiage);
}
curl_close($ch);
where $log is a log4php object, you pick your display method.
EDIT adding a sample log with this 'trace' option enabled
INFO - testCreateLabRequest starts.
TRACE - Query for lab request [dispensary[1]inboundLabRequest[2]ForPatient[1]] returned no records for feed [toubibCreamedFeedGuid]
DEBUG - Sending json
{ *redacted* , "trackingId":"5ac4473b-407f-4d7d-83eb-1a3f9abe5b8b"}
INFO - Verbose information:
* Trying fe80::3636:3bff:fecf:804c...
* Connected to darkmax.local (fe80::3636:3bff:fecf:804c) port 80 (#0)
> POST /ehr/feed/lab/request HTTP/1.1
Host: darkmax.local
Protocol_version : 1.1
Connection: Close
Content-Type: application/json ; charset=utf-8
Proxy-Connection: Close
User-Agent: Portable EHR(106)
Accept: application/json, text/html;
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Content-Length: 851
* upload completely sent off: 851 out of 851 bytes
< HTTP/1.1 200 OK
< Date: Wed, 31 Aug 2016 02:04:03 GMT
< Server: Apachee/2.4.12
< Access-Control-Allow-Origin: *
< Origin: http://darkmax.local
< Connection: close
< Transfer-Encoding: chunked
< Content-Type: application/json; charset=utf-8
<
* Closing connection 0
The lines with a < correspond to what my cURL session is receiving from the remote peer. Having some visibility into the actual HTTP protocol flying from me-to-peer-back-to-me has helped me many times figure out the nature of some underlying issue.

Related

Get jsonRPC data with PHP curl,

Setting up a JSON-RPC on my vps which I want to connect via PHP CURL on my website doing a basic request and looking for getmasternodecount.
Tried many scripts and libraries before however none seems to work in my case. Now I try to write some basic php code, but this skill isnt my best.
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
function coinFunction () {
$feed = 'http://user:pass#ip/';
$post_string = '{"method": "getmasternodecount", "params": []}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $feed);
curl_setopt($ch, CURLOPT_PORT, port);
curl_setopt($ch, CURLOPT_USERPWD, "user:pass");
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
//curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/stratum', 'Content-length: '.strlen($post_string)));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json', 'Content-length: '.strlen($post_string)));
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
$data = coinFunction();
var_dump($data);
echo $data;
?>
And gives me this data dump:
string(127) "HTTP/1.1 403 Forbidden Date: Sun, 24 May 2020 00:06:21 GMT Content-Length: 0 Content-Type: text/html; charset=ISO-8859-1 " HTTP/1.1 403 Forbidden Date: Sun, 24 May 2020 00:06:21 GMT Content-Length: 0 Content-Type: text/html; charset=ISO-8859-1
When i delete all the var dump information etc, it send me a whitepage and sometimes NULL.
Kindly Regards,
Let's work with the first snippet. Since it's a POST request, file_get_contents is rather out of place here. Add the following setopt lines:
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
Without those, the result of curl_exec won't contain the returned content.
It would also be advisable to specify the Content-Type of the request (which is application/json). The server might handle it even without, but just in case:
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type:application/json'));
Authentication is another thing. Credentials in the URL suggest Basic, but the server might expect otherwise... See CURLOPT_HTTPAUTH.

Pinterest login with PHP and cURL not working

I have been trying to make cURL login into pinterest.com for the last 17 hours straight, have tried countless and countless different ways just with cURL but it does not work at all.
My current code only goes to the page but the data is not posted, so it does not login just takes me to the login page.
This first code is using USERPWD which is where it takes me to the login page but it does not login.
error_reporting(E_ALL);
ini_set("display_errors", 1);
$url = "https://www.pinterest.com/login/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // allow https verification if true
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // check common name and verify with host name
curl_setopt($ch, CURLOPT_SSLVERSION,3); //
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "pin.pem"); // allow ssl cert direct comparison
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE); // set new cookie session
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
curl_setopt($ch, CURLOPT_USERPWD, "email:password");
curl_setopt($ch, CURLOPT_SSLVERSION,3);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL connection, save cookie file, free up system resources
curl_close($ch);
and if I switch it from CURLOPT_USERPWD to
curl_setopt($ch, CURLOPT_POSTFIELDS, 'username_or_email=$email&password=$password');
it just displays a blank page.
The pin.pem is the X.509 Certificate (PEM) file.
Any direction to make this work would be greatly appreciated it.
Edit
new code but leaves blank page and I got the output with a few arrays and displays this:
Array ( [url] => https://www.pinterest.com/login/ [content_type] => [http_code] => 0 [header_size] => 0 [request_size] => 0 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.036169 [namelookup_time] => 3.3E-5 [connect_time] => 0.036186 [pretransfer_time] => 0 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => -1 [upload_content_length] => -1 [starttransfer_time] => 0 [redirect_time] => 0 [certinfo] => Array ( ) [redirect_url] => )
error_reporting(E_ALL);
ini_set("display_errors", 1);
$email = 'email';
$password = 'password';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.pinterest.com/login/');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSLVERSION,3); //
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . 'pin.pem');
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
curl_setopt($ch, CURLOPT_POSTFIELDS, 'username_or_email=$email&password=$password');
curl_setopt($ch, CURLOPT_SSLVERSION,3);
curl_exec($ch);
$output=#curl_exec($ch);
$info = #curl_getinfo($ch);
echo $output;
print_r($info);
curl_close($ch);
The Pinterest login process isn't quite that simple. They use a CSRF token which you must extract and send with your login, along with the username and password in the POST body.
Here is what an actual login request to Pinterest looks like, so you will need to emulate this with cURL.
POST /resource/UserSessionResource/create/ HTTP/1.1
Host: www.pinterest.com
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:28.0) Gecko/20100101 Firefox/28.0
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
X-CSRFToken: 2rjgN4Qv67pN4wX91kTr4eIkgF54CzJH
X-NEW-APP: 1
X-APP-VERSION: 737af79
X-Requested-With: XMLHttpRequest
Referer: https://www.pinterest.com/login/
Content-Length: 300
Cookie: csrftoken=2rjgN4Qv67pN4wX91kTr4eIkgF54CzJH; _pinterest_sess="aPgJnrIBzvSKLUY/4H5UocshliA47GkkGtHLQwo1H4IcQv58vrdazclonByOb4fWCzb3a3nycKjQzDc6SkCB9eBKoejaLiCjkKLk/QAFRn2x1pvHFlFM+1EoD01/yFxmeQKlvULYU9+qf4D6Mkj8A=="; _track_cm=1;
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
source_url=%2Flogin%2F&data=%7B%22options%22%3A%7B%22username_or_email%22%3A%22YOU%40YOUROMAIN.COM%22%2C%22password%22%3A%22YOURPASSWORD%22%7D%2C%22context%22%3A%7B%7D%7D&module_path=App()%3ELoginPage()%3ELogin()%3EButton(class_name%3Dprimary%2C+text%3DLog+In%2C+type%3Dsubmit%2C+size%3Dlarge)
The source_url data in the request is the POST body (urlencoded). Take note that username_or_email is your login (I put YOU%40YOURDOMAIN.COM) and password is the password.
What you will have to do is make a GET request to /login/ to establish a session and cookies in the cURL session. Then using the same cURL handle, you can switch to a POST request, set CURLOPT_POSTFIELDS with the data from the source_url...... line.
You will probably also need to set the headers X-CSRFToken, X-NEW-APP, X-APP-VERSION, and X-Requested-With to match the above (except you will need to figure out how to get the correct CSRF Token value).
Unfortunately I don't have the time right now to make a working example, the next paragraph may help. You will need to use your browser to help you debug some of the HTTP requests to figure out all the requests you may need to make to get all the relevant data for your request.
If you check out this answer it shows curl login with PHP and links to a number of useful other related answers with examples.
EDIT:
Here is a working example of using PHP and cURL to log in to Pinterest.
This code is a Pinterest PHP login example (works as of 2014-05-11]. You may ask yourself, can what I want to do be done with the API instead of this hackish code which could break at any time???
As you can see I parse the CSRF_Token out of the headers, you should probably do this for the APP-VERSION as well since it can update almost daily. Right now it's hard coded.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$username = 'you#yoursite.com'; // your username
$password = 'yourpassword'; // your password
// this is the http post data for logging in - username & password are substituted in later
$login_post = array(
'source_url' => '/login/',
'data' => '{"options":{"username_or_email":"%s","password":"%s"},"context":{}}',
'module_path' => 'App()>LoginPage()>Login()>Button(class_name=primary, text=Log In, type=submit, size=large',
);
$pinterest_url = 'https://www.pinterest.com/'; // pinterest home url
$login_url = $pinterest_url . 'login/'; // pinterest login page url
$login_post_url = $pinterest_url . 'resource/UserSessionResource/create/'; // pinterest login post url
// http headers to send with requests
$httpheaders = array(
'Connection: keep-alive',
'Pragma: no-cache',
'Cache-Control: no-cache',
'Accept-Language: en-US,en;q=0.5',
);
// http headers to send when logging in
$login_header = array(
'X-NEW-APP: 1',
'X-APP-VERSION: d2bb370', // THIS WILL UPDATE FREQUENTLY, CHANGE IT!!!
'X-Requested-With: XMLHttpRequest',
'Accept: application/json, text/javascript, */*; q=0.01');
// ----------------------------------------------------------------------------
// request home page to establish cookies and a session, set curl options
$ch = curl_init($pinterest_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Iron/31.0.1700.0 Chrome/31.0.1700.0');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_STDERR, fopen('/tmp/debug.txt', 'w+'));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheaders);
$data = curl_exec($ch);
// ----------------------------------------------------------------------------
// parse the csrf token out of the cookies to set later when logging in
list($headers, $body) = explode("\r\n\r\n", $data, 2);
preg_match('/csrftoken=(.*?)[\b;\s]/i', $headers, $csrf_token);
// next request the login page
curl_setopt($ch, CURLOPT_URL, $login_url);
$data = curl_exec($ch);
// ----------------------------------------------------------------------------
// perform login post
$login_header[] = 'X-CSRFToken: ' . $csrf_token[1];
$login_post['data'] = sprintf($login_post['data'], $username, $password);
$post = http_build_query($login_post);
curl_setopt($ch, CURLOPT_URL, $login_post_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge($httpheaders, $login_header));
curl_setopt($ch, CURLOPT_REFERER, $login_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
// check response and output status
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200) {
echo "Error logging in.<br />";
var_dump(curl_getinfo($ch));
} else {
$response = json_decode($data, true);
if ($response === null) {
echo "Failed to decode JSON response.<br /><br />";
var_dump($response);
} else if ($response['resource_response']['error'] === null) {
echo "Login successful, " . $response['resource_response']['data']['username'] . "<br /><br />";
echo "You have {$response['resource_response']['data']['follower_count']} followers, are following {$response['resource_response']['data']['following_count']} users. You have liked {$response['resource_response']['data']['like_count']} pins.";
}
}
My output:
Login successful, drew010
You have 0 followers, are following 0 users. You have liked 0 pins.
FYI, Pinterest has login rate limit so don't run this before every request.
Here is my Ruby implementation of the Pinterest login/session mechanism.
Run this once a day to save the headers (including csrftoken). Then use the saved headers to do requests that are not (yet) supported by the api (like ads reports).
class PinterestHeadersScheduler
include Sidekiq::Worker
sidekiq_options queue: :recurring, retry: 0
HOMEPAGE = 'https://ads.pinterest.com/'
LOGIN_URL = "#{HOMEPAGE}login/"
SESSION_URL = "#{HOMEPAGE}resource/UserSessionResource/create/"
LOGIN_DATA = {
source_url: '/login/',
data: { options: { username_or_email: ENV['PI_USERNAME'], password: ENV['PI_PASSWORD'] }, context: {} }.to_json
}
HEADERS = {
'Accept': 'application/json,text/html,image/webp,image/apng,*/*;q=0.8',
'Origin': 'https://ads.pinterest.com',
'Referer': 'https://ads.pinterest.com/',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36',
'Connection': 'keep-alive',
'Cache-Control': 'no-cache',
'Accept-Charset': 'utf-8;ISO-8859-1q=0.7,*;q=0.7',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'en-US,en;q=0.8'
}
SESSION_HEADERS = HEADERS.merge({
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'X-Requested-With': 'XMLHttpRequest'
})
def perform
login = HTTParty.get(LOGIN_URL, { headers: HEADERS })
cjar = login.get_fields('Set-Cookie').each_with_object(HTTParty::CookieHash.new) { |cookie, jar| jar.add_cookies(cookie) }
headers = SESSION_HEADERS.merge({ 'Cookie': cjar.to_cookie_string, 'X-CSRFToken': cjar[:csrftoken] })
res = HTTParty.post(SESSION_URL, { headers: headers, body: LOGIN_DATA.to_param })
session = JSON.parse(ActiveSupport::Gzip.decompress(res.body))
raise "login error #{session['resource_response']['error']}" if session['resource_response']['error']
cjar = res.headers.get_fields('Set-Cookie').each_with_object(HTTParty::CookieHash.new) { |cookie, jar| jar.add_cookies(cookie) }
save_session_headers(HEADERS.merge({ 'Cookie' => cjar.to_cookie_string }))
end
def save_session_headers(headers)
# replace this with your cache/db
Utils::RedisUtil.set(:pinterest_session_headers, headers.to_json)
end
end

php - CURL HTTP Authentication at server side

i want to change xml value in php but don't change ....
<?php
$url = "http://192.168.1.103:8080/ew.xml";
$xml = '<?xml version="1.0" ?>
<DDCConfig:getValue xmlns:DDCConfig="urn:SMUDDCConfiguration">
<DDCConfig:Network>
<DDCConfig:LocalIP>192.168.103.223</DDCConfig:LocalIP>
<DDCConfig:GlobalIP>168.188.127.123</DDCConfig:GlobalIP>
<DDCConfig:RootBridge>Yes</DDCConfig:RootBridge>
</DDCConfig:Network>
</DDCConfig:getValue>';
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_USERPWD, "lmk:alrud89");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_URL, "$url");
curl_setopt($ch, CURLOPT_PORT, 8080);
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,array($xml));
$http_result = curl_exec($ch);
if($http_result){
echo $http_result;
}else{
echo curl_error($ch);
}
curl_close($ch);
?>
error message is
* About to connect() to 192.168.1.103 port 8080 (#0)
* Trying 192.168.1.103...
* connected
* Server auth using Basic with user 'lmk'
> PUT /ew.xml HTTP/1.1
> Authorization: Basic bG1rOmFscnVkODk=
> Host: 192.168.1.103:8080 Accept: /
> Transfer-Encoding: chunked
> Expect: 100-continue
< HTTP/1.1 401 Unauthorized
< Content-Length: 0
<
> WWW-Authenticate: Digest qop="auth", realm="mydomain.com",
nonce="1366254379"
* HTTP error before end of send, stop sending
* Closing connection #0
what is the problem & solution?
Your credentials are incorrect! (that's what error 401 means)
There's a wonderful plugin for Firefox called poster - install it and use it to double-check that the request is formatted correctly.
By the way, you might want to add the following header:
curl_setopt ($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/xml"))
Another thing: PUT is not always supported, try POST - sometimes it'll work.

Making a PUT request using CURL in PHP

I am using PHP 5.3.6 and it seems I am unable to make a PUT request using CURL for PUTting just a string.
function put_data($url, $data)
{
$useragent="SimpleAgent-1.0";
$fh = fopen('php://memory', 'rw');
fwrite($fh, $data);
rewind($fh);$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_INFILE, $fh);
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($data));
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
fclose($fh);
return $result;
}
Here, $data is the string that I want to PUT.
Doesn't work and returns the following error:
500 Internal Server Error The server has either erred or is incapable
of performing the requested operation.
expected string or buffer
I used your code so defined a url and filled the data with a string and all worked as expected. Being I was rejected by the website as there was no receiving end that could deal with a put. To get info easily just add the line
curl_setopt($ch, CURLOPT_VERBOSE, true);
and you will get something like:
* About to connect() to yyyy.xxxx.com port 80 (#0)
* Trying 62.221.196.28...
* connected
* Connected to yyyy.xxxx.com (zz.221.196.28) port 80 (#0)
> PUT / HTTP/1.1
User-Agent: SimpleAgent-1.0
Host: yyyy.xxxx.com
Accept: */*
Content-Length: 14
Expect: 100-continue
< HTTP/1.1 100 Continue
* We are completely uploaded and fine
< HTTP/1.1 405 Method Not Allowed
< Date: Thu, 09 Feb 2012 19:46:28 GMT
< Server: Apache
< Allow: GET,HEAD,POST,OPTIONS
< Vary: Accept-Encoding
< Content-Length: 231
< Content-Type: text/html; charset=iso-8859-1
<
* Connection #0 to host yyy.xxxx.com left intact
* Closing connection #0
As you can see from the logging the request went out, however when you want to put the data, the apache setting doesn't allow you to put data to that url. So depending upon the server you will have to take care of a receiving url that accepts the PUT.
I am only able to pass the array as a data with the versions I am using. So this is what I am doing now:
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
curl_setopt($ch, CURLOPT_COOKIE, $curl_cookie);
$arr = array();
if (isset($data)) {
$arr['my_data'] = $data;
}
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($arr));
curl_exec($ch);

Why am I getting HTTP_CODE 0 from curl?

I have been using curl with PHP for a while. Today I've been trying to fetch http://www.webhostingstuff.com/category/Best-Hosting.html and I keep getting http code 0, which is new to me.
I set the headers
$s->headers = array(
"User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1",
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language: en-gb,en;q=0.5",
"Accept-Encoding: gzip, deflate",
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7",
"Keep-Alive: 115",
"Connection: keep-alive",
"Referer: https://google.com"
);
and I have a cookie file (which has nothing in it when the script finishes loading)
Here's the curl function
function fetch($url, $username='', $data='', $proxy=''){
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
if(isset($proxy)) {
curl_setopt($ch,CURLOPT_TIMEOUT,30);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, $proxy);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'proxyadmin:parola');
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FRESH_CONNECT,true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
if(!empty($username)) {
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie/{$username}.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie/{$username}.txt");
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
if (is_array($data) && count($data)>0) {
curl_setopt($ch, CURLOPT_POST, true);
$params = http_build_query($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
}
if (is_array($this->headers) && count($this->headers)>0){
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
}
$this->result = curl_exec($ch);
$curl_info = curl_getinfo($ch);
$header_size = $curl_info["header_size"];
$this->headers = substr($this->result, 0, $header_size);
$this->http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$this->error = curl_error($ch);
curl_close($ch);
}
I've also tried to execute via SSH from a different server(in case it's IP blocked)
[brian#ip-184-168-22-244 ~]$ curl -url http://www.webhostingstuff.com/addcomments/5ite.html
Enter host password for user 'rl':
curl: (7) couldn't connect to host
[brian#ip-184-168-22-244 ~]$
How might I resolve this?
Your command
curl -url http://www.webhostingstuff.com/addcomments/5ite.html
should have been:
curl --url http://www.webhostingstuff.com/addcomments/5ite.html
cURL thinks you are specifying the -u option, which is used to specify a username, hence the error message you got. You need to specify --url (two dashes).
Hope that at least helps with the debugging.
Statuscode 0 means the connection was closed (gracefully) before any output was returned.
I guess I'd start by figuring out whether you can connect to the machine at all. If you have access to the remote machine it will likely help debugging.
In my case, the http code 0 was being returned because of a connection timeout. By adding
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
I was able to get rid of the error
Yesterday I faced similar problem. I spent 2 hours on this issue. I was on RHEL system. The curl code had below block for authentication:
if($httpCode == 200) {
$_SESSION["username"] = $username;
$_SESSION["password"] = $password;
return array(true, "Login successful. Please wait while you are redirected to home page.");
}else if($httpCode == 401){
return array(false, "Login failure. Incorrect username / password");
}
This code was being used for authentication. In lab environment it returned 200, but on production it returned 0.
Then I made a similar script (which used curl), and run if from command line like php test3.php . This run resulted in 200 status code.
Then out of curiosity I decided to turn off SELinux temporarily by running this command:
setenforce 0
And guess what this worked. You can then properly set context by running setsebool httpd_can_network_connect on
0 code means curl can't find the server you were looking for.
"yahoo.com/whatever" will return 404, while "yahoo.comwhatever" will return 0.
Maybe it was some internal server error? Right now it works:
> GET /category/Best-Hosting.html HTTP/1.1
> User-Agent: HTTP_Request2/0.5.2 (http://pear.php.net/package/http_request2) PHP/5.2.12
> Host: www.webhostingstuff.com
> Accept: */*
> Accept-Encoding: deflate, gzip
>
< HTTP/1.1 200 OK
< date: Sun, 31 Jul 2011 10:57:43 GMT
< server: Apache
< last-modified: Sun, 31 Jul 2011 10:55:00 GMT
< content-encoding: gzip
< vary: Accept-Encoding
< transfer-encoding: chunked
< content-type: text/html
I use HTTP_Request2 pear package as curl wrapper, the code:
$url = 'http://www.webhostingstuff.com/category/Best-Hosting.html';
$request = new HTTP_Request2 (
$url,
HTTP_Request2::METHOD_GET,
array (
'adapter' => new HTTP_Request2_Adapter_Curl(),
'ssl_verify_peer' => false,
)
);
$request->attach(new HTTP_Request2_Observer_Log('log.txt'));
$result = $request->send();

Categories