THis code is not showing up the google home page. Please point out the error in it.
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://www.google.com.kw");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
print $result;
?>
Remember that if your response is string-based; curl automatically encodes the response in utf8. So it might be necessary to decode the String to get what you want. Otherwise, your code is OK but here is another variant:
<?php
$serviceURL = "https://www.google.com.kw";
$curl = curl_init();
$settings = array(
CURLOPT_URL => $serviceURL,
CURLOPT_POST => false,
CURLOPT_RETURNTRANSFER => true,
);
curl_setopt_array($curl, $settings);
$response = curl_exec($curl);
$response = utf8_decode ( $response ); // <== DECODES THE UTF8 ENCODED STRING.
if(curl_errno($curl)){
var_dump( curl_error($curl) );
exit;
}
print($response);
curl_close($curl);
Try This :
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://www.google.co.in");
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept-Encoding: gzip,deflate'));
curl_setopt($curl,CURLOPT_ENCODING, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HEADER, 1);
$result = curl_exec($curl);
print $result;
Related
Am trying to implement ibm watson language translator using this IBM curl request data below
url -X POST -u "apikey:{apikey}" --header "Content-Type: application/json" --data "{\"text\": [\"Hello, world! \", \"How are you?\"], \"model_id\":\"en-es\"}" "{url}/v3/translate?version=2018-05-01"
I have written two codes in an attempt to get it work but when I run both codes below. it displays error
{"code":401, "error": "Unauthorized"}{"code":401, "error": "Unauthorized"}
please what can I do to get it to work. Thanks
first code attempt
<?php
$apikey ="my-api-key-goes-here";
$url = "https://api.eu-gb.language-translator.watson.cloud.ibm.com/instances/48bed10c-ce07-4a77-adec-014e0729de40/v3/translate?version=2018-05-01";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
'apikey'=>$apikey
]));
//$params_post ="{\"text\": [\"Hello, world! \", \"How are you?\"], \"model_id\":\"en-es\"}";
$params_post ='{"text":["Hello"],"model_id":"en-es"}';
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch,CURLOPT_CUSTOMREQUEST,'POST');
curl_setopt($ch,CURLOPT_POSTFIELDS, $params_post);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
echo $response = curl_exec($ch);
curl_close($ch);
print_r($response);
?>
second code attempt
<?php
$apikey ="my-api-key-goes-here";
//$params_post ="{\"text\": [\"Hello, world! \", \"How are you?\"], \"model_id\":\"en-es\"}";
$params_post ='{"text":["Hello"],"model_id":"en-es"}';
$uname ="apikey";
$pass =$apikey;
/*
$header = array(
'Content-Type: application/json',
"Authorization: apikey:$apikey"
);
*/
$header = array(
'Content-Type:application/json',
'Authorization: Basic '.$apikey
);
// Set options for REST call via curl
$endpointurl ="https://api.eu-gb.language-translator.watson.cloud.ibm.com/instances/48bed10c-ce07-4a77-adec-014e0729de40/v3/translate?version=2018-05-01";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $endpointurl);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERPWD, "$uname:$pass");
//curl_setopt($ch, CURLOPT_USERPWD, $uname . ":" . $pass);
//curl_setopt($curl, CURLOPT_USERPWD, 'apikey:' . $apikey);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl,CURLOPT_CUSTOMREQUEST,'POST');
curl_setopt($curl,CURLOPT_POSTFIELDS, $params_post);
echo $result = curl_exec($curl);
print_r($result);
?>
I know what this topic is old, but here is what helped me.
$url = "{URL here}/v3/translate? version = 2018-05-01";
$user = "apikey";
$pass = "{API key here}";
$options = array (
CURLOPT_RETURNTRANSFER =>true,
CURLOPT_FOLLOWLOCATION =>true,
CURLOPT_AUTOREFERER =>true,
);
$data = [
'text' =>[
'Hello, world!',
'How are you?'
],
'model_id' =>'en-es'
];
$ch = curl_init ();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_USERPWD, $user. ":". $pass);
curl_setopt ($ch, CURLOPT_HTTPHEADER, array ('Content-Type: application/json'));
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt ($ch, CURLOPT_VERBOSE, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, json_encode ($data));
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt_array ($ch, $options);
$result = curl_exec ($ch);
curl_close ($ch);
And here is where I found this
https://www.tutorialfor.com/questions-111457.htm
I hope it is helpful to someone.
I m try to update some data on mongodb using cms api
on terminal i can update information like this
curl -X PUT -d name=12345a https://api1.MYWEBISITE.com/api/v1in/user/2039/?t=mytoken
now using PHP i have try so many ways and no one looks to work for me
tried like this
class Curl {
public function put($url, $data_string){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
return $result;
}
}
$curl = new Curl;
$data_string = '{"name" : "name123"}';
$url = "https://api1.MYWEBSITE.com/api/v1in/user/2039/?t=mytoken";
echo $curl->put($url, $data_string);
also i tried like this
$data = array( "name" => '12344');
$url = "https://api1.mywebsite.com/api/v1in/user/2039/?t=mytoken";
$curl = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($curl);
$response = json_decode($result);
var_dump($response);
curl_close($curl);
both php solutions dont works, nothing is updated, no error msg is showing
any help?
Please can anyone help with me this, I am fed up by trying many methods but not getting proper the solution.
Below is my code I am trying to update liquid template file using php and curl but I am not able to do that.
<?php
error_reporting(-1);
$value = 'this is testing';
//Code for updating file
//$service_url = 'https://apikeyauthentication#test.myshopify.com/admin/themes/160467719/assets.json?asset[key]=snippets/product-preview.liquid&asset[value]=7788888&theme_id=160467719';
$service_url = 'https://apikeyauthentication#test.myshopify.com.myshopify.com/admin/themes/160467719/assets.json';
$curl = curl_init($service_url);
$curl_post_data = array(
'asset'=> array(
"key" => "snippets/product-akash123.liquid",
"value" => 'This is testing'
)
);
//echo json_encode($curl_post_data);die;
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: text/x-liquid'));
curl_setopt($curl, CURLOPT_PUT, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($curl_post_data));
//curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
//curl_setopt($curl, CURLOPT_POSTFIELDS,http_build_query($curl_post_data));
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
$curl_response = curl_exec($curl);
if ($curl_response === false) {
$info = curl_getinfo($curl);
var_dump(curl_error($curl));
curl_close($curl);
die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
curl_close($curl);
$decoded = json_decode($curl_response);
echo "<pre>";print_r(curl_getinfo($curl));
//echo curl_error($curl);
echo "<pre>"; print_r($decoded);die('loll');
?>
You'll want to set the Content-Type header to application/json. Try something like this:
<?php
$ch = curl_init('https://key:pass#yourstore.myshopify.com/admin/themes/160467719/assets.json');
$asset = array('asset'=> array(
'key' => 'snippets/yournewsnippet.liquid',
'value' => '{% comment %} here is your new snippet {% endcomment %}'
));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($asset));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$response = curl_exec($ch);
I'm using Drupal 7 and the services module and I'm trying to update a user profile using PHP & Curl.
Do I always have to login before sending a "PUT/update" ?
This is my code so far :
<?php
// REST Server URL
$request_url = 'http://mywebsite/end/user/login';
// User data
$user_data = array(
'username' => 'user2',
'password' => 'pass1',
);
// cURL
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $request_url);
curl_setopt($curl, CURLOPT_POST, 1); // Do a regular HTTP POST
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($user_data)); // Set POST data
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($curl);
print $response;
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
// Check if login was successful
if ($http_code == 200) {
// Convert json response as array
$logged_user = json_decode($response);
}
else {
// Get error msg
$http_message = curl_error($curl);
die($http_message);
}
print_r($logged_user);
// REST Server URL
$request_url = 'http://mywebsite.com/end/user/8&XDEBUG_SESSION_START=netbeans-xdebug';
$user_data = array('current_pass' => 'pass1', 'pass' => 'pass2');
// Define cookie session
$cookie_session = $logged_user->session_name . '=' . $logged_user->sessid;
// cURL
$curl = curl_init($request_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json',
'Content-type: application/json')); // Accept JSON response
curl_setopt($curl, CURLOPT_PUT, TRUE);
curl_setopt($curl, CURLOPT_HEADER, TRUE); // FALSE); // Ask to not return Header
curl_setopt($curl, CURLOPT_COOKIE, "$cookie_session"); // use the previously saved session
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_FAILONERROR, TRUE);
// Emulate file.
$serialize_args = json_encode($user_data);
$putData = fopen('php://temp', 'rw+');
fwrite($putData, $serialize_args);
fseek($putData, 0);
curl_setopt($curl, CURLOPT_INFILE, $putData);
curl_setopt($curl, CURLOPT_INFILESIZE, drupal_strlen($serialize_args));
$response = curl_exec($curl);
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
// Check if login was successful
$ret;
if ($http_code == 200) {
// Convert json response as array
$ret = json_decode($response);
}
else {
// Get error msg
$http_message = curl_error($curl);
die($http_message);
}
print_r($ret);
curl_close($curl);
}
?>
What am I missing here?
Nothing happens to my profile.
Any answer is welcomed!
Hope this can help u.
$service_url = 'http://mywebsite/end/user/login'; // .xml asks for xml data in response
$post_data = array(
'username' => 'user2',
'password' => 'pass1',
);
$post_data = http_build_query($post_data, '', '&'); // Format post data as application/x-www-form-urlencoded
// set up the request
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // have curl_exec return a string
curl_setopt($curl, CURLOPT_POST, true); // do a POST
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data); // POST this data
// make the request
curl_setopt($curl, CURLOPT_VERBOSE, true); // output to command line
$response = curl_exec($curl);
curl_close($curl);
// parse the response
$xml = new SimpleXMLElement($response);
$session_cookie = $xml->session_name .'='. $xml->sessid;
if(empty($xml->session_name) && empty($xml->sessid)){
echo 'Wrong';exit;
}
$service_url = 'http://mywebsite/end/user/token'; // .xml asks for xml data in response
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_POST, true); // do a POST
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data); // POST this data
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // have curl_exec return a string
curl_setopt($curl, CURLOPT_COOKIE, "$session_cookie"); // use the previously saved session
// make the request
curl_setopt($curl, CURLOPT_VERBOSE, true); // output to command line
$csrf_token = curl_exec($curl);
curl_close($curl);
$xml = new SimpleXMLElement($csrf_token);
$csrf_token = $xml->token;
$csrf_header = 'X-CSRF-Token: ' . $csrf_token;
// REST Server URL
$request_url = 'http://mywebsite/end/user/8&XDEBUG_SESSION_START=netbeans-xdebug';
$user_data = array('current_pass' => 'pass1', 'pass' => 'testing');
// cURL
$curl = curl_init($request_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json',
'Content-type: application/json',$csrf_header)); // Accept JSON response
curl_setopt($curl, CURLOPT_PUT, TRUE);
//curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($user_data)); // Set POST data
curl_setopt($curl, CURLOPT_HEADER, TRUE); // FALSE); // Ask to not return Header
curl_setopt($curl, CURLOPT_COOKIE, "$session_cookie"); // use the previously saved session
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_FAILONERROR, TRUE);
// Emulate file.
$serialize_args = json_encode($user_data);
$putData = fopen('php://temp', 'rw+');
fwrite($putData, $serialize_args);
fseek($putData, 0);
curl_setopt($curl, CURLOPT_INFILE, $putData);
curl_setopt($curl, CURLOPT_INFILESIZE, strlen($serialize_args));
$response = curl_exec($curl);
curl_close($curl);
You can achieve this by using "CURLOPT_COOKIEJAR" for writing and preserving cookies but you also need to set "CURLOPT_COOKIEFILE" for reading. More info can be found at http://php.net/manual/en/function.curl-setopt.php
define('COOKIE_FILE', "/tmp/sess" . time() . $user_data['username']);
curl_setopt ($curl, CURLOPT_COOKIEJAR, COOKIE_FILE);
curl_setopt ($curl, CURLOPT_COOKIEFILE, COOKIE_FILE);
I'm using php 5.3.2 and when i execute a curl it display the result directly without adding a print or echo function.
Here is my code:
<?php
$pvars = array('query' => 'ice age', 'orderby' => 'popularity');
$timeout = 30;
$myurl = "http://www.website.com";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $myurl);
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);
$xml = curl_exec($curl);
curl_close ($curl);
?>
What's wrong with my code and why it displays the result?
By default, the curl extension prints out the result.
You need to enable the CURLOPT_RETURNTRANSFER option, like so:
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
After that option is enabled, curl_exec will return the result, instead.
after php 5.1 curl always displays results as you can view on the documention. to avoid that simply do:
echo "< span style='display:none'>";
$pvars = array('query' => 'ice age', 'orderby' => 'popularity');
$timeout = 30;
$myurl = "http://www.website.com";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $myurl);
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);
$xml = curl_exec($curl);
curl_close ($curl);
echo"< /span>";