Trying to access a url using curl - php

I'm trying to access access a URL using Curl, inn which I would like to status of the url, whether it is redirected, not found, suceess etc...
The code I'm using is working at certain times, but not always..
curl -u 98ur5t9#:9#98ur5t -v --silent --request GET "http://$url/laber.txt" --include 2>&1 | grep -q "HTTP/1.1 200 OK" 2> /dev/null
if [ $? -eq 0 ]
echo 'url ok'
else
echo 'url not oke'
fi
Can anyone help me with how do I get the correct status and if it is 200 echo ok as output..

curl -u 98ur5t9#:9#98ur5t -s -I http://www.example.org | grep -q "HTTP/1.1 200 OK"
if [ $? -eq 0 ]
echo 'url ok'
else
echo 'url not oke'
fi

Use --head to get only the response headers, something like:
response=$(curl --head -s -u 98ur5t9#:9#98ur5t GET "http://$url/laber.txt")
if [[ $response =~ 'HTTP/1.1 200 OK' ]]; then
echo 'url ok'
else
echo 'url NOT ok'
fi

Related

Get latest video from youtube channel

I want to put the latest video from my channel on my website. This returns the following error:
Warning: file_get_contents(https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId=UCD---------&maxResults=1&key=A---------------): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden in C:\xampp\htdocs\inc\latestVideo.php on line 15
My code:
<?php
$API_key = 'A---------------';
$channelID = 'UCD---------';
$maxResults = 1;
$videoList = json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId='.$channelID.'&maxResults='.$maxResults.'&key='.$API_key.''));
if(isset($item->id->videoId)){
echo '<div class="youtube-video">
<iframe width="280" height="150" src="https://www.youtube.com/embed/'.$item->id->videoId.'" frameborder="0" allowfullscreen></iframe>
<h2>'. $item->snippet->title .'</h2>
</div>';
}
?>
You should actually be doing a curl
curl \
'https://www.googleapis.com/youtube/v3/channels?part=snippet%2CcontentDetails%2Cstatistics&id=UC_x5XG1OV2P6uZZ5FSM9Ttw&maxResults=1&key=[YOUR_API_KEY]' \
--header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
--header 'Accept: application/json' \
--compressed
Notice how an Authorization bearer token is required on the header in addition to the API key. Also channelId needs to be id.
To understand how to do a curl call from PHP check this out ->
php curl: I need a simple post request and retrival of page example
Reference of the api is here -> https://developers.google.com/youtube/v3/code_samples/code_snippets?apix_params=%7B%22part%22%3A%22snippet%2CcontentDetails%2Cstatistics%22%2C%22id%22%3A%22UC_x5XG1OV2P6uZZ5FSM9Ttw%22%7D&apix=true
You can even try in the interactive editor.

Get and evaluate curl response in Jenkins

I am trying to get the response of a Http post with curl in Jenkins, I have the following script:
curl -X POST -k -H "Accept: application/json" -H "Content-Type: application/json" --data-binary "#/var/lib/jenkins/workspace/Folder/sessions.json" http://mypage/Data/file.php
As you can see I am sending to file.php a json file, and then I am calling some functions and am returning a specific result.
With that script, I am getting the result I want, but I want to evaluate that result, let's say for example the result was "OK", then I want to assign the result to a variable, and then say if $result=="OK" then do this else do that. How can I do that, I have tried something like this:
if $response == "true" then exit 1 fi
But it does not seem to work out, does anyone know how it can be done?
They marked it as similar to this question
PHP cURL, extract an XML response
, but I don't see how, because I am not talking about php code, bash code, and I want to store the curl result in a variable....
Thanks in advance!!!
You can check the status code if you only need to check if the request was successful :
status=$(curl --write-out '%{http_code}' \
-s -o /dev/null \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
--data-binary "#/var/lib/jenkins/workspace/Folder/sessions.json" \
"http://mypage/Data/file.php")
if [ "$status" == "200" ]; then
echo "request was successful"
else
echo "error status : $status"
fi
with :
--write-out '%{http_code}' : output the status code
-o /dev/null : doesn't to output the body
-s : doesn't display connection log
As you have specified Accept: application/json, you expect a response in JSON format, so you could use jq JSON parser to parse it :
If the response is :
{ "status": true }
then you can do the following :
status=$(curl -s -H "Accept: application/json" \
-H "Content-Type: application/json" \
--data-binary "#/var/lib/jenkins/workspace/Folder/sessions.json" \
"http://mypage/Data/file.php" | jq -r '.status')
if [ "$status" == "true" ]; then
echo "request was successful"
else
echo "error status : $status"
fi
If the response is not in JSON format and the response is OK :
status=$(curl -s -H "Content-Type: application/json" \
--data-binary "#/var/lib/jenkins/workspace/Folder/sessions.json" \
"http://mypage/Data/file.php")
if [ "$status" == "OK" ]; then
echo "request was successful"
else
echo "error status : $status"
fi

Laravel RESTful API NOT receiving data from cURL Request

So I have a "Skeleton" written for my projects' RESTful API. I've been trying to POST data to the store() function through cURL. And so I used
if(Input::hasfile('file'))
to check if the image/data was posting and it returned false. How would I actually detect the image/data if I were to post it via a cURL request similar to this
curl.exe --user "admin:password" --header "Content-Type: multipart/form-data" --data-binary "#vok.png" --output "out.txt" --dump-header "header.txt" http://bitdrops.co/bitdrops/public/index.php/api/v1/drop/
Ignore the header and response output.
This is my current code for the store() function.
public function store()
{
$file = Input::file('file');
$destinationPath = public_path() . '/uploads';
$filename = $file->getClientOriginalName();
//$extension =$file->getClientOriginalExtension();
$upload_success = Input::file('file')->move($destinationPath, $filename);
if( $upload_success ) {
return Response::json('success', 200);
} else {
return Response::json('error', 400);
}
}
The issue appears that in your cURL statement
curl.exe --user "admin:password" --header "Content-Type: multipart/form-data" --data-binary "#vok.png" --output "out.txt" --dump-header "header.txt" http://bitdrops.co/bitdrops/public/index.php/api/v1/drop/
You are not passing the file as "file". As you commented earlier on the solution is:
curl.exe -i --user user:pass -F file=#vok.png --output "out.html" http://bitdrops.co/bitdrops/public/index.php/api/v1/drop

Send JSON data with Guzzle

I have got the following cURL command that is running expectedly:
curl -X POST
-H "Content-Type: application/json"
-d '{"duplicateEntitiesIds": [21,31,41]}'
{URL}:3003/v1/entities/5/merge
And I am trying to replicate that with Guzzle, which however fails, returning a 400 status code:
$request = $httpClient->post('{URL}:3003/v1/entities/'.$mainEntityId.'/merge',
['json' =>
['duplicateEntitiesIds' => $duplEntitiesIdsToArray]
]
);
$response = $request->send();
I have tried to change my post body , but it keeps failing. Any ideas would be appreciated.
NOTE
The data should be sent in the following format:
{"duplicateEntitiesId": [2,3,4]}

Unable to get JSON data in _POST variable in php

I'm using this curl command to send json data to a php webservice, but i'm not getting anything in the $_POST variable.
Here is the curl command
curl -X POST -i -H "Content-type: application/json" -c cookies.txt -X POST http://192.168.2.127:8888/json.php -d '{"age":"234","password":"password"}'
and here is the php code.
<?php
header('Content-type: application/json');
var_dump($_POST);
$return_arr = array();
$age = $_POST['age'];
$ageInt = intval($_POST['age']);
$return_arr['age1'] = $age;
var_dump($_POST);
$return_arr['age2'] = $ageInt;
echo json_encode($return_arr);
?>
Thanks in advance
Once try this
curl -X POST -H "Content-Type: application/json" -d '{"age":"21343","password":"password"}' http://192.168.2.127:8888/json.php

Categories